在开发过程中,项目的编写还是修改是必不可少的,需要我们多次部署代码到仿真,线上,虽然不是我们的事,但是我们需要不断的打包,多麻烦,使用 shell 脚本 帮你轻松搞定,上传到你想上传的地方,大大缩短时间。
Shell 俗称壳,它接收用户命令,然后调用相应的应用程序。
- $ npm install [-g] shelljs
有两种用法,一种是全局引用,也就是添加到 window 上
- require('shelljs/global')
第二种就是当作一个全局变量。
- var shell = require('shelljs');
- shell.echo('hello world');
但是官方不建议第一种方法,怕污染全局。
- var shell = require('shelljs');
- if (!shell.which('git')) {
- shell.echo('Sorry, this script requires git');
- shell.exit(1);
- }
- // Copy files to release dir
- shell.rm('-rf', 'out/Release');
- shell.cp('-R', 'stuff/', 'out/Release');
- // Replace macros in each .js file
- shell.cd('lib');
- shell.ls('*.js').forEach(function(file) {
- shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
- shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
- shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file);
- });
- shell.cd('..');
- // Run external tool synchronously
- if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
- shell.echo('Error: Git commit failed');
- shell.exit(1);
- }
- require('shelljs/global')
- var res = exec('git status -s').stdout; console.log('开始转换 git status')
- var arrayResult= res.split('\n');
- var array = []
- console.log('开始筛选 git status')
- array = arrayResult.filter((item) => {
- var i = item.trim()
- if(i[0] === 'M' || i[0] === 'A') {
- return true
- }
- return false
- }
"M" 或 "A" 是 git 里面的文件标志。
状态 | 描述 |
---|---|
A | 你本地新增的文件(服务器上没有) |
C | 文件的一个新拷贝 |
D | 你本地删除的文件(服务器上还在) |
M | 文件的内容或者 mode 被修改了 |
R | 文件名被修改了 |
T | 文件的类型被修改了 |
U | 文件没有被合并 (你需要完成合并才能进行提交) |
X | 未知状态 (很可能是遇到 git 的 bug 了,你可以向 git 提交 bug report) |
- var host = '上传的地址'
- console.log('开始转换 上传:',host)
- array.forEach((item) => {
- var i = item.trim().replace(/^m|^a/i,'').trim()
- exec(`要上传的地址`)
- console.log(i)
- })
注意因为 git status -s 查看文件的信息,需要在 commit 之前上传。
执行 npm 命令,用 nodejs 调用 shell 脚本。
如果需要的话可以去官网看看,链接
来源: http://www.jianshu.com/p/43ddb93a65e9