@taole/deploy-helper 0.5.4 → 1.0.0-beta.1

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/deploy.mjs ADDED
@@ -0,0 +1,201 @@
1
+ import { hasChangeMe } from './util.mjs';
2
+ import { runPipeline, checkYunxiaoToken } from './lib/pipelineApi.mjs';
3
+ import { log } from './lib/util.mjs';
4
+ import { simpleGit } from 'simple-git';
5
+ import { getScpClient, TEST_SERVER_HOST } from './util.mjs';
6
+
7
+
8
+ export default class Deployer {
9
+ async deploy(workDir, mode, config) {
10
+ // 检查配置中是否存在默认值(CHANGE_ME)
11
+ const result = hasChangeMe(JSON.stringify(config));
12
+ if (result) {
13
+ throw new Error(`配置中存在默认值(CHANGE_ME), 请先修改配置`);
14
+ }
15
+
16
+ const needHandleAssets = config.assets && config.assets.dest;
17
+ const needHandleEntry = config.entry && config.entry.dest && (mode === 'prod' || (mode === 'test' && !config.entry.onlyProd));
18
+ const entryTestBranch = (config.entry && config.entry.testBranch) || "test";
19
+ const entryProdBranch = (config.entry && config.entry.prodBranch) || "master";
20
+ const entryTargetBranch = mode === 'prod' ? entryProdBranch : entryTestBranch;
21
+
22
+ // 检查流水线配置
23
+ checkYunxiaoToken(config, mode);
24
+
25
+
26
+ // 检查项目
27
+ // 1. 检查项目是否存在
28
+ let assetsDest = null;
29
+ let entryDest = null;
30
+ if (needHandleAssets) {
31
+ assetsDest = join(workDir, config.assets.dest);
32
+ if (!fs.existsSync(assetsDest)) {
33
+ throw new Error(`assets.dest路径不存在: ${assetsDest}, 请先创建`);
34
+ }
35
+ }
36
+ // entry.dest
37
+ if (needHandleEntry) {
38
+ entryDest = join(workDir, config.entry.dest);
39
+ if (!fs.existsSync(entryDest)) {
40
+ throw new Error(`entry.dest路径不存在: ${entryDest}, 请先创建`);
41
+ }
42
+ }
43
+
44
+ // 2. 检查项目是否clean
45
+ let assetsGit = null;
46
+ let assetsStatus = null;
47
+ if (needHandleAssets) {
48
+ assetsGit = simpleGit(assetsDest);
49
+ assetsStatus = await assetsGit.status();
50
+ if (!assetsStatus.isClean()) {
51
+ throw new Error(`${assetsDest}目前有未提交内容, 请先处理`);
52
+ }
53
+ }
54
+
55
+ let entryGit;
56
+ let entryStatus;
57
+ if (needHandleEntry) {
58
+ entryGit = simpleGit(entryDest);
59
+ entryStatus = await entryGit.status();
60
+ if (!entryStatus.isClean()) {
61
+ throw new Error(`${entryDest}目前有未提交内容, 请先处理`);
62
+ }
63
+ }
64
+
65
+
66
+ // 3. 检查assets项目分支,并切换至master分支
67
+ // assets项目固定使用master分支
68
+ if (needHandleAssets && assetsStatus.current !== "master") {
69
+ log(`${assetsDest}当前分支不是master, 切换至master分支`);
70
+ await assetsGit.checkout("master");
71
+ log(`${assetsDest}切换至master分支成功`);
72
+ }
73
+ if (needHandleEntry && entryStatus.current !== entryTargetBranch) {
74
+ log(`${entryDest}当前分支不是${entryTargetBranch}, 切换至${entryTargetBranch}分支`);
75
+ await entryGit.checkout(entryTargetBranch);
76
+ log(`${entryDest}切换至${entryTargetBranch}分支成功`);
77
+ }
78
+
79
+ // 4. 执行git pull
80
+ if (assetsGit) {
81
+ log(`${assetsDest}执行git pull`);
82
+ await assetsGit.pull();
83
+ log(`${assetsDest} git pull done`);
84
+ }
85
+ if (entryGit) {
86
+ log(`${entryDest}执行git pull`);
87
+ await entryGit.pull();
88
+ log(`${entryDest} git pull done`);
89
+ }
90
+
91
+ // 5. 复制构建产物
92
+ // 5.1 复制assets
93
+ if (needHandleAssets) {
94
+ let assetsFilesCount = 0;
95
+ const assetsFiles = config.assets.files;
96
+ for (const [src, dest] of Object.entries(assetsFiles)) {
97
+ const srcPath = join(workDir, src);
98
+ if (!fs.existsSync(srcPath)) {
99
+ throw new Error(`${srcPath}不存在,请确认构建产物是否正确`);
100
+ }
101
+ const destPath = join(assetsDest, dest);
102
+ if (fs.statSync(srcPath).isDirectory()) {
103
+ fs.cpSync(srcPath, destPath, { recursive: true });
104
+ } else {
105
+ fs.copyFileSync(srcPath, destPath);
106
+ }
107
+ log(`assets copy: ${srcPath} -> ${destPath}`);
108
+ assetsFilesCount++;
109
+ }
110
+ // log(`assets copy done.`);
111
+ }
112
+ // 5.2 复制entry
113
+ if (needHandleEntry) {
114
+ const entryFiles = config.entry.files;
115
+ for (const [src, dest] of Object.entries(entryFiles)) {
116
+ const srcPath = join(workDir, src);
117
+ if (!fs.existsSync(srcPath)) {
118
+ throw new Error(`${srcPath}不存在,请确认构建产物是否正确`);
119
+ }
120
+ if (fs.statSync(srcPath).isDirectory()) {
121
+ throw new Error(`entry: ${srcPath}是目录,不支持entry为目录的部署`);
122
+ }
123
+ const destPath = join(entryDest, dest);
124
+ fs.copyFileSync(srcPath, destPath);
125
+ log(`entry copy: ${srcPath} -> ${destPath}`);
126
+ }
127
+ // log(`entry copy done.`);
128
+ }
129
+
130
+ // 6. 提交
131
+ let canPushAssets = false;
132
+ let canPushEntry = false;
133
+ if (needHandleAssets) {
134
+ assetsStatus = await assetsGit.status();
135
+ if (!assetsStatus.isClean()) {
136
+ canPushAssets = true;
137
+ await assetsGit.add(".");
138
+ const assetsCommit = `${config.assets.commit || "feat:部署资源文件"} by deploy-helper`;
139
+ const assetsCommitResult = await assetsGit.commit(assetsCommit);
140
+ log(`assets commit: ${assetsCommit}`);
141
+ log(`assets commit: ${JSON.stringify(assetsCommitResult.summary)}`);
142
+ } else {
143
+ log(`${assetsDest} 未发现修改内容,跳过提交`);
144
+ }
145
+ }
146
+ if (needHandleEntry) {
147
+ entryStatus = await entryGit.status();
148
+ if (!entryStatus.isClean()) {
149
+ canPushEntry = true;
150
+ await entryGit.add(".");
151
+ const entryCommit = `${config.entry.commit || "feat:部署入口文件"} by deploy-helper`;
152
+ const entryCommitResult = await entryGit.commit(entryCommit);
153
+ log(`entry commit: ${entryCommit}`);
154
+ log(`entry commit: ${JSON.stringify(entryCommitResult.summary)}`);
155
+ } else {
156
+ log(`${entryDest} 未发现修改内容,跳过提交`);
157
+ }
158
+ }
159
+
160
+ // 7. 推送
161
+ if (canPushAssets) {
162
+ log(`assets push start`);
163
+ try {
164
+ await assetsGit.push();
165
+ } catch (error) {
166
+ await assetsGit.push();
167
+ }
168
+ log(`assets push done.`);
169
+ }
170
+ if (canPushEntry) {
171
+ log(`entry push start`);
172
+ try {
173
+ await entryGit.push();
174
+ } catch (error) {
175
+ await entryGit.push();
176
+ }
177
+ log(`entry push done.`);
178
+ }
179
+
180
+ try {
181
+ // 8 测试环境将entry scp至测试环境
182
+ if (mode === 'test' && config.testSync) {
183
+ const syncTestFiles = config.testSync;
184
+ const scpClient = await getScpClient();
185
+ for (const [src, dest] of Object.entries(syncTestFiles)) {
186
+ const srcPath = join(workDir, src);
187
+ const destPath = "/home/web/website/tuwan_www/templets/static/play/" + dest;
188
+ log(`scp: ${srcPath} -> ${TEST_SERVER_HOST}:${destPath}`);
189
+ await scpClient.uploadFile(srcPath, destPath)
190
+ }
191
+ }
192
+ } catch (error) {
193
+ log(`scp error: ${error}`);
194
+ if (assetsFilesCount > 0) {
195
+ log(`不过请放心,构建产物的assets已经处理完成,只要手动处理index.html文件即可。`);
196
+ }
197
+ }
198
+ // 处理触发流水线的任务
199
+ await runPipeline(config, mode);
200
+ }
201
+ }
package/deploy2.mjs ADDED
@@ -0,0 +1,6 @@
1
+ export default class Deployer2 {
2
+ async deploy(workDir, mode, config) {
3
+ // check
4
+ // run
5
+ }
6
+ }
package/index.mjs CHANGED
@@ -1,20 +1,19 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { Client } from 'node-scp'
4
3
  import fs from 'fs';
