+106
-5
| '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 |
+2
-2
| The MIT License (MIT) | ||
| Copyright (c) 2016 EGOIST 0x142857@gmail.com | ||
| Copyright (c) EGOIST <0x142857@gmail.com> (github.com/egoist) | ||
@@ -21,2 +21,2 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| THE SOFTWARE. |
+50
-25
| { | ||
| "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" | ||
| } | ||
| } |
+75
-4
@@ -1,9 +0,80 @@ | ||
| # cac | ||
| # CAC [](https://npmjs.com/package/cac) [](https://npmjs.com/package/cac) [](https://circleci.com/gh/egoist/cac) [](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
-5
| const cac = require('./') | ||
| cac.parse() | ||
| console.log(cac) |
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
129.59%96
700%81
710%6
500%5
400%4
-33.33%1
Infinity%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added