@taole/deploy-helper 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{index.js → index.mjs} +61 -33
- package/package.json +6 -4
package/{index.js → index.mjs}
RENAMED
|
@@ -30,7 +30,7 @@ async function main() {
|
|
|
30
30
|
|
|
31
31
|
// other commands
|
|
32
32
|
|
|
33
|
-
if(process.argv[2] === "help"){
|
|
33
|
+
if (process.argv[2] === "help") {
|
|
34
34
|
console.log(`deploy-helper 部署脚本`);
|
|
35
35
|
console.log(`usage: deploy-helper [mode]`);
|
|
36
36
|
console.log(`mode: prod 生产环境部署`);
|
|
@@ -38,7 +38,7 @@ async function main() {
|
|
|
38
38
|
process.exit(0);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
if(!["prod", "test"].includes(process.argv[2])){
|
|
41
|
+
if (!["prod", "test"].includes(process.argv[2])) {
|
|
42
42
|
console.log("不支持的命令");
|
|
43
43
|
process.exit(1);
|
|
44
44
|
}
|
|
@@ -57,6 +57,12 @@ async function main() {
|
|
|
57
57
|
return;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
// 需要处理entry
|
|
61
|
+
const needHandleEntry = (mode === 'prod' && config.entry.onlyProd) || (mode === 'test' && !config.entry.onlyProd);
|
|
62
|
+
const entryTestBranch = (config.entry && config.entry.testBranch) || "test";
|
|
63
|
+
const entryProdBranch = (config.entry && config.entry.prodBranch) || "master";
|
|
64
|
+
const entryTargetBranch = mode === 'prod' ? entryProdBranch : entryTestBranch;
|
|
65
|
+
|
|
60
66
|
// 检查项目
|
|
61
67
|
// 1. 检查项目是否存在
|
|
62
68
|
const assetsDest = join(workDir, config.assets.dest);
|
|
@@ -65,24 +71,25 @@ async function main() {
|
|
|
65
71
|
console.log(`assets.dest路径不存在: ${assetsDest}, 请先创建`);
|
|
66
72
|
return;
|
|
67
73
|
}
|
|
68
|
-
//
|
|
69
|
-
if (
|
|
74
|
+
// entry.dest
|
|
75
|
+
if (needHandleEntry && !fs.existsSync(entryDest)) {
|
|
70
76
|
console.log(`entry.dest路径不存在: ${entryDest}, 请先创建`);
|
|
71
77
|
return;
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
// 2. 检查项目是否clean
|
|
75
81
|
const assetsGit = simpleGit(assetsDest);
|
|
76
|
-
|
|
82
|
+
let assetsStatus = await assetsGit.status();
|
|
77
83
|
if (!assetsStatus.isClean()) {
|
|
78
84
|
console.log(`${assetsDest}目前有未提交内容, 请先处理`);
|
|
79
85
|
return;
|
|
80
86
|
}
|
|
81
87
|
|
|
82
88
|
let entryGit;
|
|
83
|
-
|
|
89
|
+
let entryStatus;
|
|
90
|
+
if (needHandleEntry) {
|
|
84
91
|
entryGit = simpleGit(entryDest);
|
|
85
|
-
|
|
92
|
+
entryStatus = await entryGit.status();
|
|
86
93
|
if (!entryStatus.isClean()) {
|
|
87
94
|
console.log(`${entryDest}目前有未提交内容, 请先处理`);
|
|
88
95
|
return;
|
|
@@ -90,14 +97,14 @@ async function main() {
|
|
|
90
97
|
}
|
|
91
98
|
|
|
92
99
|
// 2.1如果是prod模式,检查当前项目是不是处于master分支
|
|
93
|
-
if(mode === 'prod'){
|
|
100
|
+
if (mode === 'prod') {
|
|
94
101
|
const currentGit = simpleGit(workDir);
|
|
95
102
|
const currentStatus = await currentGit.status();
|
|
96
|
-
if(!currentStatus.isClean()){
|
|
103
|
+
if (!currentStatus.isClean()) {
|
|
97
104
|
console.log(`当前项目有未提交内容, 请先处理`);
|
|
98
105
|
process.exit(1);
|
|
99
106
|
}
|
|
100
|
-
if(currentStatus.branch !== "master"){
|
|
107
|
+
if (currentStatus.branch !== "master") {
|
|
101
108
|
console.log(`当前项目不是master分支, 请切换至master分支`);
|
|
102
109
|
process.exit(1);
|
|
103
110
|
}
|
|
@@ -109,10 +116,10 @@ async function main() {
|
|
|
109
116
|
await assetsGit.checkout("master");
|
|
110
117
|
console.log(`${assetsDest}切换至master分支成功`);
|
|
111
118
|
}
|
|
112
|
-
if (
|
|
113
|
-
console.log(`${entryDest}
|
|
114
|
-
await entryGit.checkout(
|
|
115
|
-
console.log(`${entryDest}
|
|
119
|
+
if (needHandleEntry && entryStatus.current !== entryTargetBranch) {
|
|
120
|
+
console.log(`${entryDest}当前分支不是${entryTargetBranch}, 切换至${entryTargetBranch}分支`);
|
|
121
|
+
await entryGit.checkout(entryTargetBranch);
|
|
122
|
+
console.log(`${entryDest}切换至${entryTargetBranch}分支成功`);
|
|
116
123
|
}
|
|
117
124
|
|
|
118
125
|
// 4. 执行git pull
|
|
@@ -140,9 +147,9 @@ async function main() {
|
|
|
140
147
|
console.log(`assets copy: ${srcPath} -> ${destPath}`);
|
|
141
148
|
assetsFilesCount++;
|
|
142
149
|
}
|
|
143
|
-
console.log(`assets copy done.`);
|
|
150
|
+
// console.log(`assets copy done.`);
|
|
144
151
|
// 5.2 复制entry
|
|
145
|
-
if (
|
|
152
|
+
if (needHandleEntry) {
|
|
146
153
|
const entryFiles = config.entry.files;
|
|
147
154
|
for (const [src, dest] of Object.entries(entryFiles)) {
|
|
148
155
|
const srcPath = join(workDir, src);
|
|
@@ -158,33 +165,54 @@ async function main() {
|
|
|
158
165
|
fs.copyFileSync(srcPath, destPath);
|
|
159
166
|
console.log(`entry copy: ${srcPath} -> ${destPath}`);
|
|
160
167
|
}
|
|
161
|
-
console.log(`entry copy done.`);
|
|
168
|
+
// console.log(`entry copy done.`);
|
|
162
169
|
}
|
|
163
170
|
|
|
164
171
|
// 6. 提交
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
await assetsGit.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
await
|
|
171
|
-
const
|
|
172
|
-
await
|
|
173
|
-
console.log(`
|
|
172
|
+
let canPushAssets = false;
|
|
173
|
+
let canPushEntry = false;
|
|
174
|
+
assetsStatus = await assetsGit.status();
|
|
175
|
+
if (!assetsStatus.isClean()) {
|
|
176
|
+
canPushAssets = true;
|
|
177
|
+
await assetsGit.add(".");
|
|
178
|
+
const assetsCommit = `${config.assets.commit || "feat:部署资源文件"} by deploy-helper`;
|
|
179
|
+
const assetsCommitResult = await assetsGit.commit(assetsCommit);
|
|
180
|
+
console.log(`assets commit: ${assetsCommit}`);
|
|
181
|
+
console.log(`assets commit: ${JSON.stringify(assetsCommitResult.summary)}`);
|
|
182
|
+
} else {
|
|
183
|
+
console.log(`${assetsDest} 未发现修改内容,跳过提交`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (needHandleEntry) {
|
|
187
|
+
entryStatus = await entryGit.status();
|
|
188
|
+
if (!entryStatus.isClean()) {
|
|
189
|
+
canPushEntry = true;
|
|
190
|
+
await entryGit.add(".");
|
|
191
|
+
const entryCommit = `${config.entry.commit || "feat:部署入口文件"} by deploy-helper`;
|
|
192
|
+
const entryCommitResult = await entryGit.commit(entryCommit);
|
|
193
|
+
console.log(`entry commit: ${entryCommit}`);
|
|
194
|
+
console.log(`entry commit: ${JSON.stringify(entryCommitResult.summary)}`);
|
|
195
|
+
} else {
|
|
196
|
+
console.log(`${entryDest} 未发现修改内容,跳过提交`);
|
|
197
|
+
}
|
|
174
198
|
}
|
|
175
199
|
|
|
176
200
|
// 7. 推送
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
201
|
+
if (canPushAssets) {
|
|
202
|
+
const assetsPushResult = await assetsGit.push();
|
|
203
|
+
console.log(`assets push done.`);
|
|
204
|
+
// console.log(`assets push: ${JSON.stringify([...assetsPushResult.remoteMessages.all()])}`);
|
|
205
|
+
}
|
|
206
|
+
if (canPushEntry) {
|
|
207
|
+
const entryPushResult = await entryGit.push();
|
|
181
208
|
console.log(`entry push done.`);
|
|
209
|
+
// console.log(`entry push: ${JSON.stringify([...entryPushResult.remoteMessages.all()])}`);
|
|
182
210
|
}
|
|
183
211
|
|
|
184
212
|
try {
|
|
185
213
|
// 8 测试环境将entry scp至测试环境
|
|
186
|
-
if (mode === 'test' && config.
|
|
187
|
-
const syncTestFiles = config.
|
|
214
|
+
if (mode === 'test' && config.testSync) {
|
|
215
|
+
const syncTestFiles = config.testSync;
|
|
188
216
|
// 不能放密码。。残念
|
|
189
217
|
const scpClientConfig = {
|
|
190
218
|
host: '192.168.0.35',
|
|
@@ -206,7 +234,7 @@ async function main() {
|
|
|
206
234
|
} catch (error) {
|
|
207
235
|
console.log(`scp error: ${error}`);
|
|
208
236
|
console.log(`复制文件到测试环境服务器失败, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
|
|
209
|
-
if(assetsFilesCount > 0){
|
|
237
|
+
if (assetsFilesCount > 0) {
|
|
210
238
|
console.log(`不过请放心,构建产物的assets已经处理完成,只要手动处理index.html文件即可。`);
|
|
211
239
|
}
|
|
212
240
|
process.exit(1);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taole/deploy-helper",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -8,14 +8,16 @@
|
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
9
|
},
|
|
10
10
|
"bin": {
|
|
11
|
-
"deploy-helper": "index.
|
|
12
|
-
"dh": "index.
|
|
11
|
+
"deploy-helper": "index.mjs",
|
|
12
|
+
"dh": "index.mjs"
|
|
13
13
|
},
|
|
14
14
|
"publishConfig": {
|
|
15
15
|
"access": "public",
|
|
16
16
|
"registry": "https://registry.npmjs.org/"
|
|
17
17
|
},
|
|
18
|
-
"files": [
|
|
18
|
+
"files": [
|
|
19
|
+
"index.js"
|
|
20
|
+
],
|
|
19
21
|
"keywords": [],
|
|
20
22
|
"author": "",
|
|
21
23
|
"license": "ISC",
|