-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgulpfile.mjs
More file actions
192 lines (185 loc) · 5.61 KB
/
gulpfile.mjs
File metadata and controls
192 lines (185 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import fs from "fs-extra";
import inquirer from "inquirer";
import { format } from "prettier";
import { spawnSync, exec } from "child_process";
import util from "util";
import chalk from "chalk";
const execPromise = util.promisify(exec);
/**
* @param {string} oldVersion
* @returns {Promise<string>}
*/
const checkVersion = async (oldVersion) => {
if (oldVersion.includes("beta")) {
const V = oldVersion.split("-beta.")[0];
const B = Number(oldVersion.split("-beta.")[1]);
const update = `${V}-beta.${B + 1}`;
const publish = `${V}`;
const { option } = await inquirer.prompt([
{
type: "list",
name: "option",
message: `当前版本为${oldVersion ?? "未定义"},请选择更新级别`,
choices: [
{ value: "update", name: `更新当前测试(${update})` },
{ value: "publish", name: `结束当前测试(${publish})` },
{ value: "old", name: `不变更版本号(${oldVersion})` },
],
default: "update",
},
]);
if (option === "update") {
return update;
} else if (option === "publish") {
return publish;
} else {
return oldVersion;
}
} else {
const [H, N, S] = oldVersion.split(".").map((item) => Number(item));
const B = "-beta.0";
const small = `${H}.${N}.${S + 1}${B}`;
const normal = `${H}.${N + 1}.0${B}`;
const huge = `${H + 1}.0.0${B}`;
const { option } = await inquirer.prompt([
{
type: "list",
name: "option",
message: `当前版本为${oldVersion ?? "未定义"},请选择更新级别`,
choices: [
{ value: "small", name: `小版本测试(${small})` },
{ value: "normal", name: `普通版本测试(${normal})` },
{ value: "huge", name: `大版本测试(${huge})` },
{ value: "old", name: `不变更版本号(${oldVersion})` },
],
},
]);
if (option === "small") {
return small;
} else if (option === "normal") {
return normal;
} else if (option === "huge") {
return huge;
} else {
return oldVersion;
}
}
};
async function changeVersion(version) {
const packageFile = await fs.readJSON("./package.json");
const versionFilePath = "./src/config.ts";
const configFileStr = (await fs.readFile(versionFilePath, "utf-8"))
.replace("/** 请勿手动修改本文件,本文件通过命令行自动生成 */\n*", "")
.replace("export default ", "")
.replace(/\/\*\*.*\*\/\n*/g, "");
const configFile = JSON.parse(configFileStr);
packageFile.version = version;
configFile.version = version;
const str = format(
`/** 请勿手动修改本文件,本文件通过命令行自动生成 */\nexport default ${JSON.stringify(
configFile,
null,
2
)}`,
{
singleQuote: false,
trailingComma: "none",
semi: false,
quoteProps: "preserve",
parser: "babel",
}
);
await Promise.all([
fs.writeJson("./package.json", packageFile, {
spaces: 2,
}),
fs.writeFile(versionFilePath, str),
]);
spawnSync("npm run build", {
stdio: "inherit",
shell: true,
});
console.log(`config, package.json 的版本号已更新为${version}`);
}
async function getVersion() {
const packageFile = await fs.readJSON("./package.json");
/** @type { string } */
return packageFile.version;
}
export async function customizeVersion() {
// 确认版本
const oldVerion = await getVersion();
const { version } = await inquirer.prompt([
{
type: "input",
name: "version",
message: `当前版本为${oldVerion ?? "未定义"},请输入版本号`,
default: "oldVerion",
},
]);
await changeVersion(version);
}
const npmPublish = (version) => {
if (version.includes("beta")) {
const command = "npm publish --tag beta";
console.log(command);
return spawnSync(command, {
stdio: "inherit",
shell: true,
});
}
const command = "npm publish";
console.log(command);
return spawnSync(command, { stdio: "inherit", shell: true });
};
export async function publish() {
// 确认版本
const packageFile = await fs.readJSON("./package.json");
/** @type { string } */
let version = packageFile.version;
const checkedVersion = await checkVersion(version);
if (checkedVersion) {
if (version !== checkedVersion) {
version = checkedVersion;
await changeVersion(version);
spawnSync(`git add . && git commit -m "build: release v${version}"`, {
stdio: "inherit",
shell: true,
});
}
const branch = (
await execPromise("git rev-parse --abbrev-ref HEAD")
).stdout.trim();
if (branch === "master" || branch === "main") {
// 判断是否是`master`分支,如果是,允许任意版本发布
npmPublish(version);
spawnSync(`git add . && git commit -m "build: release v${version}"`, {
stdio: "inherit",
shell: true,
});
if (!version.includes("beta")) {
spawnSync(`git tag v${version}`, { stdio: "inherit", shell: true });
spawnSync(`git push origin --tags`, { stdio: "inherit", shell: true });
}
return;
} else {
// beta版本
if (!version.includes("beta")) {
console.log(
`当前分支为${chalk.yellow(branch)}, 非master分支只允许发布beta版本`
);
return await changeVersion(oldVersion);
} else {
npmPublish(version);
return spawnSync(
`git add . && git commit -m "build: release v${version}"`,
{
stdio: "inherit",
shell: true,
}
);
}
}
}
}
export default publish;