@taole/deploy-helper 1.0.0-beta.6 → 1.0.0
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.mjs +387 -34
- package/lib/login.mjs +167 -0
- package/lib/offlinePkg.mjs +314 -0
- package/lib/pipelineApi.mjs +1 -2
- package/lib/project.mjs +346 -0
- package/lib/upload.js +49 -0
- package/lib/util.mjs +85 -0
- package/modules/alibabacloud-devops-mcp-server/dist/operations/flow/pipeline.js +8 -1
- package/package.json +10 -5
- package/deploy.mjs +0 -203
- package/deploy2.mjs +0 -275
- package/util.mjs +0 -22
package/index.mjs
CHANGED
|
@@ -1,39 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import { Client } from 'node-scp'
|
|
3
4
|
import fs from 'fs';
|
|
4
5
|
import { join, basename, dirname } from "path";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
6
|
+
import { simpleGit } from 'simple-git';
|
|
7
|
+
import { homedir } from 'os'
|
|
8
|
+
import { runPipeline, checkYunxiaoToken, triggerPipeline } from './lib/pipelineApi.mjs';
|
|
9
|
+
import { setDebug, log, getUserDeployHelperConfig } from './lib/util.mjs';
|
|
10
|
+
import path from 'path';
|
|
7
11
|
import { fileURLToPath } from 'url';
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
+
import { checkOfflinePkg, syncApi as syncOfflinePkgApi } from './lib/offlinePkg.mjs';
|
|
13
|
+
import { cmdWhoami, cmdLogout, cmdLogin } from './lib/login.mjs';
|
|
14
|
+
import { cmdProjectCreate, cmdProjectPublish, cmdProjectPull } from './lib/project.mjs';
|
|
12
15
|
|
|
13
16
|
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
-
const __dirname = dirname(__filename);
|
|
17
|
+
const __dirname = path.dirname(__filename);
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const TEST_SERVER_HOST = "192.168.0.35";
|
|
15
21
|
|
|
22
|
+
function fmtAssetsCommit(msg) {
|
|
23
|
+
if (msg.startsWith("-#")) {
|
|
24
|
+
return msg;
|
|
25
|
+
}
|
|
26
|
+
return `-#DH-1 ${msg}`;
|
|
27
|
+
}
|
|
16
28
|
|
|
17
29
|
/**
|
|
18
30
|
* 加载配置
|
|
19
31
|
* @returns 配置对象
|
|
20
32
|
*/
|
|
21
|
-
async function loadConfig() {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (fs.existsSync(configJsonPath)) {
|
|
25
|
-
configJson = JSON.parse(fs.readFileSync(configJsonPath, "utf-8"));
|
|
26
|
-
}
|
|
27
|
-
if(!configJson){
|
|
28
|
-
log(`deploy.config.json不存在, 请先创建`);
|
|
29
|
-
return null;
|
|
33
|
+
async function loadConfig(configFileName) {
|
|
34
|
+
if(!configFileName){
|
|
35
|
+
configFileName = 'deploy.config.json';
|
|
30
36
|
}
|
|
31
|
-
const
|
|
32
|
-
if (fs.existsSync(
|
|
33
|
-
const
|
|
34
|
-
configJson
|
|
37
|
+
const configJsonPath = join(process.cwd(), configFileName);
|
|
38
|
+
if (fs.existsSync(configJsonPath)) {
|
|
39
|
+
const configJson = JSON.parse(fs.readFileSync(configJsonPath, "utf-8"));
|
|
40
|
+
return configJson;
|
|
35
41
|
}
|
|
36
|
-
|
|
42
|
+
log(`文件${configFileName}不存在, 请先创建`);
|
|
43
|
+
return null;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
46
|
async function initConfigJson() {
|
|
@@ -78,6 +85,28 @@ async function initConfigJson() {
|
|
|
78
85
|
process.exit(0);
|
|
79
86
|
}
|
|
80
87
|
|
|
88
|
+
function hasChangeMe(content) {
|
|
89
|
+
return content.includes("CHANGE_ME");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function getScpClient() {
|
|
93
|
+
let scpClient = null;
|
|
94
|
+
// 不能放密码。。残念
|
|
95
|
+
const scpClientConfig = {
|
|
96
|
+
host: TEST_SERVER_HOST,
|
|
97
|
+
username: 'root',
|
|
98
|
+
tryKeyboard: false,
|
|
99
|
+
password: 'tuwan123!@#',
|
|
100
|
+
}
|
|
101
|
+
// const sshKeyPath = join(homedir(), ".ssh/id_rsa");
|
|
102
|
+
// if (fs.existsSync(sshKeyPath)) {
|
|
103
|
+
// scpClientConfig.privateKey = fs.readFileSync(sshKeyPath, "utf-8");
|
|
104
|
+
// } else {
|
|
105
|
+
// log(`${sshKeyPath}不存在, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
|
|
106
|
+
// }
|
|
107
|
+
scpClient = await Client(scpClientConfig);
|
|
108
|
+
return scpClient;
|
|
109
|
+
}
|
|
81
110
|
|
|
82
111
|
async function getVersion() {
|
|
83
112
|
const packageJsonPath = join(__dirname, "package.json");
|
|
@@ -85,50 +114,95 @@ async function getVersion() {
|
|
|
85
114
|
return packageJson.version;
|
|
86
115
|
}
|
|
87
116
|
|
|
88
|
-
|
|
89
|
-
function
|
|
90
|
-
if(
|
|
91
|
-
return
|
|
117
|
+
const commandMap = {};
|
|
118
|
+
function registerCommand(command, handler, help) {
|
|
119
|
+
if(!command || !handler){
|
|
120
|
+
return;
|
|
92
121
|
}
|
|
93
|
-
|
|
122
|
+
commandMap[command] = {
|
|
123
|
+
handler,
|
|
124
|
+
help,
|
|
125
|
+
};
|
|
94
126
|
}
|
|
95
127
|
|
|
128
|
+
// 注册命令
|
|
129
|
+
function doRegisterCommands() {
|
|
130
|
+
registerCommand("create", cmdProjectCreate, "创建项目");
|
|
131
|
+
registerCommand("publish", cmdProjectPublish, "更新项目到H5平台");
|
|
132
|
+
registerCommand("pull", cmdProjectPull, "拉取已有项目到本地");
|
|
133
|
+
registerCommand("whoami", cmdWhoami, "查看当前登录用户信息");
|
|
134
|
+
registerCommand("logout", cmdLogout, "退出登录");
|
|
135
|
+
registerCommand("login", cmdLogin, "登录");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
doRegisterCommands();
|
|
139
|
+
|
|
96
140
|
async function main() {
|
|
97
141
|
const version = await getVersion();
|
|
98
142
|
|
|
99
143
|
// process.argv.includes("--debug") ||
|
|
100
144
|
// 默认开启debug debug目前只是打印日志的时候额外打印时间
|
|
101
145
|
setDebug(true);
|
|
146
|
+
log(`deploy-helper v${version}`);
|
|
102
147
|
|
|
103
148
|
|
|
149
|
+
// await devTest();
|
|
104
150
|
// log(`process.argv: ${process.argv[2]} ${process.argv[3]} ${process.argv[4]}`);
|
|
105
151
|
|
|
106
152
|
// return;
|
|
107
153
|
// other commands
|
|
108
154
|
let command = process.argv[2];
|
|
109
|
-
|
|
155
|
+
const registeredCommand = commandMap[command];
|
|
156
|
+
if(registeredCommand){
|
|
157
|
+
// pass
|
|
158
|
+
} else if (!["init", "prod", "test", "scp", "scpevt", "pipeline", "offlinepkgrm"].includes(command)) {
|
|
110
159
|
command = "help";
|
|
111
160
|
}
|
|
112
161
|
|
|
162
|
+
if(registeredCommand){
|
|
163
|
+
const handler = registeredCommand.handler;
|
|
164
|
+
try {
|
|
165
|
+
await Promise.resolve(handler());
|
|
166
|
+
process.exit(0);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
log(`执行命令失败: ${error}`);
|
|
169
|
+
console.log(error.stack);
|
|
170
|
+
process.exit(1);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
113
174
|
if (command === "help") {
|
|
114
175
|
console.log(`deploy-helper v${version}`);
|
|
115
176
|
console.log(`usage: deploy-helper [command]`);
|
|
116
|
-
console.log(`command: init 初始化配置`);
|
|
117
|
-
console.log(`command: prod 生产环境部署`);
|
|
118
|
-
console.log(`command: test 测试环境部署`);
|
|
177
|
+
console.log(`command: init [-c {configFileName}] 初始化配置`);
|
|
178
|
+
console.log(`command: prod [-c {configFileName}] 生产环境部署`);
|
|
179
|
+
console.log(`command: test [-c {configFileName}] 测试环境部署`);
|
|
119
180
|
console.log(`command: scp {file} 复制文件{file}到OfficialSite测试服务器{file}`);
|
|
120
181
|
console.log(`command: scp {file} {dest} 复制文件{file}到OfficialSite测试服务器{dest}`);
|
|
121
182
|
console.log(`command: scpevt {file} 复制文件{file}到events测试服务器{file}`);
|
|
122
183
|
console.log(`command: scpevt {file} {dest} 复制文件{file}到events测试服务器{dest}`);
|
|
123
184
|
console.log(`command: pipeline {pipelineName|pipelineId} [branch] 触发流水线{pipelineName|pipelineId}, 指定分支(可省略)`);
|
|
185
|
+
console.log(`command: offlinepkgrm {name} {platform} [mode] 删除离线包{name}, 指定平台{platform}, 指定模式(可省略,默认test)`);
|
|
186
|
+
Object.keys(commandMap).forEach(command => {
|
|
187
|
+
const cmdObj = commandMap[command];
|
|
188
|
+
console.log(`command: ${command} ${cmdObj.help}`);
|
|
189
|
+
})
|
|
124
190
|
process.exit(0);
|
|
125
191
|
}
|
|
126
|
-
log(`deploy-helper v${version} start`);
|
|
127
192
|
|
|
128
193
|
// 其他命令
|
|
129
194
|
if (command === "init") {
|
|
130
195
|
await initConfigJson();
|
|
131
196
|
process.exit(0);
|
|
197
|
+
} else if (command === "whoami") {
|
|
198
|
+
await cmdWhoami();
|
|
199
|
+
process.exit(0);
|
|
200
|
+
} else if (command === "logout") {
|
|
201
|
+
await cmdLogout();
|
|
202
|
+
process.exit(0);
|
|
203
|
+
} else if (command === "login") {
|
|
204
|
+
await cmdLogin();
|
|
205
|
+
process.exit(0);
|
|
132
206
|
} else if (["scp", "scpevt"].includes(command)) {
|
|
133
207
|
const file = process.argv[3];
|
|
134
208
|
if (!file) {
|
|
@@ -182,17 +256,296 @@ async function main() {
|
|
|
182
256
|
process.exit(1);
|
|
183
257
|
}
|
|
184
258
|
|
|
259
|
+
} else if (command === "offlinepkgrm") {
|
|
260
|
+
const name = process.argv[3];
|
|
261
|
+
if (!name) {
|
|
262
|
+
log(`name参数不能为空`);
|
|
263
|
+
process.exit(1);
|
|
264
|
+
}
|
|
265
|
+
const platform = process.argv[4];
|
|
266
|
+
if (!platform) {
|
|
267
|
+
log(`platform参数不能为空`);
|
|
268
|
+
process.exit(1);
|
|
269
|
+
}
|
|
270
|
+
const mode = process.argv[5] || "test";
|
|
271
|
+
if (!["prod", "test"].includes(mode)) {
|
|
272
|
+
log(`mode参数只能是prod或test`);
|
|
273
|
+
process.exit(1);
|
|
274
|
+
}
|
|
275
|
+
const userDeployHelperConfig = getUserDeployHelperConfig();
|
|
276
|
+
if (!userDeployHelperConfig || !userDeployHelperConfig.offlineApi || !userDeployHelperConfig.offlineApi.get || !userDeployHelperConfig.offlineApi.set) {
|
|
277
|
+
log(`配置文件未配置离线包api接口, 请先配置`);
|
|
278
|
+
process.exit(1);
|
|
279
|
+
}
|
|
280
|
+
await syncOfflinePkgApi(userDeployHelperConfig, mode, { name, remove: true, platform }, null);
|
|
281
|
+
process.exit(0);
|
|
185
282
|
}
|
|
186
283
|
try {
|
|
187
284
|
const workDir = process.cwd(); // 当前项目根目录
|
|
188
285
|
const mode = process.argv[2] === 'prod' ? 'prod' : 'test'; // 部署环境
|
|
189
286
|
log(`deploy mode: ${mode}`);
|
|
190
287
|
log(`workDir: ${workDir}`);
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
288
|
+
let configFileName = '';
|
|
289
|
+
if(process.argv[3] === '-c'){
|
|
290
|
+
// 尝试从-c中获取配置文件名
|
|
291
|
+
configFileName = process.argv[4];
|
|
292
|
+
if(!configFileName){
|
|
293
|
+
log(`-c参数必须指定配置文件名`);
|
|
294
|
+
process.exit(1);
|
|
295
|
+
} else if(configFileName){
|
|
296
|
+
log(`将使用配置文件: ${configFileName}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// 读取配置
|
|
300
|
+
const config = await loadConfig(configFileName);
|
|
301
|
+
if(!config){
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
// log(`config`, config);
|
|
305
|
+
|
|
306
|
+
const result = hasChangeMe(JSON.stringify(config));
|
|
307
|
+
if (result) {
|
|
308
|
+
log(`配置中存在默认值(CHANGE_ME), 请先修改配置`);
|
|
309
|
+
process.exit(1);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
const needHandleAssets = config.assets && config.assets.dest;
|
|
314
|
+
|
|
315
|
+
// 需要处理entry
|
|
316
|
+
const needHandleEntry = config.entry && config.entry.dest && (mode === 'prod' || (mode === 'test' && !config.entry.onlyProd));
|
|
317
|
+
const entryTestBranch = (config.entry && config.entry.testBranch) || "test";
|
|
318
|
+
const entryProdBranch = (config.entry && config.entry.prodBranch) || "master";
|
|
319
|
+
const entryTargetBranch = mode === 'prod' ? entryProdBranch : entryTestBranch;
|
|
320
|
+
// const currentProdBranch = config.prodBranch || "master";
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
// 检查流水线配置
|
|
324
|
+
checkYunxiaoToken(config, mode);
|
|
325
|
+
const offlinePkgResult = checkOfflinePkg({
|
|
326
|
+
workDir: workDir,
|
|
327
|
+
mode,
|
|
328
|
+
});
|
|
329
|
+
if (!offlinePkgResult.canBuild && offlinePkgResult.errorMsg) {
|
|
330
|
+
log(`${offlinePkgResult.errorMsg}`);
|
|
331
|
+
process.exit(1);
|
|
332
|
+
}
|
|
333
|
+
if (offlinePkgResult.canBuild) {
|
|
334
|
+
await offlinePkgResult.hookPostBuild();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// 检查项目
|
|
338
|
+
// 1. 检查项目是否存在
|
|
339
|
+
let assetsDest = null;
|
|
340
|
+
let entryDest = null;
|
|
341
|
+
if (needHandleAssets) {
|
|
342
|
+
assetsDest = join(workDir, config.assets.dest);
|
|
343
|
+
if (!fs.existsSync(assetsDest)) {
|
|
344
|
+
log(`assets.dest路径不存在: ${assetsDest}, 请先创建`);
|
|
345
|
+
process.exit(1);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
// entry.dest
|
|
349
|
+
if (needHandleEntry) {
|
|
350
|
+
entryDest = join(workDir, config.entry.dest);
|
|
351
|
+
if (!fs.existsSync(entryDest)) {
|
|
352
|
+
log(`entry.dest路径不存在: ${entryDest}, 请先创建`);
|
|
353
|
+
process.exit(1);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// 2. 检查项目是否clean
|
|
358
|
+
let assetsGit = null;
|
|
359
|
+
let assetsStatus = null;
|
|
360
|
+
if (needHandleAssets) {
|
|
361
|
+
assetsGit = simpleGit(assetsDest);
|
|
362
|
+
assetsStatus = await assetsGit.status();
|
|
363
|
+
if (!assetsStatus.isClean()) {
|
|
364
|
+
log(`${assetsDest}目前有未提交内容, 请先处理`);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
let entryGit;
|
|
370
|
+
let entryStatus;
|
|
371
|
+
if (needHandleEntry) {
|
|
372
|
+
entryGit = simpleGit(entryDest);
|
|
373
|
+
entryStatus = await entryGit.status();
|
|
374
|
+
if (!entryStatus.isClean()) {
|
|
375
|
+
log(`${entryDest}目前有未提交内容, 请先处理`);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// 2.1如果是prod模式,检查当前项目是不是处于主分支
|
|
381
|
+
// if (mode === 'prod') {
|
|
382
|
+
// const currentGit = simpleGit(workDir);
|
|
383
|
+
// const currentStatus = await currentGit.status();
|
|
384
|
+
// if (!currentStatus.isClean()) {
|
|
385
|
+
// log(`当前项目有未提交内容, 请先处理`);
|
|
386
|
+
// process.exit(1);
|
|
387
|
+
// }
|
|
388
|
+
// if (currentStatus.branch !== currentProdBranch) {
|
|
389
|
+
// log(`当前项目分支${currentStatus.branch}不是${currentProdBranch}分支, 请切换至${currentProdBranch}分支再执行prod命令`);
|
|
390
|
+
// process.exit(1);
|
|
391
|
+
// }
|
|
392
|
+
// }
|
|
393
|
+
|
|
394
|
+
// 3. 检查assets项目分支,并切换至master分支
|
|
395
|
+
// assets项目固定使用master分支
|
|
396
|
+
if (needHandleAssets && assetsStatus.current !== "master") {
|
|
397
|
+
log(`${assetsDest}当前分支不是master, 切换至master分支`);
|
|
398
|
+
await assetsGit.checkout("master");
|
|
399
|
+
log(`${assetsDest}切换至master分支成功`);
|
|
400
|
+
}
|
|
401
|
+
if (needHandleEntry && entryStatus.current !== entryTargetBranch) {
|
|
402
|
+
log(`${entryDest}当前分支不是${entryTargetBranch}, 切换至${entryTargetBranch}分支`);
|
|
403
|
+
await entryGit.checkout(entryTargetBranch);
|
|
404
|
+
log(`${entryDest}切换至${entryTargetBranch}分支成功`);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// 4. 执行git pull
|
|
408
|
+
if (assetsGit) {
|
|
409
|
+
log(`${assetsDest}执行git pull`);
|
|
410
|
+
await assetsGit.pull();
|
|
411
|
+
log(`${assetsDest} git pull done`);
|
|
412
|
+
}
|
|
413
|
+
if (entryGit) {
|
|
414
|
+
log(`${entryDest}执行git pull`);
|
|
415
|
+
await entryGit.pull();
|
|
416
|
+
log(`${entryDest} git pull done`);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
let assetsFilesCount = 0;
|
|
420
|
+
// 5. 复制构建产物
|
|
421
|
+
// 5.1 复制assets
|
|
422
|
+
if (needHandleAssets) {
|
|
423
|
+
const assetsFiles = config.assets.files;
|
|
424
|
+
for (const [src, dest] of Object.entries(assetsFiles)) {
|
|
425
|
+
const srcPath = join(workDir, src);
|
|
426
|
+
if (!fs.existsSync(srcPath)) {
|
|
427
|
+
log(`${srcPath}不存在,请确认构建产物是否正确`);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
const destPath = join(assetsDest, dest);
|
|
431
|
+
if (fs.statSync(srcPath).isDirectory()) {
|
|
432
|
+
fs.cpSync(srcPath, destPath, { recursive: true });
|
|
433
|
+
} else {
|
|
434
|
+
fs.copyFileSync(srcPath, destPath);
|
|
435
|
+
}
|
|
436
|
+
log(`assets copy: ${srcPath} -> ${destPath}`);
|
|
437
|
+
assetsFilesCount++;
|
|
438
|
+
}
|
|
439
|
+
// log(`assets copy done.`);
|
|
440
|
+
}
|
|
441
|
+
// 5.2 复制entry
|
|
442
|
+
if (needHandleEntry) {
|
|
443
|
+
const entryFiles = config.entry.files;
|
|
444
|
+
for (const [src, dest] of Object.entries(entryFiles)) {
|
|
445
|
+
const srcPath = join(workDir, src);
|
|
446
|
+
if (!fs.existsSync(srcPath)) {
|
|
447
|
+
log(`${srcPath}不存在,请确认构建产物是否正确`);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (fs.statSync(srcPath).isDirectory()) {
|
|
451
|
+
log(`entry: ${srcPath}是目录,不支持entry为目录的部署`);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const dests = Array.isArray(dest) ? dest : [dest];
|
|
455
|
+
for (const dest of dests) {
|
|
456
|
+
const destPath = join(entryDest, dest);
|
|
457
|
+
fs.copyFileSync(srcPath, destPath);
|
|
458
|
+
log(`entry copy: ${srcPath} -> ${destPath}`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
// log(`entry copy done.`);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// 6. 提交
|
|
465
|
+
let canPushAssets = false;
|
|
466
|
+
let canPushEntry = false;
|
|
467
|
+
if (needHandleAssets) {
|
|
468
|
+
assetsStatus = await assetsGit.status();
|
|
469
|
+
if (!assetsStatus.isClean()) {
|
|
470
|
+
canPushAssets = true;
|
|
471
|
+
await assetsGit.add(".");
|
|
472
|
+
let assetsCommit = `${config.assets.commit || "feat:部署资源文件"} by deploy-helper`;
|
|
473
|
+
assetsCommit = fmtAssetsCommit(assetsCommit);
|
|
474
|
+
const assetsCommitResult = await assetsGit.commit(assetsCommit);
|
|
475
|
+
log(`assets commit: ${assetsCommit}`);
|
|
476
|
+
log(`assets commit: ${JSON.stringify(assetsCommitResult.summary)}`);
|
|
477
|
+
} else {
|
|
478
|
+
log(`${assetsDest} 未发现修改内容,跳过提交`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (needHandleEntry) {
|
|
482
|
+
entryStatus = await entryGit.status();
|
|
483
|
+
if (!entryStatus.isClean()) {
|
|
484
|
+
canPushEntry = true;
|
|
485
|
+
await entryGit.add(".");
|
|
486
|
+
let entryCommit = `${config.entry.commit || "feat:部署入口文件"} by deploy-helper`;
|
|
487
|
+
entryCommit = fmtAssetsCommit(entryCommit);
|
|
488
|
+
const entryCommitResult = await entryGit.commit(entryCommit);
|
|
489
|
+
log(`entry commit: ${entryCommit}`);
|
|
490
|
+
log(`entry commit: ${JSON.stringify(entryCommitResult.summary)}`);
|
|
491
|
+
} else {
|
|
492
|
+
log(`${entryDest} 未发现修改内容,跳过提交`);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// 7. 推送
|
|
497
|
+
if (canPushAssets) {
|
|
498
|
+
log(`assets push start`);
|
|
499
|
+
try {
|
|
500
|
+
await assetsGit.push();
|
|
501
|
+
} catch (error) {
|
|
502
|
+
await assetsGit.pull();
|
|
503
|
+
await assetsGit.push();
|
|
504
|
+
}
|
|
505
|
+
log(`assets push done.`);
|
|
506
|
+
}
|
|
507
|
+
if (canPushEntry) {
|
|
508
|
+
log(`entry push start`);
|
|
509
|
+
try {
|
|
510
|
+
await entryGit.push();
|
|
511
|
+
} catch (error) {
|
|
512
|
+
await entryGit.pull();
|
|
513
|
+
await entryGit.push();
|
|
514
|
+
}
|
|
515
|
+
log(`entry push done.`);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
try {
|
|
519
|
+
// 8 测试环境将entry scp至测试环境
|
|
520
|
+
if (mode === 'test' && config.testSync) {
|
|
521
|
+
const syncTestFiles = config.testSync;
|
|
522
|
+
const scpClient = await getScpClient();
|
|
523
|
+
for (const [src, dest] of Object.entries(syncTestFiles)) {
|
|
524
|
+
const srcPath = join(workDir, src);
|
|
525
|
+
const dests = Array.isArray(dest) ? dest : [dest];
|
|
526
|
+
for (const dest of dests) {
|
|
527
|
+
const destPath = "/home/web/website/tuwan_www/templets/static/play/" + dest;
|
|
528
|
+
log(`scp: ${srcPath} -> ${TEST_SERVER_HOST}:${destPath}`);
|
|
529
|
+
await scpClient.uploadFile(srcPath, destPath)
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
} catch (error) {
|
|
534
|
+
log(`scp error: ${error}`);
|
|
535
|
+
log(`复制文件到测试环境服务器失败, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
|
|
536
|
+
if (assetsFilesCount > 0) {
|
|
537
|
+
log(`不过请放心,构建产物的assets已经处理完成,只要手动处理index.html文件即可。`);
|
|
538
|
+
}
|
|
539
|
+
process.exit(1);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
// 处理触发流水线的任务
|
|
544
|
+
await runPipeline(config, mode);
|
|
545
|
+
|
|
194
546
|
log(`deploy-helper deploy done.`);
|
|
195
547
|
process.exit(0);
|
|
548
|
+
|
|
196
549
|
} catch (error) {
|
|
197
550
|
log(`error occurred in deploy-helper`, error);
|
|
198
551
|
process.exit(1);
|
package/lib/login.mjs
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { log } from "./util.mjs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import os from "os";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import { exec } from "child_process";
|
|
6
|
+
import { promisify } from "util";
|
|
7
|
+
|
|
8
|
+
const execAsync = promisify(exec);
|
|
9
|
+
const isDev = false;
|
|
10
|
+
/**
|
|
11
|
+
* 保存本地用户登录状态的缓存文件
|
|
12
|
+
*/
|
|
13
|
+
const cacheFilePath = path.join(os.homedir(), ".tw-cli-userinfo-cache.json");
|
|
14
|
+
let host =
|
|
15
|
+
"https://fapi.tuwan.com";
|
|
16
|
+
if (isDev) {
|
|
17
|
+
host = "https://local-h5-plat.tuwan.com";
|
|
18
|
+
}
|
|
19
|
+
const devHost = "http://localhost:9000";
|
|
20
|
+
let CREATE_SESSION_URL = `${host}/api/login/createLoginSession`;
|
|
21
|
+
|
|
22
|
+
export async function getCache() {
|
|
23
|
+
try {
|
|
24
|
+
if (fs.existsSync(cacheFilePath)) {
|
|
25
|
+
const cacheStr = fs.readFileSync(cacheFilePath, "utf8");
|
|
26
|
+
const cache = JSON.parse(cacheStr);
|
|
27
|
+
const Tuwan_Passport = cache.Tuwan_Passport;
|
|
28
|
+
if (Tuwan_Passport) {
|
|
29
|
+
// 检查Tuwan_Passport是否还有效
|
|
30
|
+
const res = await fetch(
|
|
31
|
+
"https://yapi.tuwan.com/User/getUid?format=json",
|
|
32
|
+
{
|
|
33
|
+
headers: {
|
|
34
|
+
Cookie: `Tuwan_Passport=${Tuwan_Passport}`,
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
//log("getCache res", res);
|
|
39
|
+
const uidResp = await res.json();
|
|
40
|
+
//log("getCache uidResp", uidResp);
|
|
41
|
+
if (
|
|
42
|
+
uidResp &&
|
|
43
|
+
uidResp.error === 0 &&
|
|
44
|
+
uidResp.data &&
|
|
45
|
+
uidResp.data.uid === cache.userInfo.uid
|
|
46
|
+
) {
|
|
47
|
+
return cache;
|
|
48
|
+
} else {
|
|
49
|
+
log("验证本地缓存失败, 已清理本地缓存");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
clearCache();
|
|
54
|
+
return null;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error("获取缓存失败:", error);
|
|
57
|
+
clearCache();
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function setCache(data) {
|
|
63
|
+
fs.writeFileSync(cacheFilePath, JSON.stringify(data));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function clearCache() {
|
|
67
|
+
if (fs.existsSync(cacheFilePath)) {
|
|
68
|
+
fs.unlinkSync(cacheFilePath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function openBrowser(url) {
|
|
73
|
+
const platform = process.platform;
|
|
74
|
+
if (platform === "win32") {
|
|
75
|
+
await execAsync(`start "" "${url}"`);
|
|
76
|
+
} else if (platform === "darwin") {
|
|
77
|
+
await execAsync(`open "${url}"`);
|
|
78
|
+
} else {
|
|
79
|
+
await execAsync(`xdg-open "${url}"`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function createLoginSession() {
|
|
84
|
+
let url = CREATE_SESSION_URL;
|
|
85
|
+
if (isDev) {
|
|
86
|
+
url = url.replace(host, devHost);
|
|
87
|
+
}
|
|
88
|
+
const res = await fetch(url, {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: { "Content-Type": "application/json" },
|
|
91
|
+
});
|
|
92
|
+
if (!res.ok) {
|
|
93
|
+
throw new Error(`创建登录会话失败: ${res.status} ${res.statusText}`);
|
|
94
|
+
}
|
|
95
|
+
return res.json();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function queryLoginResult(
|
|
99
|
+
doneUrl,
|
|
100
|
+
intervalMs = 2000,
|
|
101
|
+
timeoutMs = 120000
|
|
102
|
+
) {
|
|
103
|
+
const start = Date.now();
|
|
104
|
+
if (isDev) {
|
|
105
|
+
doneUrl = doneUrl.replace(host, devHost);
|
|
106
|
+
}
|
|
107
|
+
while (Date.now() - start < timeoutMs) {
|
|
108
|
+
const res = await fetch(doneUrl, {
|
|
109
|
+
method: "POST",
|
|
110
|
+
headers: { "Content-Type": "application/json" },
|
|
111
|
+
});
|
|
112
|
+
//log("queryLoginResult res", res);
|
|
113
|
+
if (!res.ok) {
|
|
114
|
+
throw new Error(`轮询登录结果失败: ${res.status} ${res.statusText}`);
|
|
115
|
+
}
|
|
116
|
+
const data = await res.json();
|
|
117
|
+
//log("queryLoginResult data", JSON.stringify(data, null, 2));
|
|
118
|
+
let loginResult = data.loginResult;
|
|
119
|
+
if (loginResult && data.status === "success") {
|
|
120
|
+
return JSON.parse(loginResult);
|
|
121
|
+
}
|
|
122
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
123
|
+
}
|
|
124
|
+
throw new Error("登录超时,请重试");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function cmdLogin() {
|
|
128
|
+
const cache = await getCache();
|
|
129
|
+
if (cache && cache.userInfo && cache.userInfo.uid) {
|
|
130
|
+
log(
|
|
131
|
+
`已登录账号: ${cache.userInfo.nickname}(${cache.userInfo.uid}), 若需登录其他账号请先登出`
|
|
132
|
+
);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
log("正在创建登录会话...");
|
|
136
|
+
const { openUrl, doneUrl } = await createLoginSession();
|
|
137
|
+
log("请在浏览器中完成登录:", openUrl);
|
|
138
|
+
|
|
139
|
+
log("正在打开浏览器...");
|
|
140
|
+
await openBrowser(openUrl);
|
|
141
|
+
|
|
142
|
+
log("等待登录结果...");
|
|
143
|
+
const result = await queryLoginResult(doneUrl);
|
|
144
|
+
log(`${result.userInfo.nickname}(${result.userInfo.uid})登录成功`);
|
|
145
|
+
setCache(result);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function cmdWhoami() {
|
|
149
|
+
const cache = await getCache();
|
|
150
|
+
const userInfo = cache?.userInfo;
|
|
151
|
+
if (!userInfo || !userInfo.uid) {
|
|
152
|
+
log("当前未登录");
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
log(`当前登录账号: ${userInfo.nickname}(${userInfo.uid})`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function cmdLogout() {
|
|
159
|
+
const cache = await getCache();
|
|
160
|
+
const userInfo = cache?.userInfo;
|
|
161
|
+
if (!userInfo || !userInfo.uid) {
|
|
162
|
+
log("当前未登录");
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
clearCache();
|
|
166
|
+
log("已退出登录");
|
|
167
|
+
}
|