1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| import chalk from 'chalk' import ora from 'ora' import scpClient from 'scp2' import compressing from 'compressing' import { Client } from 'ssh2' import minimist from 'minimist' import fs from 'fs'
const projName = minimist(process.argv.slice(2))['proj'] if (!projName) throw new Error("缺少传入参数") const data = JSON.parse(fs.readFileSync('../servers.json')) let serverPath = data['serWebPath'][projName] if (!serverPath) throw new Error("当前项目未配置serWebPath") serverPath = data['serWebPath']['base'] + serverPath
let curServer = data['servers']['[SERVERNAME]'] curServer['pKey'] = fs.readFileSync(curServer['pKey'])
const uploading = ora(`正在发布到${curServer['ip']}服务器`) const ziping = ora(`正在压缩${projName}……`)
const conn = new Client()
ziping.start() if (fs.existsSync('./dist.zip')) fs.unlink('./dist.zip')
compressing.zip.compressDir('./dist', 'dist.zip') .then(e => { ziping.stop() console.log(chalk.green('压缩成功!')) spinner.start() scpClient.scp('dist.zip', { host: curServer['ip'], port: curServer['port'], username: curServer['user'], path: serverPath, privateKey: curServer['pKey'] }, err => { spinner.stop() if (err) { console.log(chalk.red('发布失败!')) throw err } conn.on('ready', () => { conn.exec(`unzip -o ${serverPath}/dist.zip -d ${serverPath}/ && mv ${serverPath}/dist/* ${serverPath}/ && rm -rf ${serverPath}/dist*`, (err, stream) => { if (err) throw err stream.on('close', (code, signal) => { console.log('chalk.green(`成功发布到${curServer['ip']}服务器`)') conn.close() }) .on('data', data => console.log('STDOUT:' + data)) .stderr.on('data', data => console.log('STDERR:' + data))
} ) }).connect({ host: curServer['ip'], port: curServer['port'], username: curServer['user'], privateKey: curServer['pKey'] }) } ) }) .catch(err => console.log(chalk.red('Fail!压缩失败!')))
|