/* eslint-disable / /*
- 上传工具
- eg: node ./deploy.js
*/
var client = require(‘scp2’) var path = require(‘path’) var chalk = require(‘chalk’) var codePath = process.cwd() var http = require(“http”); const ora = require(‘ora’) // 配置信息从nacos配置中心获取 // configUrl = { // “host”:“10.206.30.180”, // “user”:“appdeploy”, // “password”:“BrysjHhrhl!@12”, // “path”:"/home/appdeploy/atao_project/nginx/dist" // } var configUrl = “http://10.206.28.104:8848/nacos/v1/cs/configs?dataId=platform-web&group=iqa”
// 上传前需先执行build操作 var distPath = path.resolve(codePath, ‘./dist/’)
var getConfig = () => { return new Promise((resolve, reject) => { http.get(configUrl, function (data) { var str = “”; data.on(“data”, function (chunk) { str += chunk;//监听数据响应,拼接数据片段 }) data.on(“end”, function () { // console.log(str.toString()) var res = JSON.parse(str.toString()) var conf = { host: res.host, username: res.user, password: res.password, path: res.path }; resolve(conf) })
})
}) }
var makeUploadConf = () => { return new Promise((resolve, reject) => { getConfig() .then((conf) => { // console.log(conf) resolve(conf) })
}) }
var upload = function (config) { const spinner = ora(chalk.red(‘开始上传…’)) spinner.start() client.scp( distPath, config, err => { if (!err) { spinner.succeed(chalk.green(‘上传完成\n’)) console.log( chalk.yellow( ’ upload file to \n\n ’ + ’ ’ + config.host + ‘\n\n’ + ’ ’ + config.path + ‘\n\n’ + ’ ’ + ‘操作时间:’ + new Date().toLocaleString() + ’ \n\n success===’ ) ) } else { console.log(chalk.red(err)) spinner.fail(‘上传失败\n’) } } ) }
var run = function () {
makeUploadConf() .then((conf) => { // console.log(‘upload conf’, conf) upload(conf) }) .catch(() => { console.log(chalk.red(’\n[deployer] —上传失败—\n’)) }) }
run()
|