Socket
Socket
Sign inDemoInstall

cac

Package Overview
Dependencies
43
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

75

package.json
{
"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 &copy; [EGOIST](https://github.com)
MIT © [EGOIST](https://github.com/egoist)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc