[关闭]
@bornkiller 2014-10-16T02:02:37.000000Z 字数 2976 阅读 2532

Nodejs command

nodejs


前言

关于执行脚本,肯定会想到shell脚本,借助于存量庞大的资源储备,生产力很不错。但上手难度大,语法略显逆天,也是不争的事实。如果可以使用javascript来写脚本,对于前端工程师来说可谓减轻不少工作量。因为工作需求,需要做HTTP LOG分析,虽然最后还是会采用shell脚本,但是会实现一个Node版本。

参考资源

文件准备

创建临时工作目录,创建待运行脚本文件。

  1. mkdir workDir;
  2. cd workDir;
  3. touch sample
  4. chmod u+x sample;
  5. npm install commander
  6. npm install inquirer

脚本编写

Nodejs原生模块processreadline用来处理脚本编写。前者实现命令定义,后者负责交互。先使用readline模块实现让用户输入两个数字,然后求和输出.以下代码即可实现

  1. #!/usr/bin/env node
  2. var readline = require('readline');
  3. var calculator = readline.createInterface(process.stdin, process.stdout);
  4. var first, second;
  5. calculator.question('what the first number is? \t', function(firstInput) {
  6. first = firstInput;
  7. calculator.question('what the second number is? \t', function(secondInput) {
  8. second = secondInput;
  9. console.log(first + second);
  10. calculator.close();
  11. });
  12. });

运行结果如下(没有作数值转换,不要在意这些细节,(^__^) 嘻嘻……)

  1. MacdeMacBook-Pro-3:command mac$ ./sum
  2. what the first number is? 12
  3. what the second number is? 23
  4. 1223

再使用process模块伪造简单命令,用以输出所有参数。

  1. #!/usr/bin/env node
  2. console.log(process.argv.slice(2));

运行结果如下,通过process.argv可以获得所有参数,API文档介绍非常清楚。

  1. MacdeMacBook-Pro-3:command mac$ ./sum install koa
  2. [ 'install', 'koa' ]

第三方模块

虽然理论上来说利用原生模块即可实现脚本编写,但是需要处理的细节过多,会造成脚本臃肿,所以需要使用第三方模块。commandar, inquirer。前者主要负责脚本功能定义,后者负责交互。需要注意,commandar当前四个交互方法貌似有问题,本机上完全失败,慎用。

  1. #!/usr/bin/env node
  2. var program = require('commander');
  3. var readline = require('readline');
  4. program
  5. .version('0.1.0')
  6. .option('-p, --no-peppers', 'Add peppers')
  7. .option('-P, --pineapple', 'Add pineapple')
  8. .option('-n, --name [name]', 'Add bbq sauce');
  9. program.parse(process.argv);

执行--help看输出结果,非常熟悉,非常简洁。

  1. MacdeMacBook-Pro-3:command mac$ ./love --help
  2. Usage: love [options]
  3. Options:
  4. -h, --help output usage information
  5. -V, --version output the version number
  6. -p, --no-peppers Add peppers
  7. -P, --pineapple Add pineapple
  8. -n, --name [name] Add bbq sauce

再看一下inquire的简单输出

  1. #!/usr/bin/env node
  2. var inquirer = require('inquirer');
  3. var questions = [
  4. {
  5. type: 'checkbox',
  6. name: 'selection',
  7. message: 'select fruits',
  8. choices: [
  9. {
  10. name: 'apple'
  11. },
  12. {
  13. name: 'banana'
  14. },
  15. {
  16. name: 'melon'
  17. }
  18. ]
  19. }
  20. ];
  21. inquirer.prompt(questions, function(answers) {
  22. console.log(answers.selection);
  23. })

执行结果如下

  1. MacdeMacBook-Pro-3:command mac$ ./inquir
  2. ? select fruits:
  3. apple
  4. banana
  5. ❯◉ melon

简易文件过滤示例

通道传输数据通过process.stdinprocess.stdout,按照标准的stream处理即可,与commandar结合使用不再赘述。

  1. #!/usr/bin/env node
  2. process.stdin.setEncoding('utf8');
  3. process.stdin.on('data', function(chunk) {
  4. var messages = chunk.split('\n').slice(0, -1);
  5. messages.forEach(function(value, key) {
  6. if (value.indexOf(process.argv[2]) !== -1) {
  7. console.log(Date.now() + '\t' + value);
  8. }
  9. })
  10. });
  11. process.stdin.on('end', function() {
  12. console.log('stdin resolved');
  13. });

运行结果如下:

  1. MacdeMacBook-Pro-3:command mac$ ls
  2. inquir love node_modules stdin sum
  3. MacdeMacBook-Pro-3:command mac$ ls | ./stdin n
  4. 1413424753506 inquir
  5. 1413424753508 node_modules
  6. 1413424753509 stdin
  7. stdin resolved

大功告成,stream处理,在NPM上肯定存在对应封装包,不必深究。

总结

上述第三方示例基本上来自文档示例,如有兴趣,可以自行前往查阅。相当于基础命令行模块,具体的业务实现需要其他的功能模块来实现,不做细究。当前暂时有一个问题,stdin, stderror, stdout处理暂时不太清楚,working on that。

联系方式

QQ: 491229492

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注