使用Node.js的Shell脚本
我想知道是否有任何节点框架或节点lib在那里,我可以用来编写一个shell脚本? 例如,我有bash shell程序来安装Graphite和OpenTSDB RRD工具,我想用node.js来做,可能吗?
谢谢
看一下shelljs – 它实现了shell和gnu类似于coreutils的函数。 与coffeescript一起,它可以看起来非常类似于shell脚本:
if not which 'git' echo 'Sorry, this script requires git' exit 1 # Copy files to release dir mkdir '-p', 'out/Release' cp '-R', 'stuff/*', 'out/Release' # Replace macros in each .js file cd 'lib' for file in ls '*.js' sed '-i', 'BUILD_VERSION', 'v0.1.2', file sed '-i', /.*REMOVE_THIS_LINE.*\n/, '', file sed '-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat 'macro.js', file cd '..' # Run external tool synchronously if (exec 'git commit -am "Auto-commit"').code != 0 echo 'Error: Git commit failed' exit 1
你应该看看grunt ,它是一个帮助人们在node.js中编写构build和其他脚本的工具包。 有很多插件可以帮助你轻松做有趣的事情。
这就是说,如果你知道Bash,我会坚持bash。
在Twitter上查看这个有趣的线索关于Bash vs. Grunt脚本
我为什么使用Grunt
- 运行Lint工具
- 运行JSunit testing
- 运行预处理器(sass,require.js,uglify等)
我用什么Capistrano *
为
- 将代码部署到生产环境
我用什么Bash **
- 设置服务器并运行它们等
-
*
capistrano + git或厨师或任何其他 -
**
bash或其他任何你想使用的工具
有很多,其中最受欢迎的是commander
npm install --save commander
然后编写这个命令很简单:
#!/usr/bin/env node var program = require('commander'); program .version('0.0.1') .option('-f, --foo', 'enable some foo') .option('-b, --bar', 'enable some bar') .option('-B, --baz', 'enable some baz'); program.on('--foo', function(){ console.log('Stuff!'); });