|
@@ -1,4 +1,5 @@
|
|
|
import Client from 'ssh2-sftp-client';
|
|
|
+import ssh2 from 'ssh2';
|
|
|
import chalk from 'chalk';
|
|
|
import ora from 'ora';
|
|
|
import shell from 'shelljs';
|
|
@@ -20,10 +21,42 @@ const compileDist = async () => {
|
|
|
|
|
|
// 获取配置
|
|
|
const getConfig = () => {
|
|
|
- const [ result ] = config.filter(({ nodeEnv }) => nodeEnv === filterStage);
|
|
|
+ const [result] = config.filter(({ nodeEnv }) => nodeEnv === filterStage);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+const execRemoteCommand = async () => {
|
|
|
+ const config = getConfig();
|
|
|
+ const conn = new ssh2.Client();
|
|
|
+
|
|
|
+ conn.on("ready", function () {
|
|
|
+ console.log(chalk.blue("ssh2 链接成功"));
|
|
|
+
|
|
|
+ const commands = [
|
|
|
+ 'cd /data/node_web/smartRobot',
|
|
|
+ 'pwd',
|
|
|
+ 'pm2 list',
|
|
|
+ 'pm2 del smartRobot',
|
|
|
+ 'pm2 start'
|
|
|
+ ];
|
|
|
+
|
|
|
+ conn.exec(commands.join(";"), { pty: true }, function (err, stream) {
|
|
|
+ if (err) throw err;
|
|
|
+ stream
|
|
|
+ .on('close', function (code, signal) {
|
|
|
+ console.log('Command 执行成功');
|
|
|
+ conn.end(); // 结束SSH连接
|
|
|
+ })
|
|
|
+ .on('data', function (data) {
|
|
|
+ process.stdout.write(data);
|
|
|
+ });
|
|
|
+
|
|
|
+ stream.stdin.end();
|
|
|
+ });
|
|
|
+ }).connect({ ...config });
|
|
|
+}
|
|
|
+
|
|
|
// 部署
|
|
|
const connectShell = async () => {
|
|
|
const sftp = new Client();
|
|
@@ -35,7 +68,7 @@ const connectShell = async () => {
|
|
|
console.log(chalk.red(`${item.host} --> `) + chalk[color](text));
|
|
|
}
|
|
|
|
|
|
- printMsg({color: 'green', text: '服务器连接中'});
|
|
|
+ printMsg({ color: 'green', text: '服务器连接中' });
|
|
|
|
|
|
sftp.connect({
|
|
|
host: item.host,
|
|
@@ -43,31 +76,36 @@ const connectShell = async () => {
|
|
|
username: item.username,
|
|
|
password: item.password,
|
|
|
})
|
|
|
- .then(() => {
|
|
|
- printMsg({color: 'green', text: '服务器连接成功'});
|
|
|
- printMsg({color: 'yellow', text: '执行删除文件中'});
|
|
|
- return sftp.rmdir(item.path, true);
|
|
|
- })
|
|
|
- .then(() => {
|
|
|
- printMsg({color: 'green', text: '执行删除文件成功'});
|
|
|
- printMsg({color: 'green', text: '即将开始上传'});
|
|
|
- spinner = ora().start();
|
|
|
- spinner.text = '文件上传中,请等待'
|
|
|
- return sftp.uploadDir(path.resolve(__dirname, "../dist"), item.path);
|
|
|
- })
|
|
|
- .then(() => {
|
|
|
- spinner.info('文件上传结束')
|
|
|
- spinner.stop();
|
|
|
- printMsg({color: 'green', text: '上传完成,部署成功'});
|
|
|
- sftp.end();
|
|
|
- })
|
|
|
- .catch((err) => {
|
|
|
- console.error(
|
|
|
- err,
|
|
|
- chalk.red(`${item.host} -->`) + chalk.red(`上传失败`)
|
|
|
- );
|
|
|
- sftp.end();
|
|
|
- });
|
|
|
+ .then(() => {
|
|
|
+ printMsg({ color: 'green', text: '服务器连接成功' });
|
|
|
+ printMsg({ color: 'yellow', text: '执行删除文件中' });
|
|
|
+ return sftp.rmdir(item.path, true);
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ printMsg({ color: 'green', text: '执行删除文件成功' });
|
|
|
+ printMsg({ color: 'green', text: '即将开始上传' });
|
|
|
+ spinner = ora().start();
|
|
|
+ spinner.text = '文件上传中,请等待'
|
|
|
+ return sftp.uploadDir(path.resolve(__dirname, "../dist"), item.path);
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ spinner.info('文件上传结束')
|
|
|
+ spinner.stop();
|
|
|
+
|
|
|
+ // if (filterStage === 'prod') {
|
|
|
+ execRemoteCommand()
|
|
|
+ // }
|
|
|
+
|
|
|
+ printMsg({ color: 'green', text: '上传完成,准备指令中' });
|
|
|
+ sftp.end();
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ console.error(
|
|
|
+ err,
|
|
|
+ chalk.red(`${item.host} -->`) + chalk.red(`上传失败`)
|
|
|
+ );
|
|
|
+ sftp.end();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
async function runStart() {
|