brand-code

前言

  好久没写文章了,一直在忙,刚好今天有点时间,就想写点东西。废话不多说,直接进入正文吧。

准备工作

  需要用到scp2,这是基于ssh2实现的ftp上传工具。npm安装到开发依赖即可,也可全局安装。

npm install -D scp2

具体文档 https://www.npmjs.com/package/scp2

本人的是angular项目开发的,其他框架也都一样。项目根目录新建文件publish.test.js,

 

代码如下:

"use strict"
const client = require(\'scp2\');
const ssh2Cliend = require(\'ssh2\').Client;
const conn = new ssh2Cliend();
const ora = require(\'ora\');
const chalk = require(\'chalk\');
const spinner = ora(chalk.green(\'正在发布到本地虚拟机服务器……\'));

// 服务器配置
const serveConfig = {
    host: \'ip/域名\',
    port: \'22\',  //端口默认22
    username: \'root\',
    password: \'password\',
    path: \'/mydir/website/\'
};
async function backup() {
    conn.exec(`mv -fu ${serveConfig.path} ${serveConfig.path}.backup`, (e, mv) => {
        mv.on(\'close\', async() => {
            // tslint:disable-next-line:no-console
            console.log(\'备份完成……\');
            // 开始上传
            await upload();
            conn.end();
        })
    })
}
async function upload() {
    spinner.start();
    client.scp(\'./dist/master/\', {
        host: serveConfig.host,
        port: serveConfig.port,
        username: serveConfig.username,
        password: serveConfig.password,
        path: serveConfig.path
    }, fail => {
        spinner.stop();
        if (!fail) {
            // tslint:disable-next-line:no-console
            console.log(\'项目发布完成!\');
        } else {
            // tslint:disable-next-line:no-console
            console.log(\'fail:\', fail);
        }
    });
}
conn.on(\'ready\', async() => {
    // 先备份原文件
    await backup();

}).connect({
    host: serveConfig.host,
    port: serveConfig.port,
    username: serveConfig.username,
    password: serveConfig.password
});

package.json配置命令:

 

 配置完成,运行 npm run publish:test,即可打包,发布到测试服务器。

 

分类:

技术点:

相关文章:

  • 2021-12-04
  • 2021-12-14
  • 2021-12-14
  • 2021-08-01
  • 2021-12-14
  • 2021-11-06
  • 2021-11-06
  • 2021-12-10
猜你喜欢
  • 2021-11-30
  • 2021-03-28
  • 2021-11-06
  • 2021-12-14
  • 2021-12-03
  • 2021-12-14
相关资源
相似解决方案