用shelljs实现前端部署自动化

1、场景

在前端开发过程当中,你确定常常使用到如下等命令:前端

npm run build
git add .
git commit -m 'commit'
git push

本人在用vue-cli写个人我的博客时,将其部署到coding pages上。不用github pages的缘由纯粹是由于慢。。。每一次部署,都要将上面的命令敲一遍,实在令我很痛苦。若是能用一条命令执行以上全部任务,那就爽多了。vue

话很少说,说干就干。node

2、Shelljs

这个库可以让咱们在js文件中执行shell命令,具体能够看文档git

安装

npm install [-g] shelljs

有两种使用方式,一种是全局模式(对应全局安装),一种是局部模式。看下面的使用案例就知道二者区别。github

3、使用

在根目录下新建文件shell.js,内容以下:vue-cli

//局部模式
var shell = require('shelljs');
//全局模式下,就不须要用shell开头了。
//require('shelljs/global');

if (shell.exec('npm run build').code !== 0) {//执行npm run build 命令
  shell.echo('Error: Git commit failed');
  shell.exit(1);
}

//因为个人用另一个仓库存放dist目录,因此这里要将文件增量复制到目标目录。并切换到对应目录。
shell.cp ('-r', './dist/*', '../../Rychou');
shell.cd('../../Rychou');

shell.exec('git add .');
shell.exec("git commit -m 'autocommit'")
shell.exec('git push')

这时在根目录下执行node shell.js就能够了shell

这里只是最简单的使用案例。npm

4、再让它更方便些

package.json中加入:json

"script":{
+    "push":"node ./shell.js"
}

在根目录下执行npm run push就搞定了。ui

参考连接: Shelljs
相关文章
相关标签/搜索