package.json
中的 bin
字段如今,无论是前端项目仍是 node
项目,通常都会用 npm
作包管理工具,而 package.json
是其相关的配置信息。前端
对 node
项目而言,模块导出入口文件由 package.json
的 main
字段指定,而若是是要安装到命令行的工具,则是由 package.json
的 bin
字段指定。node
{ "name": "pro", "bin": "bin/pro.js" }
这样安装的命令名称就是 pro
。linux
{ "name": "pro-cli", "bin": { "pro": "bin/pro.js" } }
这样安装的命令名称也是 pro
。webpack
{ "name": "pro-cli", "bin": { "pro": "bin/pro.js", "mini": "bin/mini.js" } }
这样安装就有 pro
与 mini
两个命令。git
bin/pro.js
文件的写法#!/usr/bin/env node require('../lib/pro');
与普通的 js
文件写法同样,只是前面要加上 #!/usr/bin/env node
。github
这段前缀代码叫 shebang
,具体能够参考 Shebang (Unix) - Wikipedia).web
npm i -g pro-cli
这种安装方式能够在命令行全局使用。shell
pro dev pro build
npm i --save-dev pro-cli
这种安装方式须要配合 npm
一块儿使用,好比:npm
# package.json { "scripts": { "dev": "pro dev", "build": "pro build" } } # 使用 npm run dev npm run build
通常来讲,一个命令都会有以下的一些参数:json
-v, --version
或 -V, --version
: 查看版本号-h, --help
: 查看帮助信息若是彻底本身来写的,就会很麻烦,尤为是帮助信息。因此,选择一个好的命令行封装库,可以帮咱们省去不少工做。
用的比较多的:
以 commander.js
为例:
npm install commander --save
const commander = require('commander');
注册版本号与描述
commander .version('0.0.1') .description('A cli application named pro');
注册参数(非子命令参数)
commander .option('-p, --peppers', 'Add peppers') .option('-P, --pineapple', 'Add pineapple') .option('-b, --bbq-sauce', 'Add bbq sauce') .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
注册子命令
commander .command('rm <dir>') .option('-r, --recursive', 'Remove recursively') .action((dir, cmd) => { console.log('remove ' + dir + (cmd.recursive ? ' recursively' : '')) })
解析
commander.parse(process.argv);
查看版本号
pro -V pro --version # 打印结果 0.0.1
运行 rm
子命令
pro rm dir
查看帮助(commander
会自动生成)
pro -h pro --help # 打印结果 Usage: pro [options] A cli application named pro Options: -h, --help output usage information -V, --version output the version number -p, --peppers Add peppers -P, --pineapple Add pineapple -b, --bbq Add bbq sauce -c, --cheese <type> Add the specified type of cheese [marble] -C, --no-cheese You do not want any cheese
更多用法查看 commander.js。
var argv = require('minimist')(process.argv.slice(2)); console.dir(argv);
$ node example/parse.js -a beep -b boop { _: [], a: 'beep', b: 'boop' }
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz { _: [ 'foo', 'bar', 'baz' ], x: 3, y: 4, n: 5, a: true, b: true, c: true, beep: 'boop' }
更多参考 minimist。
更多参考 chalk。
更多参考 Inquirer.js。
var shell = require('shelljs'); if (!shell.which('git')) { shell.echo('Sorry, this script requires git'); shell.exit(1); } // Copy files to release dir shell.rm('-rf', 'out/Release'); shell.cp('-R', 'stuff/', 'out/Release'); // Replace macros in each .js file shell.cd('lib'); shell.ls('*.js').forEach(function (file) { shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file); shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file); shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file); }); shell.cd('..'); // Run external tool synchronously if (shell.exec('git commit -am "Auto-commit"').code !== 0) { shell.echo('Error: Git commit failed'); shell.exit(1); }
更多参考 shelljs。
更多参考 blessed-contrib。
与 shelljs 功能差很少。
const $ = require('cash'); const out = $.ls('.', {l: true});
更多参考 cash。
与 Inquirer.js 功能差很少。
更多参考 prompts。
更多参考 ora。
downloading [===== ] 39/bps 29% 3.7s
更多参考 progress。
更多关于命令行的工具库能够参考 command-line-utilities。
命令行相关的应用就不少啦,好比 babel
、webpack
、rollup
、eslint
等,但这些不单单是命令行工具。
下面介绍一些纯命令行应用:
更多纯命令行应用能够参考 command-line-apps。
更多博客,查看 https://github.com/senntyou/blogs
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)