@noahyu/cd-cli 1.3.0 → 1.3.2
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/dist/src/index.js +54 -13
- package/package.json +1 -1
package/dist/src/index.js
CHANGED
|
@@ -90,16 +90,25 @@ async function deploy(config, version) {
|
|
|
90
90
|
const versionPath = join(config.server.deployPath, versionDirName);
|
|
91
91
|
const currentLinkPath = join(config.server.deployPath, buildDirName);
|
|
92
92
|
spinner.start('正在准备部署目录...');
|
|
93
|
-
await ssh.execCommand(`mkdir -p ${config.server.deployPath}`);
|
|
94
|
-
|
|
93
|
+
const mkDeployResult = await ssh.execCommand(`mkdir -p ${config.server.deployPath}`);
|
|
94
|
+
if (mkDeployResult.code !== 0) {
|
|
95
|
+
throw new Error(`创建部署目录失败: ${mkDeployResult.stderr}`);
|
|
96
|
+
}
|
|
97
|
+
const mkVersionResult = await ssh.execCommand(`mkdir -p ${versionPath}`);
|
|
98
|
+
if (mkVersionResult.code !== 0) {
|
|
99
|
+
throw new Error(`创建版本目录失败: ${mkVersionResult.stderr}`);
|
|
100
|
+
}
|
|
95
101
|
spinner.succeed('部署目录准备完成');
|
|
96
102
|
spinner.start(`正在上传文件到版本目录 ${versionDirName}...`);
|
|
97
103
|
const remoteZipPath = join(versionPath, 'build.zip');
|
|
98
104
|
await ssh.putFile(zipPath, remoteZipPath);
|
|
99
105
|
spinner.succeed('文件上传完成');
|
|
100
106
|
spinner.start('正在解压文件...');
|
|
101
|
-
await ssh.execCommand(`cd ${versionPath} && unzip -o build.zip && rm build.zip`);
|
|
102
|
-
|
|
107
|
+
const unzipResult = await ssh.execCommand(`cd ${versionPath} && unzip -o build.zip && rm build.zip`);
|
|
108
|
+
if (unzipResult.code !== 0) {
|
|
109
|
+
throw new Error(`解压文件失败: ${unzipResult.stderr || unzipResult.stdout}`);
|
|
110
|
+
}
|
|
111
|
+
const mvResult = await ssh.execCommand(`
|
|
103
112
|
cd ${versionPath} &&
|
|
104
113
|
if [ -d "${buildDirName}" ]; then
|
|
105
114
|
mv ${buildDirName}/* . 2>/dev/null || true
|
|
@@ -107,6 +116,9 @@ async function deploy(config, version) {
|
|
|
107
116
|
rmdir ${buildDirName} 2>/dev/null || true
|
|
108
117
|
fi
|
|
109
118
|
`);
|
|
119
|
+
if (mvResult.code !== 0) {
|
|
120
|
+
throw new Error(`移动构建目录内容失败: ${mvResult.stderr}`);
|
|
121
|
+
}
|
|
110
122
|
spinner.succeed('文件解压完成');
|
|
111
123
|
if (config.files && config.files.length > 0) {
|
|
112
124
|
spinner.start('正在部署额外文件...');
|
|
@@ -119,17 +131,34 @@ async function deploy(config, version) {
|
|
|
119
131
|
spinner.start('正在部署额外文件...');
|
|
120
132
|
continue;
|
|
121
133
|
}
|
|
122
|
-
await ssh.execCommand(`mkdir -p ${join(config.server.deployPath, file, '..')}`);
|
|
123
|
-
|
|
134
|
+
const mkdirResult = await ssh.execCommand(`mkdir -p ${join(config.server.deployPath, file, '..')}`);
|
|
135
|
+
if (mkdirResult.code !== 0) {
|
|
136
|
+
throw new Error(`创建目录失败: ${mkdirResult.stderr}`);
|
|
137
|
+
}
|
|
138
|
+
const cpResult = await ssh.execCommand(`cp -f ${remoteVersionFilePath} ${remoteRootFilePath}`);
|
|
139
|
+
if (cpResult.code !== 0) {
|
|
140
|
+
throw new Error(`复制额外文件失败 (${file}): ${cpResult.stderr}`);
|
|
141
|
+
}
|
|
124
142
|
}
|
|
125
143
|
spinner.succeed(`额外文件部署完成 (${config.files.length} 个)`);
|
|
126
144
|
}
|
|
127
145
|
if (config.afterDeploy) {
|
|
128
|
-
spinner.start('执行部署后命令...');
|
|
129
146
|
for (const cmd of config.afterDeploy) {
|
|
130
|
-
|
|
147
|
+
spinner.start(`执行部署后命令: ${cmd}`);
|
|
148
|
+
const result = await ssh.execCommand(cmd, { cwd: config.server.deployPath });
|
|
149
|
+
if (result.stdout) {
|
|
150
|
+
spinner.stop();
|
|
151
|
+
console.log(result.stdout);
|
|
152
|
+
}
|
|
153
|
+
if (result.stderr) {
|
|
154
|
+
spinner.stop();
|
|
155
|
+
console.error(chalk.yellow(result.stderr));
|
|
156
|
+
}
|
|
157
|
+
if (result.code !== 0) {
|
|
158
|
+
throw new Error(`部署后命令执行失败 (exit ${result.code}): ${cmd}`);
|
|
159
|
+
}
|
|
160
|
+
spinner.succeed(`命令执行完成: ${cmd}`);
|
|
131
161
|
}
|
|
132
|
-
spinner.succeed('部署后命令执行完成');
|
|
133
162
|
}
|
|
134
163
|
spinner.start(`正在切换到新版本 ${version}...`);
|
|
135
164
|
const tempLinkPath = `${currentLinkPath}.tmp.${Date.now()}`;
|
|
@@ -221,7 +250,10 @@ async function cleanOldVersions(ssh, deployPath, buildDirName, keepCount) {
|
|
|
221
250
|
if (versionDirs.length > keepCount) {
|
|
222
251
|
const dirsToDelete = versionDirs.slice(0, -keepCount);
|
|
223
252
|
for (const dir of dirsToDelete) {
|
|
224
|
-
await ssh.execCommand(`rm -rf "${dir}"`);
|
|
253
|
+
const rmResult = await ssh.execCommand(`rm -rf "${dir}"`);
|
|
254
|
+
if (rmResult.code !== 0) {
|
|
255
|
+
console.warn(chalk.yellow(`⚠️ 删除旧版本失败 (${dir}): ${rmResult.stderr}`));
|
|
256
|
+
}
|
|
225
257
|
}
|
|
226
258
|
}
|
|
227
259
|
}
|
|
@@ -458,8 +490,14 @@ async function performRollback(config, targetVersion, buildDirName) {
|
|
|
458
490
|
spinner.start('正在还原额外文件...');
|
|
459
491
|
continue;
|
|
460
492
|
}
|
|
461
|
-
await ssh.execCommand(`mkdir -p ${join(config.server.deployPath, file, '..')}`);
|
|
462
|
-
|
|
493
|
+
const mkdirResult = await ssh.execCommand(`mkdir -p ${join(config.server.deployPath, file, '..')}`);
|
|
494
|
+
if (mkdirResult.code !== 0) {
|
|
495
|
+
throw new Error(`创建目录失败: ${mkdirResult.stderr}`);
|
|
496
|
+
}
|
|
497
|
+
const cpResult = await ssh.execCommand(`cp -f ${remoteVersionFilePath} ${remoteRootFilePath}`);
|
|
498
|
+
if (cpResult.code !== 0) {
|
|
499
|
+
throw new Error(`还原额外文件失败 (${file}): ${cpResult.stderr}`);
|
|
500
|
+
}
|
|
463
501
|
}
|
|
464
502
|
spinner.succeed('额外文件还原完成');
|
|
465
503
|
}
|
|
@@ -589,7 +627,10 @@ async function createSSHConnection(server) {
|
|
|
589
627
|
}
|
|
590
628
|
async function cleanTempLinks(ssh, deployPath, buildDirName) {
|
|
591
629
|
try {
|
|
592
|
-
await ssh.execCommand(`find ${deployPath} -name "${buildDirName}.tmp.*" -type l -delete`);
|
|
630
|
+
const result = await ssh.execCommand(`find ${deployPath} -name "${buildDirName}.tmp.*" -type l -delete`);
|
|
631
|
+
if (result.code !== 0) {
|
|
632
|
+
console.warn(chalk.yellow(`⚠️ 清理临时链接时出现警告: ${result.stderr}`));
|
|
633
|
+
}
|
|
593
634
|
}
|
|
594
635
|
catch (error) {
|
|
595
636
|
console.warn(chalk.yellow(`⚠️ 清理临时链接时出现警告: ${error}`));
|