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

版本号是我当前使用的版本,可自行选择
分4步:
npm linknhw => 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"
}
#! /usr/bin/env node
stackoverflow.com/questions/3…
这句话是一个shebang line实例, 作用是告诉系统运行这个文件的解释器是node;
比如,本来需要这样运行node ./file.js,但是加上了这句后就可以直接./file.js运行了
{
// 模块系统的名字,如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 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
效果:
推荐使用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)
实际效果就是这些小图标:
这里可以找到各式各样的badges:
github.com/badges/shie…
travis-ci:
.travis.yml这个文件, 并写好简单的配置
language: node_js
node_js:
- "6"
"scripts": {
"test": "mocha"
}
在travis设置成功后,继续覆盖率的处理:
npm install istanbul coveralls --save-dev
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
给命令行输出上色