-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
217 lines (196 loc) · 4.87 KB
/
app.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const inquirer = require("inquirer");
const execShPromise = require("exec-sh").promise;
const ghpages = require("gh-pages");
// test 测试环境 pre 预发布环境 prod 正式环境 source 源码
const types = ["test", "pre", "prod", "source"];
const names = {
test: "测试环境",
pre: "预发布环境",
prod: "正式环境",
source: "源码",
};
function flog(...res) {
res = ["\n\n", "---->", ...res];
console.log(...res);
}
// 时间转化
const timeFormat = (type, time) => {
const addNum = (value) => value.toString().padStart(2, "0");
let date = time ? new Date(time) : new Date();
let formateArr = ["YY", "MM", "DD", "hh", "mm", "ss"];
let list = [];
list.push(date.getFullYear());
list.push(addNum(date.getMonth() + 1));
list.push(addNum(date.getDate()));
list.push(addNum(date.getHours()));
list.push(addNum(date.getMinutes()));
list.push(addNum(date.getSeconds()));
for (let i in formateArr) {
type = type.replace(formateArr[i], list[i]);
}
return type;
};
// 获取版本号
const getTag = {
async prod(name) {
const { tag } = await inquirer.prompt([
{
type: "input",
message: `${name}Tag name(提示:必须包含项目名缩写以及版本号,如v1.0.0)`,
name: "tag",
validate: (value) => {
if (!/^v\d+\.\d+\.\d+$/.test(value)) {
return false;
}
return true;
},
},
]);
return tag;
},
async pre(name) {
const { tag } = await inquirer.prompt([
{
type: "input",
message: `${name}Tag name(提示:必须包含项目名缩写以及版本号,如v1.0.0.p1)`,
name: "tag",
validate: (value) => {
if (!/^v\d+\.\d+\.\d+(\.p\d+)$/.test(value)) {
return false;
}
return true;
},
},
]);
return tag;
},
async source(name) {
const { tag } = await inquirer.prompt([
{
type: "input",
message: `${name}Tag name(提示:必须包含项目名缩写以及版本号,如v1.0.0)`,
name: "tag",
validate: (value) => {
if (!/^v\d+\.\d+\.\d+$/.test(value)) {
return false;
}
return true;
},
},
]);
return tag;
},
test() {
return Promise.resolve("");
},
};
// git提交
const publish = (options) => {
return new Promise((resolve, reject) => {
ghpages.publish(
"dist",
{
...options,
},
(err) => {
if (err) {
reject(err);
} else {
resolve();
}
}
);
});
};
async function deploy() {
let buildFlag = false;
const { type } = await inquirer.prompt([
{
type: "list",
message: "部署什么环境?",
name: "type",
choices: types,
},
]);
if (!type.includes("source")) {
const { build } = await inquirer.prompt([
{
type: "confirm",
message: `是否需要重新build 运行npm run build命令?`,
name: "build",
},
]);
buildFlag = build;
}
const { branch } = await inquirer.prompt([
{
type: "input",
message: `请输入${names[type]}git branch(默认为:master)`,
name: "branch",
default: "master",
validate: (value) => {
if (!type.includes("prod") && value === "master") {
// return "master是正式环境分支,输入正确分支名";
}
return true;
},
},
]);
const { commit } = await inquirer.prompt([
{
type: "input",
message: `请输入commit message(默认为:版本更新)`,
name: "commit",
default: "版本更新",
},
]);
const tag = await getTag[type](names[type]);
if (!type.includes("source") && buildFlag) {
try {
await execShPromise(`npm run build`);
flog("打包完成");
} catch (err) {
flog(`build 出错,`, err);
return;
}
}
const params = {
// repo: "/ad_front_dist.git",
message: commit,
branch,
};
tag && (params.tag = tag);
console.log(params);
if (type.includes("source")) {
ghpages.publish(
"./",
{
dotfiles: true,
message: commit,
branch,
tag
},
async (err) => {
if (err) {
flog("推送失败", err);
} else {
await execShPromise("git fetch");
flog("推送成功");
flog("tag:", tag);
}
}
);
return;
}
await publish(params)
.then(async (res) => {
await execShPromise("git fetch");
flog("推送成功");
flog("tag:", tag);
})
.catch((err) => {
flog("推送失败", err);
return;
});
}
deploy();