vue + scp部署服务器

npm i scp2 --save

scp.js 放在根目录下


const scp = require('scp2')
// 导入fs包
const fs = require("fs");
// 导入readline包
let readline = require('readline');
// 实例化接口对象
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
// 封装的写入
function fsWrite(path,content){
    return new Promise (function (resolve,reject) {
        //  查找是否有文件 ,有则插入内容,没有就创建文件
        fs.readFile(path, "utf8", (err, data) => {
            if(!err){
                console.log("日志写入");
                fs.writeFile(path,','+content,{flag: 'a',encoding: 'utf-8'},function (err) {
                    if(err){
                        console.log("日志失败");
                        reject(err);
                    }else{
                        console.log("日志成功");
                        resolve(err);
                    }
                });
            }else{
                fs.appendFile(path, content, function (err) { 
                    if (err) throw err; 
                    console.log("日志写入");
                    fs.writeFile(path,','+content,{flag: 'a',encoding: 'utf-8'},function (err) {
                        if(err){
                            console.log("日志失败");
                            reject(err);
                        }else{
                            console.log("日志成功");
                            resolve(err);
                        }
                    });
                }); 
            }
         });
        
    });
}

// 封装的提问事件
function htQuestion (question) {
    return new Promise(function (resolve,reject){
        rl.question(question, function (answer){
            resolve(answer);
        });
    });
}

// close事件监听
rl.on('close',function () {
    // 结束程序
    process.exit(0);
});
// 创建package.json文件的方法
async function createPackage () {
    let host = await htQuestion("host name: "); // 服务器的地址
    let username = await htQuestion("user name: "); // 用户名
    let author = await htQuestion("author: "); //密码
    let localFile = await htQuestion("localFile: ") // 本地更新文件夹
    let filePath = await htQuestion("filePath: ") //服务器存放文件路径
    let content = {
        host: host,   	  
        port: 22,             
        username: username ,     
        password: author,	 
        path: filePath
    };
    upDate(localFile,content)
}
function upDate(localFile,data){
    console.log(`部署中........`)
    scp.scp(localFile, data, err => {
        let title = ''
        if (!err) {
            title = `部署完成!`
            console.log(title)
        } else {
            console.log('部署失败.........,请重新输入')
            title = err.level+(`部署失败`)
            console.log(title)
        }
        let obj = {
            title,
            data
        }
        gofsWrite(JSON.stringify(obj) )
    })
}
async function gofsWrite(obj){
   await fsWrite('updatelog.log',obj);
   rl.close();
}
createPackage();

package.json 加入此代码

"inputDeploy": "cross-env NODE_ENV=production vue-cli-service build &&node ./scp.js"

npm run inputDeploy
对应服务器地址,账号密码等信息
对应服务器地址,账号密码等信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值