cac
Advanced tools
Comparing version 0.0.0 to 0.1.0
111
index.js
'use strict' | ||
const Path = require('path') | ||
const co = require('co') | ||
const isGeneratorFunction = require('is-generator-function') | ||
const minimist = require('minimist') | ||
const camelcaseKeys = require('camelcase-keys') | ||
const readPkgUp = require('read-pkg-up') | ||
const assign = require('deep-assign') | ||
class Cac { | ||
parse(argv) { | ||
argv = argv || process.argv.slice(2) | ||
this.argv = minimist(argv, { '--': true }) | ||
const parentDir = Path.dirname(module.parent.filename) | ||
function formatArgv(argv) { | ||
const input = argv._ | ||
delete argv._ | ||
return { | ||
input, | ||
flags: camelcaseKeys(argv, {exclude: ['--']}) | ||
} | ||
} | ||
module.exports = new Cac() | ||
function printWrap(pkg, text) { | ||
const header = pkg.description | ||
? `\n ${pkg.description}\n` | ||
: '' | ||
console.log(header + text) | ||
} | ||
const Cac = function (help, options) { | ||
if (!(this instanceof Cac)) { | ||
return new Cac(help, options) | ||
} | ||
this.commands = {} | ||
this.rawCommands = [] | ||
this.help = help || '' | ||
this.options = options || {} | ||
this.pkg = readPkgUp.sync({ | ||
cwd: parentDir, | ||
normalize: false | ||
}).pkg | ||
} | ||
const _ = Cac.prototype | ||
_.command = function (name, fn) { | ||
this.commands[name] = fn | ||
this.rawCommands.push(name) | ||
} | ||
_.parse = function (argv) { | ||
argv = argv || process.argv.slice(2) | ||
const options = assign({}, { | ||
alias: { | ||
v: 'version', | ||
h: 'help' | ||
} | ||
}, this.options) | ||
this.argv = formatArgv(minimist(argv, options)) | ||
this.bootstrap() | ||
} | ||
_.bootstrap = function () { | ||
const title = this.pkg.bin | ||
? Object.keys(this.pkg.bin)[0] | ||
: this.pkg.name | ||
process.title = title | ||
if (this.argv.flags.help) { | ||
this.showHelp() | ||
return | ||
} | ||
if (this.argv.flags.version && this.pkg.version) { | ||
console.log(this.pkg.version) | ||
return | ||
} | ||
const command = this.argv.input[0] | ||
if (this.rawCommands.indexOf(command) === -1) { | ||
this.runCommand(this.commands['*']) | ||
} else { | ||
this.runCommand(this.commands[command]) | ||
} | ||
} | ||
_.runCommand = function (commandFn) { | ||
if (commandFn) { | ||
const context = { | ||
input: this.argv.input, | ||
flags: this.argv.flags, | ||
showHelp: this.showHelp | ||
} | ||
if (isGeneratorFunction(commandFn)) { | ||
co(commandFn.bind(context)) | ||
} else { | ||
commandFn.call(context) | ||
} | ||
} | ||
} | ||
_.showHelp = function () { | ||
if (this.help) { | ||
if (Array.isArray(this.help)) { | ||
printWrap.call(this.pkg, this.help.join('\n')) | ||
} else { | ||
printWrap(this.pkg, this.help) | ||
} | ||
} | ||
} | ||
module.exports = Cac |
{ | ||
"name": "cac", | ||
"version": "0.0.0", | ||
"description": "command line parser made simple yet powerful.", | ||
"main": "index.js", | ||
"version": "0.1.0", | ||
"description": "Command And Conquer.", | ||
"license": "MIT", | ||
"repository": "egoist/cac", | ||
"author": { | ||
"name": "EGOIST", | ||
"email": "0x142857@gmail.com", | ||
"url": "github.com/egoist" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "xo && nyc ava" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/egoist/cac.git" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"cli" | ||
"cli", | ||
"commander", | ||
"meow", | ||
"minimist" | ||
], | ||
"author": "EGOIST", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/egoist/cac/issues" | ||
}, | ||
"homepage": "https://github.com/egoist/cac#readme", | ||
"dependencies": { | ||
"minimist": "^1.2.0" | ||
}, | ||
"engines": { | ||
"node": ">= 4" | ||
}, | ||
"devDependencies": { | ||
"cz-conventional-changelog": "^1.1.5" | ||
"ava": "latest", | ||
"coveralls": "^2.11.9", | ||
"nyc": "^6.4.0", | ||
"then-sleep": "^1.0.1", | ||
"xo": "latest" | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
"xo": { | ||
"semicolon": false, | ||
"space": 2, | ||
"rules": { | ||
"operator-linebreak": [ | ||
2, | ||
"before" | ||
], | ||
"no-warning-comments": [ | ||
2, | ||
{ | ||
"terms": [ | ||
"fixme" | ||
], | ||
"location": "start" | ||
} | ||
] | ||
} | ||
}, | ||
"dependencies": { | ||
"camelcase-keys": "^2.1.0", | ||
"co": "^4.6.0", | ||
"deep-assign": "^2.0.0", | ||
"is-generator-function": "^1.0.3", | ||
"minimist": "^1.2.0", | ||
"read-pkg-up": "^1.0.1" | ||
} | ||
} |
@@ -1,9 +0,80 @@ | ||
# cac | ||
# CAC [![NPM version](https://img.shields.io/npm/v/cac.svg)](https://npmjs.com/package/cac) [![NPM downloads](https://img.shields.io/npm/dm/cac.svg)](https://npmjs.com/package/cac) [![Build Status](https://img.shields.io/circleci/project/egoist/cac/master.svg)](https://circleci.com/gh/egoist/cac) [![Coveralls branch](https://img.shields.io/coveralls/egoist/cac/master.svg)](https://github.com/egoist/cac) | ||
The command-line parser and router for the 90s. | ||
**C**ommand **A**nd **C**onquer, the queen living in your command line. | ||
**Under heavily development** | ||
## Features | ||
- [x] Simplified [commander.js](https://github.com/tj/commander.js) | ||
- [x] Camelcased keys, eg `--disable-input` => `disableInput` | ||
- [x] Automatically read `package.json`, parse package meta | ||
- [x] Automatically print related data when `--version` and `--help` | ||
- [x] Change `process.title` for you | ||
- [x] Support [co](https://github.com/tj/co) flow | ||
- [ ] Well tested | ||
## Install | ||
```bash | ||
$ npm install --save cac | ||
``` | ||
## Usage | ||
Start your first CLI app in `example.js`: | ||
```js | ||
const cac = require('cac') | ||
const fs = require('fs-promise') | ||
const cli = cac(` | ||
Usage: | ||
node example.js create <filename> -m [content] | ||
Options: | ||
-m, --message File content | ||
-h, --help Print help (You are here!) | ||
`, { | ||
alias: { | ||
m: message, | ||
h: help | ||
} | ||
}) | ||
cli.command('create', function* () { | ||
const fileName = this.input[1] | ||
const content = this.flags.message | ||
yield fs.createFile(fileName, 'hello') | ||
console.log('Done'! | ||
}) | ||
cli.command('*', function () { | ||
console.log('Everything else') | ||
}} | ||
// use .parse() to bootstrap the CLI | ||
cli.parse() | ||
``` | ||
All set! Just run: | ||
```bash | ||
$ node example.js create lol.txt -m "just lol 😂" | ||
``` | ||
## API | ||
### cac([help], [options]) | ||
#### help | ||
Type: `string` `array` | ||
The help info. | ||
#### options | ||
The [minimist](https://github.com/substack/minimist) options. | ||
## License | ||
MIT © [EGOIST](https://github.com) | ||
MIT © [EGOIST](https://github.com/egoist) |
Sorry, the diff of this file is not supported yet
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
6362
96
81
6
5
4
1
1
+ Addedcamelcase-keys@^2.1.0
+ Addedco@^4.6.0
+ Addeddeep-assign@^2.0.0
+ Addedis-generator-function@^1.0.3
+ Addedread-pkg-up@^1.0.1
+ Addedcamelcase@2.1.1(transitive)
+ Addedcamelcase-keys@2.1.0(transitive)
+ Addedco@4.6.0(transitive)
+ Addeddeep-assign@2.0.0(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedfind-up@1.1.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.14.0(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-obj@1.0.1(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedload-json-file@1.1.0(transitive)
+ Addedmap-obj@1.0.1(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@2.1.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@1.1.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedread-pkg@1.1.0(transitive)
+ Addedread-pkg-up@1.0.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.18(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)