(一)需要

每次是打好包,手动公布。

发的多了,就想着能不能写脚本,实现半自动公布我的项目。

(二)思路

1、须要提前做好免密登录

免密登录可查看
https://segmentfault.com/a/11...

2、JS中写Shell脚本

  • 装置ShellJS
    npm install [-g] shelljs
  • 我的项目打包
  • 上传打包好的文件

(三)实现代码

var shell = require('shelljs')shell.echo('start build')if (shell.exec('npm run test').code !== 0) { // 执行npm run build 命令  shell.echo('Error: Git commit failed')  shell.exit(1)}shell.echo('build end')shell.echo('upload start')// 将我的项目上传到服务器对应的目录下shell.exec('scp dist/index.html root@IP:/目录/ ')shell.exec('scp -r dist/js root@IP:/目录/ ')shell.exec('scp -r dist/static root@IP:/目录/')shell.echo('deploy end')shell.exit(1)

参考链接

ShellJS GitHub我的项目

https://github.com/shelljs/sh...