5
4
  import { join, basename, dirname } from "path";
6
- import { simpleGit } from 'simple-git';
7
- import { homedir } from 'os'
8
5
  import { runPipeline, checkYunxiaoToken, triggerPipeline } from './lib/pipelineApi.mjs';
9
6
  import { setDebug, log } from './lib/util.mjs';
10
7
  import path from 'path';
11
8
  import { fileURLToPath } from 'url';
9
+ import Deployer from './deploy.mjs';
10
+ import { getScpClient, TEST_SERVER_HOST } from './util.mjs';
11
+
12
12
 
13
13
  const __filename = fileURLToPath(import.meta.url);
14
14
  const __dirname = path.dirname(__filename);
15
15
 
16
16
 
17
- const TEST_SERVER_HOST = "192.168.0.35";
18
17
  /**
19
18
  * 加载配置
20
19
  * @returns 配置对象
@@ -71,28 +70,6 @@ async function initConfigJson() {
71
70
  process.exit(0);
72
71
  }
73
72
 
74
- function hasChangeMe(content) {
75
- return content.includes("CHANGE_ME");
76
- }
77
-
78
- async function getScpClient() {
79
- let scpClient = null;
80
- // 不能放密码。。残念
81
- const scpClientConfig = {
82
- host: TEST_SERVER_HOST,
83
- username: 'root',
84
- tryKeyboard: false,
85
- password: 'tuwan123!@#',
86
- }
87
- // const sshKeyPath = join(homedir(), ".ssh/id_rsa");
88
- // if (fs.existsSync(sshKeyPath)) {
89
- // scpClientConfig.privateKey = fs.readFileSync(sshKeyPath, "utf-8");
90
- // } else {
91
- // log(`${sshKeyPath}不存在, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
92
- // }
93
- scpClient = await Client(scpClientConfig);
94
- return scpClient;
95
- }
96
73
 
97
74
  async function getVersion() {
98
75
  const packageJsonPath = join(__dirname, "package.json");
@@ -101,6 +78,10 @@ async function getVersion() {
101
78
  }
102
79
 
103
80
 
81
+ function getDeployer() {
82
+ return new Deployer();
83
+ }
84
+
104
85
  async function main() {
105
86
  const version = await getVersion();
106
87
 
@@ -197,234 +178,12 @@ async function main() {
197
178
  const mode = process.argv[2] === 'prod' ? 'prod' : 'test'; // 部署环境
198
179
  log(`deploy mode: ${mode}`);
199
180
  log(`workDir: ${workDir}`);
181
+ const deployer = getDeployer();
200
182
 
201
- // 读取配置
202
183
  const config = await loadConfig();
203
- // log(`config`, config);
204
-
205
- const result = hasChangeMe(JSON.stringify(config));
206
- if (result) {
207
- log(`配置中存在默认值(CHANGE_ME), 请先修改配置`);
208
- process.exit(1);
209
- }
210
-
211
-
212
- const needHandleAssets = config.assets && config.assets.dest;
213
-
214
- // 需要处理entry
215
- const needHandleEntry = config.entry && config.entry.dest && (mode === 'prod' || (mode === 'test' && !config.entry.onlyProd));
216
- const entryTestBranch = (config.entry && config.entry.testBranch) || "test";
217
- const entryProdBranch = (config.entry && config.entry.prodBranch) || "master";
218
- const entryTargetBranch = mode === 'prod' ? entryProdBranch : entryTestBranch;
219
- // const currentProdBranch = config.prodBranch || "master";
220
-
221
-
222
- // 检查流水线配置
223
- checkYunxiaoToken(config, mode);
224
-
225
-
226
- // 检查项目
227
- // 1. 检查项目是否存在
228
- let assetsDest = null;
229
- let entryDest = null;
230
- if (needHandleAssets) {
231
- assetsDest = join(workDir, config.assets.dest);
232
- if (!fs.existsSync(assetsDest)) {
233
- log(`assets.dest路径不存在: ${assetsDest}, 请先创建`);
234
- process.exit(1);
235
- }
236
- }
237
- // entry.dest
238
- if (needHandleEntry) {
239
- entryDest = join(workDir, config.entry.dest);
240
- if (!fs.existsSync(entryDest)) {
241
- log(`entry.dest路径不存在: ${entryDest}, 请先创建`);
242
- process.exit(1);
243
- }
244
- }
245
-
246
- // 2. 检查项目是否clean
247
- let assetsGit = null;
248
- let assetsStatus = null;
249
- if (needHandleAssets) {
250
- assetsGit = simpleGit(assetsDest);
251
- assetsStatus = await assetsGit.status();
252
- if (!assetsStatus.isClean()) {
253
- log(`${assetsDest}目前有未提交内容, 请先处理`);
254
- return;
255
- }
256
- }
257
-
258
- let entryGit;
259
- let entryStatus;
260
- if (needHandleEntry) {
261
- entryGit = simpleGit(entryDest);
262
- entryStatus = await entryGit.status();
263
- if (!entryStatus.isClean()) {
264
- log(`${entryDest}目前有未提交内容, 请先处理`);
265
- return;
266
- }
267
- }
268
-
269
- // 2.1如果是prod模式,检查当前项目是不是处于主分支
270
- // if (mode === 'prod') {
271
- // const currentGit = simpleGit(workDir);
272
- // const currentStatus = await currentGit.status();
273
- // if (!currentStatus.isClean()) {
274
- // log(`当前项目有未提交内容, 请先处理`);
275
- // process.exit(1);
276
- // }
277
- // if (currentStatus.branch !== currentProdBranch) {
278
- // log(`当前项目分支${currentStatus.branch}不是${currentProdBranch}分支, 请切换至${currentProdBranch}分支再执行prod命令`);
279
- // process.exit(1);
280
- // }
281
- // }
282
-
283
- // 3. 检查assets项目分支,并切换至master分支
284
- // assets项目固定使用master分支
285
- if (needHandleAssets && assetsStatus.current !== "master") {
286
- log(`${assetsDest}当前分支不是master, 切换至master分支`);
287
- await assetsGit.checkout("master");
288
- log(`${assetsDest}切换至master分支成功`);
289
- }
290
- if (needHandleEntry && entryStatus.current !== entryTargetBranch) {
291
- log(`${entryDest}当前分支不是${entryTargetBranch}, 切换至${entryTargetBranch}分支`);
292
- await entryGit.checkout(entryTargetBranch);
293
- log(`${entryDest}切换至${entryTargetBranch}分支成功`);
294
- }
295
-
296
- // 4. 执行git pull
297
- if (assetsGit) {
298
- log(`${assetsDest}执行git pull`);
299
- await assetsGit.pull();
300
- log(`${assetsDest} git pull done`);
301
- }
302
- if (entryGit) {
303
- log(`${entryDest}执行git pull`);
304
- await entryGit.pull();
305
- log(`${entryDest} git pull done`);
306
- }
307
-
308
- // 5. 复制构建产物
309
- // 5.1 复制assets
310
- if (needHandleAssets) {
311
- let assetsFilesCount = 0;
312
- const assetsFiles = config.assets.files;
313
- for (const [src, dest] of Object.entries(assetsFiles)) {
314
- const srcPath = join(workDir, src);
315
- if (!fs.existsSync(srcPath)) {
316
- log(`${srcPath}不存在,请确认构建产物是否正确`);
317
- return;
318
- }
319
- const destPath = join(assetsDest, dest);
320
- if (fs.statSync(srcPath).isDirectory()) {
321
- fs.cpSync(srcPath, destPath, { recursive: true });
322
- } else {
323
- fs.copyFileSync(srcPath, destPath);
324
- }
325
- log(`assets copy: ${srcPath} -> ${destPath}`);
326
- assetsFilesCount++;
327
- }
328
- // log(`assets copy done.`);
329
- }
330
- // 5.2 复制entry
331
- if (needHandleEntry) {
332
- const entryFiles = config.entry.files;
333
- for (const [src, dest] of Object.entries(entryFiles)) {
334
- const srcPath = join(workDir, src);
335
- if (!fs.existsSync(srcPath)) {
336
- log(`${srcPath}不存在,请确认构建产物是否正确`);
337
- return;
338
- }
339
- if (fs.statSync(srcPath).isDirectory()) {
340
- log(`entry: ${srcPath}是目录,不支持entry为目录的部署`);
341
- return;
342
- }
343
- const destPath = join(entryDest, dest);
344
- fs.copyFileSync(srcPath, destPath);
345
- log(`entry copy: ${srcPath} -> ${destPath}`);
346
- }
347
- // log(`entry copy done.`);
348
- }
349
-
350
- // 6. 提交
351
- let canPushAssets = false;
352
- let canPushEntry = false;
353
- if (needHandleAssets) {
354
- assetsStatus = await assetsGit.status();
355
- if (!assetsStatus.isClean()) {
356
- canPushAssets = true;
357
- await assetsGit.add(".");
358
- const assetsCommit = `${config.assets.commit || "feat:部署资源文件"} by deploy-helper`;
359
- const assetsCommitResult = await assetsGit.commit(assetsCommit);
360
- log(`assets commit: ${assetsCommit}`);
361
- log(`assets commit: ${JSON.stringify(assetsCommitResult.summary)}`);
362
- } else {
363
- log(`${assetsDest} 未发现修改内容,跳过提交`);
364
- }
365
- }
366
- if (needHandleEntry) {
367
- entryStatus = await entryGit.status();
368
- if (!entryStatus.isClean()) {
369
- canPushEntry = true;
370
- await entryGit.add(".");
371
- const entryCommit = `${config.entry.commit || "feat:部署入口文件"} by deploy-helper`;
372
- const entryCommitResult = await entryGit.commit(entryCommit);
373
- log(`entry commit: ${entryCommit}`);
374
- log(`entry commit: ${JSON.stringify(entryCommitResult.summary)}`);
375
- } else {
376
- log(`${entryDest} 未发现修改内容,跳过提交`);
377
- }
378
- }
379
-
380
- // 7. 推送
381
- if (canPushAssets) {
382
- log(`assets push start`);
383
- try {
384
- await assetsGit.push();
385
- } catch (error) {
386
- await assetsGit.push();
387
- }
388
- log(`assets push done.`);
389
- }
390
- if (canPushEntry) {
391
- log(`entry push start`);
392
- try {
393
- await entryGit.push();
394
- } catch (error) {
395
- await entryGit.push();
396
- }
397
- log(`entry push done.`);
398
- }
399
-
400
- try {
401
- // 8 测试环境将entry scp至测试环境
402
- if (mode === 'test' && config.testSync) {
403
- const syncTestFiles = config.testSync;
404
- const scpClient = await getScpClient();
405
- for (const [src, dest] of Object.entries(syncTestFiles)) {
406
- const srcPath = join(workDir, src);
407
- const destPath = "/home/web/website/tuwan_www/templets/static/play/" + dest;
408
- log(`scp: ${srcPath} -> ${TEST_SERVER_HOST}:${destPath}`);
409
- await scpClient.uploadFile(srcPath, destPath)
410
- }
411
- }
412
- } catch (error) {
413
- log(`scp error: ${error}`);
414
- log(`复制文件到测试环境服务器失败, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
415
- if (assetsFilesCount > 0) {
416
- log(`不过请放心,构建产物的assets已经处理完成,只要手动处理index.html文件即可。`);
417
- }
418
- process.exit(1);
419
- }
420
-
421
-
422
- // 处理触发流水线的任务
423
- await runPipeline(config, mode);
424
-
184
+ await deployer.deploy(workDir, mode, config);
425
185
  log(`deploy-helper deploy done.`);
426
186
  process.exit(0);
427
-
428
187
  } catch (error) {
429
188
  log(`error occurred in deploy-helper`, error);
430
189
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "0.5.4",
3
+ "version": "1.0.0-beta.1",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",
@@ -17,6 +17,9 @@
17
17
  },
18
18
  "files": [
19
19
  "index.mjs",
20
+ "deploy.mjs",
21
+ "deploy2.mjs",
22
+ "util.mjs",
20
23
  "lib",
21
24
  "modules/alibabacloud-devops-mcp-server/dist"
22
25
  ],
package/util.mjs ADDED
@@ -0,0 +1,22 @@
1
+ import { Client } from 'node-scp'
2
+
3
+
4
+ export function hasChangeMe(content) {
5
+ return content.includes("CHANGE_ME");
6
+ }
7
+
8
+ export const TEST_SERVER_HOST = "192.168.0.35";
9
+
10
+
11
+
12
+ export async function getScpClient() {
13
+ let scpClient = null;
14
+ const scpClientConfig = {
15
+ host: TEST_SERVER_HOST,
16
+ username: 'root',
17
+ tryKeyboard: false,
18
+ password: 'tuwan123!@#', // u cant see me
19
+ }
20
+ scpClient = await Client(scpClientConfig);
21
+ return scpClient;
22
+ }