前言
找到合适的工具包,开发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"
}复制代码
npm link命令
执行后,控制台里面会有以下输出:
/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复制代码
效果:
单元测试
推荐使用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:
github.com/badges/shie…
持续集成(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" }复制代码
常用的库
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…