找到合适的工具包,开发nodejs命令行工具是很容易的javascript
版本号是我当前使用的版本,可自行选择html
分4步:java
npm link
nhw
=> hello world
touch index.js
建立一个index.js文件,内容以下:node
#! /usr/bin/env node
console.log('hello world')复制代码
用npm init
建立一个package.json文件,以后修改为以下:git
{
"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 nodegithub
stackoverflow.com/questions/3…shell
这句话是一个shebang line实例, 做用是告诉系统运行这个文件的解释器是node;
好比,原本须要这样运行node ./file.js
,可是加上了这句后就能够直接./file.js
运行了npm
{
// 模块系统的名字,如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"
}复制代码
执行后,控制台里面会有如下输出:json
/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
命令安装便可。bash
设置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.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"
}复制代码
Portable Unix shell commands for Node.js
在nodejs里用unix命令行
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…