一些小众却有用的 Node.js 包( 三 )


});
 
server.listen(PORT);
 
console.log('listening on http://localhost:' + PORT + '/');
shelljs
shelljs是一个能够让你在任何系统上使用通用的Unix命令的包,不管是 Windows、linux 还是 mac 。这样你就不用再为项目分别编写 bash 和批处理脚本 。shelljs 提供了类似 Unix 的环境,如果你需要编写脚本来运行测试、提交代码或在服务器上启动,则只需编写一次即可 。
可以用命令执行类似操作:

require('shelljs/global');
 
ls('*.js').forEach(function(file) {
sed('-i', 'BUILD_VERSION', 'v2.0.3', file);
sed('-i', /.*REMOVE_THIS_LINE.*n/, '', file);
sed('-i', /.*REPLACE_THIS_LINE.*n/, cat('macro.js'), file);
});
执行常见命令:
require('shelljs/global');
 
mkdir('-p', 'release/data');
cp('-R', 'data/*', 'release/data');
检查可用的二进制文件:
require('shelljs/global');
 
if (!which('git')) {
echo('This script requires git!');
exit(1);
}
甚至可以像在 bash 脚本中一样运行命令:
if (exec('git commit -am "Release commit"').code !== 0) {
echo('Error: Git commit failed!');
exit(1);
}

【一些小众却有用的 Node.js 包】


推荐阅读