不多说,直接上代码了
config.js文件
module.exports = {
sit: {
remotePath: '',//上传的远程服务器的目录
username: '',//用户名
password: '',//密码
host: '0.0.0.0',//远程主机
port: 0//服务器端口号
}
}
deploy.js文件
const Client = require('ssh2').Client
const spawn = require("cross-spawn")
const path = require('path')
const ora = require('ora');
const glob = require("glob")
const env = process.argv[process.argv.length - 1].replace('--', '')
let config = require('./config.js')[env];
if (!config) {
console.log(`找不到${env}环境用户配置文件`)
return
}
const cwd = path.dirname(__dirname)
const spinner = ora('开始打包...').start();
const remotePath = config.remotePath
console.log(`构建命令:build${env}`)
const build = spawn('yarn', [`build${env}`], {
cwd: cwd
})
build.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
build.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
build.on('error', function(err) {
console.log('error:', err)
handleError(err, spinner)
})
build.on('close', function(code) {
if (code !== 0) {
console.log('打包失败', code)
spinner.stop()
return
}
spinner.text = "打包完成,开始上传..."
const conn = new Client();
conn.on('ready', function() {
glob('**/*.*', {
cwd: path.join(cwd, 'dist')
}, function(err, files) {
if (err) {
handleError(err, spinner)
return
}
let length = files.length,
index = 0
conn.sftp((err, sftp) => {
if (err) {
handleError(err, spinner)
return
}
spinner.stop()
const upload = () => {
const currentFile = files[index++]
if (!currentFile || index > length) {
console.log('上传完毕')
conn.end();
return;
}
sftp.fastPut(path.join(cwd, 'dist', currentFile), path.join(remotePath, currentFile).replace(/\\/g, '/'), function(err, result) {
if (err) {
console.log(err, '出错了')
return
}
console.log(`${currentFile}上传成功`)
upload()
});
}
upload()
})
})
}).connect({
host: config.host,
port: config.port,
username: config.username,
password: config.password
});
})
const handleError = (err, spinner) => {
console.log(err)
spinner.stop()
}