@noahyu/cd-cli 1.3.1 → 1.3.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.
Files changed (2) hide show
  1. package/dist/src/index.js +56 -13
  2. 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
- await ssh.execCommand(`mkdir -p ${versionPath}`);
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
- await ssh.execCommand(`
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,36 @@ 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
- await ssh.execCommand(`cp -f ${remoteVersionFilePath} ${remoteRootFilePath}`);
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
- await ssh.execCommand(cmd, { cwd: config.server.deployPath });
147
+ spinner.start(`执行部署后命令: ${cmd}`);
148
+ const result = await ssh.execCommand(`bash --login -c ${JSON.stringify(cmd)}`, {
149
+ cwd: config.server.deployPath,
150
+ });
151
+ if (result.stdout) {
152
+ spinner.stop();
153
+ console.log(result.stdout);
154
+ }
155
+ if (result.stderr) {
156
+ spinner.stop();
157
+ console.error(chalk.yellow(result.stderr));
158
+ }
159
+ if (result.code !== 0) {
160
+ throw new Error(`部署后命令执行失败 (exit ${result.code}): ${cmd}`);
161
+ }
162
+ spinner.succeed(`命令执行完成: ${cmd}`);
131
163
  }
132
- spinner.succeed('部署后命令执行完成');
133
164
  }
134
165
  spinner.start(`正在切换到新版本 ${version}...`);
135
166
  const tempLinkPath = `${currentLinkPath}.tmp.${Date.now()}`;
@@ -221,7 +252,10 @@ async function cleanOldVersions(ssh, deployPath, buildDirName, keepCount) {
221
252
  if (versionDirs.length > keepCount) {
222
253
  const dirsToDelete = versionDirs.slice(0, -keepCount);
223
254
  for (const dir of dirsToDelete) {
224
- await ssh.execCommand(`rm -rf "${dir}"`);
255
+ const rmResult = await ssh.execCommand(`rm -rf "${dir}"`);
256
+ if (rmResult.code !== 0) {
257
+ console.warn(chalk.yellow(`⚠️ 删除旧版本失败 (${dir}): ${rmResult.stderr}`));
258
+ }
225
259
  }
226
260
  }
227
261
  }
@@ -458,8 +492,14 @@ async function performRollback(config, targetVersion, buildDirName) {
458
492
  spinner.start('正在还原额外文件...');
459
493
  continue;
460
494
  }
461
- await ssh.execCommand(`mkdir -p ${join(config.server.deployPath, file, '..')}`);
462
- await ssh.execCommand(`cp -f ${remoteVersionFilePath} ${remoteRootFilePath}`);
495
+ const mkdirResult = await ssh.execCommand(`mkdir -p ${join(config.server.deployPath, file, '..')}`);
496
+ if (mkdirResult.code !== 0) {
497
+ throw new Error(`创建目录失败: ${mkdirResult.stderr}`);
498
+ }
499
+ const cpResult = await ssh.execCommand(`cp -f ${remoteVersionFilePath} ${remoteRootFilePath}`);
500
+ if (cpResult.code !== 0) {
501
+ throw new Error(`还原额外文件失败 (${file}): ${cpResult.stderr}`);
502
+ }
463
503
  }
464
504
  spinner.succeed('额外文件还原完成');
465
505
  }
@@ -589,7 +629,10 @@ async function createSSHConnection(server) {
589
629
  }
590
630
  async function cleanTempLinks(ssh, deployPath, buildDirName) {
591
631
  try {
592
- await ssh.execCommand(`find ${deployPath} -name "${buildDirName}.tmp.*" -type l -delete`);
632
+ const result = await ssh.execCommand(`find ${deployPath} -name "${buildDirName}.tmp.*" -type l -delete`);
633
+ if (result.code !== 0) {
634
+ console.warn(chalk.yellow(`⚠️ 清理临时链接时出现警告: ${result.stderr}`));
635
+ }
593
636
  }
594
637
  catch (error) {
595
638
  console.warn(chalk.yellow(`⚠️ 清理临时链接时出现警告: ${error}`));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noahyu/cd-cli",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Global CLI tool for simple project deployment with version management",
5
5
  "type": "module",
6
6
  "main": "./dist/bin/cli.js",