jjz-deploy
Version:
基于NodeJS的前端部署工具,将前端打包好的目录进行压缩、上传到服务器、备份并且解压到服务器目录
64 lines (63 loc) • 2.06 kB
JavaScript
const path = require('path');
const inquirer = require('inquirer');
const log = require('../util/log.js');
const { formatDate } = require('../util/date.js');
const { NodeSSH } = require('node-ssh');
const ssh = new NodeSSH;
module.exports = function upload (destZipPath) {
const questions = [{
type: 'input',
message: '服务器主机地址:',
name: 'host'
}, {
type: 'input',
message: '服务器用户名:',
name: 'username'
}, {
type: 'password',
message: '服务器密码:',
name: 'password'
}, {
type: 'input',
message: '服务器上传目录:',
name: 'uploadDir',
default: '/home/www'
}];
inquirer.prompt(questions).then(res => {
const { host, username, password, uploadDir } = res;
log.info('连接ssh服务器中...');
ssh.connect({
host,
username,
password,
port: 22
}).then(() => {
log.success('连接成功');
const filename = path.basename(destZipPath, '.zip');
const formatDateStr = formatDate(new Date, 'yyyy-MM-dd__HHmmss')
ssh
.putFile(destZipPath, path.join(uploadDir, filename + '.' + formatDateStr + '.zip'))
.then(() => {
log.success(`${ destZipPath }上传成功`);
const execCommand = `
cp -r ${ filename + '.' + formatDateStr + '.zip' } ${ filename + '.' + formatDateStr + '__bak.zip' } &&
unzip ${ filename + '.' + formatDateStr + '.zip' }
`;
ssh.execCommand(execCommand, { cwd: uploadDir }).then(res => {
if (!res.stderr) {
log.success(`部署完成, 请在${ uploadDir }查看`);
} else {
log.error('部署失败: ', res);
}
process.exit(0);
})
})
.catch(err => {
log.error(`${ destZipPath }上传失败:` + err.message);
});
}).catch((err) => {
log.error('连接失败: ' + err.message);
process.exit(0);
});
});
}