用Nodejs开发命令行工具

前言

找到合适的工具包,开发nodejs命令行工具是很容易的

准备工作

  • nodejs v6.10.1
  • npm v3.10.10

版本号是我当前使用的版本,可自行选择

Hello World

分4步:

  • index.js
  • package.json
  • 根目录下执行npm link
  • 执行命令nhw => hello world

touch index.js创建一个index.js文件,内容如下:

#! /usr/bin/env node

console.log('hello world')复制代码

npm init创建一个package.json文件,之后修改成如下:

{
    "name": "npmhelloworld", 
    "bin": {
        "nhw": "index.js" 
    },
    "preferGlobal": true,
    "version": "1.0.0",
    "description": "",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "jerryni <jerryni2014@gmail.com>",
    "license": "ISC"
}复制代码

内容详解

index.js

#! /usr/bin/env node

stackoverflow.com/questions/3…

这句话是一个shebang line实例, 作用是告诉系统运行这个文件的解释器是node;
比如,本来需要这样运行node ./file.js,但是加上了这句后就可以直接./file.js运行了

package.json

{
    // 模块系统的名字,如require('npmhelloworld')
    "name": "npmhelloworld", 
    "bin": {
        "nhw": "index.js" // nhw就是命令名 ,index.js就是入口
    },

    // 加入 安装的时候, 就会有-g的提示了
    "preferGlobal": true,


    // 去掉main: 'xxx.js'  是模块系统的程序入口
    "version": "1.0.0",
    "description": "",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "jerryni <jerryni2014@gmail.com>",
    "license": "ISC"
}复制代码

执行后,控制台里面会有以下输出:

/usr/local/bin/nhw -> /usr/local/lib/node_modules/npmhelloworld/index.js
/usr/local/lib/node_modules/npmhelloworld -> /Users/nirizhe/GitHub/npmhelloworld复制代码

解释:创建了2个软链接分别放到系统环境变量$PATH目录里,nhw命令和npmhellworld模块。npm link在用户使用的场景下是不需要执行的,用户使用npm i -g npmhellworld命令安装即可。

发布项目到npm官网供大家使用

设置npm用户名,没有的话先到npm官方网站注册一个:

npm set init.author.name "Your Name"
npm set init.author.email "you@example.com"
npm set init.author.url "http://yourblog.com"

npm adduser复制代码

项目根目录运行:

npm publish

注意:

每次发布需要修改package.json中的版本号,否则无法发布

如何处理命令行参数

当下比较流程的几个工具对比

这边使用yargs

npm install --save yargs

请看之前我实战一段代码

大概是这个样子:

var argv = yargs
    .option('name', {
        type: 'string',
        describe: '[hostName] Switch host by name',
        alias: 'n'
    })
    .option('list', {
        boolean: true,
        default: false,
        describe: 'Show hostName list',
        alias: 'l'
    })
    .option('close', {
        type: 'string',
        describe: 'close certain host',
        alias: 'c'
    })
    .example('chost -n localhost', 'Switch localhost successfully!')
    .example('chost -c localhost', 'Close localhost successfully!')
    .example('chost -l', 'All host name list: xxx')
    .help('h')
    .alias('h', 'help')
    .epilog('copyright 2017')
    .argv复制代码

效果:

yargs

单元测试

推荐使用mocha

var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal(-1, [1,2,3].indexOf(4));
    });
  });
});复制代码

执行

$ ./node_modules/mocha/bin/mocha

  Array
    #indexOf()
      ✓ should return -1 when the value is not present


  1 passing (9ms)复制代码

让你的项目显得正规(github badges)

实际效果就是这些小图标:

badges

这里可以找到各式各样的badges:
github.com/badges/shie…

持续集成(CI)和代码覆盖率

travis

Coverage Status

travis-ci:

  • 用github帐号登录,这时网站上列出你github上的项目
  • 在项目根目录放.travis.yml这个文件, 并写好简单的配置
    language: node_js
    node_js:
    - "6"复制代码
  • 在项目的package.json中添加测试脚本, 因为travis默认会执行npm test
    "scripts": {
      "test": "mocha"
    }复制代码

在travis设置成功后,继续覆盖率的处理:

  • 安装2个依赖

npm install istanbul coveralls --save-dev

  • travis的配置文件中添加
    language: node_js
    node_js:
    - "6"
    after_script:
    - npm run coverage复制代码
  • 修改package.json中的测试脚本
    "scripts": {
      "test": "node ./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha",
      "coverage": "cat ./coverage/lcov.info | coveralls"
    }复制代码

常用的库

shelljs

Portable Unix shell commands for Node.js
在nodejs里用unix命令行

chalk

Terminal string styling done right
给命令行输出上色

参考资料

javascriptplayground.com/blog/2015/0…
medium.freecodecamp.com/writing-com…
www.ruanyifeng.com/blog/2015/0…
gist.github.com/coolaj86/13…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值