找到合适的工具包,开发nodejs命令行工具是很容易的
版本号是我当前使用的版本,可自行选择
分4步:
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"
}
#! /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
给命令行输出上色
评论时间:2023-10-17 21:25:03
而作为企业信息化建设不可或缺的核心硬件设施,服务器则承虚拟主机使用感受 担着支撑整个系统运行的作用美国虚拟主机平台 硬件设施,服务器则承担着支撑整个系统运行的作用
评论时间:2023-01-09 03:25:02
下面是我所知道的最基本的网页制作步骤:1.准备云虚拟主机独享和共享该如何选择 好你要做的页面(当然也可以使用HTML语言)如何免费申请虚拟主机 面(当然也可以使用HTML语言)
2023-12-25 22:44:35 admin
活动发布区版规2023-05-27 22:03:52 御风而行
容器、无服务器、虚拟机:安全性差...2023-05-27 20:43:39 1356
服务器宕机的造成原因和解决方法介...2023-05-27 20:43:25 qingym
别糊涂了!服务器与工作站的五大区...2023-05-27 19:08:41 小绵羊0123
科技巨头布局云端 服务器 网络硬...2023-05-27 18:17:46 姐姐的跟屁虫
钉钉因系统访问流量激增,宕机1小...2022-07-23 00:34:02 freeatom
常见ftp命令 FTP命令使用详...2022-07-21 02:17:02 ares
双硬盘组建Raid0磁盘阵列图文...2022-07-20 06:17:02 mankeung123
用友软件客户端连接不上服务器的解...2022-07-23 00:51:02 antonfxb
WebService实例2022-07-13 05:38:02 苯小孩
开发、运维不可不看的Linux调...2022-07-20 18:51:02 nightstone
如何使用Charles抓取Web...2022-01-01 05:38:02 eastshop
原来双线机房是这样的2022-01-28 05:38:03 fxdfbl
基于IP的虚拟主机配置的三种方法2022-01-30 05:38:02 powerdream
如何设置不支持基于域名虚拟主机的...2022-04-04 05:38:03 gw20002000
iis配置web服务器2022-07-20 07:34:02 melon
Linux文件系统详细介绍2022-07-20 16:17:02 remus
我教你写个Shadowsocks2022-07-19 20:17:02 dengyu0429
linux vi使用及详细介绍2022-07-20 01:00:05 激动的舌头
Linux视频教程:用户管理命令...2022-07-21 20:51:02 jessica-an
创建本地FTP站点2022-02-07 05:38:03 jasonkidd
WEB服务器配置详解2022-07-20 04:51:02 wolfssss
ACL权限-默认与递归(4)2022-07-22 15:00:05 淡水珊瑚
Linux下 Samba Ser...