├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── examples ├── alias.js ├── cli.js ├── events.js ├── map.js ├── plugins │ ├── a.js │ ├── b.js │ └── c.js ├── store.js └── use.js ├── gulpfile.js ├── index.js ├── package.json ├── test ├── command.cwd.js ├── command.get.js ├── command.has.js ├── command.store.js ├── command.use.js └── fixtures │ ├── package.json │ └── plugins │ ├── a.js │ ├── b.js │ └── c.js └── verbfile.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | indent_size = 2 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | 16 | [test/**] 17 | trim_trailing_whitespace = false 18 | insert_final_newline = false 19 | 20 | [templates/**] 21 | trim_trailing_whitespace = false 22 | insert_final_newline = false 23 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *.sublime-* 3 | _gh_pages 4 | bower_components 5 | node_modules 6 | npm-debug.log 7 | actual 8 | test/actual 9 | temp 10 | tmp 11 | TODO.md 12 | vendor 13 | .idea 14 | benchmark 15 | coverage 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "stable" 5 | - "5" 6 | - "4" 7 | - "0.12" 8 | - "0.10" 9 | matrix: 10 | fast_finish: true 11 | allow_failures: 12 | - node_js: "0.10" 13 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | Adds a `cli` method to `base` for mapping parsed command line arguments existing [base][] methods or custom functions. 2 | 3 | The goal is to simplify the process of settings up command line logic for your [base][] application. 4 | 5 | ## Usage 6 | 7 | ```js 8 | var cli = require('{%= name %}'); 9 | var Base = require('base'); 10 | var app = new Base(); 11 | 12 | // register the plugin 13 | app.use(cli()); 14 | ``` 15 | 16 | ## API 17 | 18 | This adds a `cli` object to [base][] with the following (chainable) methods (`base.cli.*`): 19 | 20 | - `.map()` - [.map](#map): add mappings from command line flags/options to custom functions or `base` methods 21 | - `.alias()` - [.alias](#alias): similar to `map` but creates simple aliases. For example, `alias('show', 'get')` would invoke the `.get()` method when `--show` is passed on the command line 22 | - `.process()` - [.process](#process): once all mappings are defined, pass `argv` to `.process()` to iterate over the mappings, passing `argv` as context. 23 | 24 | ## Example 25 | 26 | ```js 27 | var argv = require('minimist')(process.argv.slice(2)); 28 | var expand = require('expand-args'); 29 | var cli = require('{%= name %}'); 30 | var Base = require('base'); 31 | 32 | var app = new Base(); 33 | app.use(cli()); 34 | 35 | app.cli 36 | .map('get', function(key, val) { 37 | app.get(key, val); 38 | }) 39 | .map('set', function(key, val) { 40 | app.set(key, val); 41 | }) 42 | 43 | app.cli.process(expand(argv), function(err) { 44 | if (err) throw err; 45 | }); 46 | 47 | // command line args: 48 | // 49 | // '--set=a:b --get=a' 50 | // 51 | // prints: 52 | // 53 | // 'a' 54 | // 55 | ``` 56 | 57 | ## Commands 58 | 59 | The following commands are currently supported. 60 | 61 | {%= apidocs("lib/**/*.js") %} 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # base-cli [![NPM version](https://img.shields.io/npm/v/base-cli.svg?style=flat)](https://www.npmjs.com/package/base-cli) [![NPM downloads](https://img.shields.io/npm/dm/base-cli.svg?style=flat)](https://npmjs.org/package/base-cli) [![Build Status](https://img.shields.io/travis/node-base/base-cli.svg?style=flat)](https://travis-ci.org/node-base/base-cli) 2 | 3 | > Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a few plugins, like 'base-store', 'base-options' and 'base-data'. 4 | 5 | You might also be interested in [base-config](https://github.com/node-base/base-config). 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install base-cli --save 13 | ``` 14 | 15 | Adds a `cli` method to `base` for mapping parsed command line arguments existing [base](https://github.com/node-base/base) methods or custom functions. 16 | 17 | The goal is to simplify the process of settings up command line logic for your [base](https://github.com/node-base/base) application. 18 | 19 | ## Usage 20 | 21 | ```js 22 | var cli = require('base-cli'); 23 | var Base = require('base'); 24 | var app = new Base(); 25 | 26 | // register the plugin 27 | app.use(cli()); 28 | ``` 29 | 30 | ## API 31 | 32 | This adds a `cli` object to [base](https://github.com/node-base/base) with the following (chainable) methods (`base.cli.*`): 33 | 34 | * `.map()` - [.map](#map): add mappings from command line flags/options to custom functions or `base` methods 35 | * `.alias()` - [.alias](#alias): similar to `map` but creates simple aliases. For example, `alias('show', 'get')` would invoke the `.get()` method when `--show` is passed on the command line 36 | * `.process()` - [.process](#process): once all mappings are defined, pass `argv` to `.process()` to iterate over the mappings, passing `argv` as context. 37 | 38 | ## Example 39 | 40 | ```js 41 | var argv = require('minimist')(process.argv.slice(2)); 42 | var expand = require('expand-args'); 43 | var cli = require('base-cli'); 44 | var Base = require('base'); 45 | 46 | var app = new Base(); 47 | app.use(cli()); 48 | 49 | app.cli 50 | .map('get', function(key, val) { 51 | app.get(key, val); 52 | }) 53 | .map('set', function(key, val) { 54 | app.set(key, val); 55 | }) 56 | 57 | app.cli.process(expand(argv), function(err) { 58 | if (err) throw err; 59 | }); 60 | 61 | // command line args: 62 | // 63 | // '--set=a:b --get=a' 64 | // 65 | // prints: 66 | // 67 | // 'a' 68 | // 69 | ``` 70 | 71 | ## Commands 72 | 73 | The following commands are currently supported. 74 | 75 | ## Related projects 76 | 77 | Other useful [base](https://github.com/node-base/base) plugins: 78 | 79 | * [base-config](https://www.npmjs.com/package/base-config): base-methods plugin that adds a `config` method for mapping declarative configuration values to other 'base'… [more](https://www.npmjs.com/package/base-config) | [homepage](https://github.com/node-base/base-config) 80 | * [base-data](https://www.npmjs.com/package/base-data): adds a `data` method to base-methods. | [homepage](https://github.com/jonschlinkert/base-data) 81 | * [base-generators](https://www.npmjs.com/package/base-generators): Adds project-generator support to your `base` application. | [homepage](https://github.com/node-base/base-generators) 82 | * [base-plugins](https://www.npmjs.com/package/base-plugins): Upgrade's plugin support in base applications to allow plugins to be called any time after… [more](https://www.npmjs.com/package/base-plugins) | [homepage](https://github.com/node-base/base-plugins) 83 | * [base-task](https://www.npmjs.com/package/base-task): base plugin that provides a very thin wrapper around [https://github.com/doowb/composer](https://github.com/doowb/composer) for adding task methods to… [more](https://www.npmjs.com/package/base-task) | [homepage](https://github.com/node-base/base-task) 84 | * [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://www.npmjs.com/package/base) | [homepage](https://github.com/node-base/base) 85 | 86 | ## Contributing 87 | 88 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/base-cli/issues/new). 89 | 90 | ## Building docs 91 | 92 | Generate readme and API documentation with [verb](https://github.com/verbose/verb): 93 | 94 | ```sh 95 | $ npm install verb && npm run docs 96 | ``` 97 | 98 | Or, if [verb](https://github.com/verbose/verb) is installed globally: 99 | 100 | ```sh 101 | $ verb 102 | ``` 103 | 104 | ## Running tests 105 | 106 | Install dev dependencies: 107 | 108 | ```sh 109 | $ npm install -d && npm test 110 | ``` 111 | 112 | ## Author 113 | 114 | **Jon Schlinkert** 115 | 116 | * [github/jonschlinkert](https://github.com/jonschlinkert) 117 | * [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 118 | 119 | ## License 120 | 121 | Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). 122 | Released under the [MIT license](https://github.com/node-base/base-cli/blob/master/LICENSE). 123 | 124 | *** 125 | 126 | _This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 14, 2016._ -------------------------------------------------------------------------------- /examples/alias.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var argv = require('minimist')(process.argv.slice(2)); 4 | var expand = require('expand-args'); 5 | var plugins = require('base-plugins'); 6 | var option = require('base-option'); 7 | var store = require('base-store'); 8 | var data = require('base-data'); 9 | var base = require('base'); 10 | var cli = require('../'); 11 | 12 | var app = base() 13 | .use(plugins) 14 | .use(option) 15 | .use(store('foo')) 16 | .use(data()) 17 | .use(cli()) 18 | 19 | app.cli({ 20 | s: 'set', 21 | g: 'get', 22 | h: 'has', 23 | d: 'del' 24 | }); 25 | 26 | app.on('set', function (val, key) { 27 | console.log('set:', val, key); 28 | }); 29 | 30 | /** 31 | * $ node examples/alias --set=foo:bar --set=baz:qux 32 | */ 33 | 34 | app.cli.process(expand(argv), function(err) { 35 | if (err) throw err; 36 | }); 37 | 38 | -------------------------------------------------------------------------------- /examples/cli.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var argv = require('minimist')(process.argv.slice(2)); 4 | var Base = require('base'); 5 | var app = new Base(); 6 | var cli = require('..'); 7 | 8 | app.set('pkg.data', require('../package.json')); 9 | app.use(cli()); 10 | 11 | app.cli.process(argv, function(err) { 12 | if (err) throw err; 13 | }); 14 | -------------------------------------------------------------------------------- /examples/events.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var expand = require('expand-args'); 4 | var argv = require('minimist')(process.argv.slice(2), { 5 | alias: {set: 's', get: 'g', del: 'd'} 6 | }); 7 | var store = require('base-store'); 8 | var option = require('base-option'); 9 | var data = require('base-data'); 10 | var base = require('base'); 11 | var cli = require('..'); 12 | var app = base() 13 | .use(store('base-cli-test')) 14 | .use(option) 15 | .use(data()) 16 | .use(cli()) 17 | 18 | app.on('set', function (key, val) { 19 | console.log('[set]', key, '=>', val); 20 | }); 21 | app.store.on('set', function (key, val) { 22 | console.log('[store.set]', key, '=>', val); 23 | }); 24 | app.on('option', function (key, val) { 25 | console.log('[option]', key, '=>', val); 26 | }); 27 | app.on('data', function (args) { 28 | console.log('[data]', args); 29 | }); 30 | 31 | // try these: 32 | // 33 | // $ node examples/events.js --set=foo:bar 34 | // $ node examples/events.js --store.set=one:two 35 | // $ node examples/events.js --data=foo:a,b,c 36 | // $ node examples/events.js --enable=lint 37 | // 38 | app.cli.process(expand(argv), function(err) { 39 | if (err) throw err; 40 | }); 41 | -------------------------------------------------------------------------------- /examples/map.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var argv = require('minimist')(process.argv.slice(2)); 4 | var expand = require('expand-args'); 5 | var Base = require('base'); 6 | var cli = require('..'); 7 | var base = new Base(); 8 | base.use(cli()); 9 | 10 | base.cli 11 | .map('set') 12 | .map('get', console.log) 13 | .map('del', console.log) 14 | .alias('foo', 'get'); 15 | 16 | // try: '--set=a:b --get=a' 17 | base.cli.process(expand(argv), function(err) { 18 | if (err) throw err; 19 | }); 20 | -------------------------------------------------------------------------------- /examples/plugins/a.js: -------------------------------------------------------------------------------- 1 | module.exports = function (app) { 2 | app.set('a', 'AAA'); 3 | }; 4 | -------------------------------------------------------------------------------- /examples/plugins/b.js: -------------------------------------------------------------------------------- 1 | module.exports = function (app) { 2 | app.set('b', 'BBB'); 3 | }; 4 | -------------------------------------------------------------------------------- /examples/plugins/c.js: -------------------------------------------------------------------------------- 1 | module.exports = function (app) { 2 | app.set('c', 'CCC'); 3 | }; 4 | -------------------------------------------------------------------------------- /examples/store.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var expand = require('expand-args'); 4 | var argv = require('minimist')(process.argv.slice(2), { 5 | alias: {set: 's', get: 'g', del: 'd'} 6 | }); 7 | var base = require('base'); 8 | var store = require('base-store'); 9 | var cli = require('..'); 10 | var app = base() 11 | .use(store('base-cli-test')) 12 | .use(cli()) 13 | 14 | app.on('store.set', function (key, val) { 15 | console.log('[store] set', key, '=>', val); 16 | }); 17 | app.store.on('set', function (key, val) { 18 | console.log('[store] set', key, '=>', val); 19 | }); 20 | app.store.on('get', function (key, val) { 21 | console.log('[store] got', key, '=>', val); 22 | }); 23 | app.store.on('del', function (key) { 24 | console.log('[store] deleted =>', key); 25 | }); 26 | 27 | // Try: 28 | // 29 | // 'node examples/store.js --store.set=a:b --store.get=a --store.del=a' 30 | // 31 | app.cli.process(expand(argv), function(err) { 32 | if (err) throw err; 33 | }); 34 | -------------------------------------------------------------------------------- /examples/use.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var expand = require('expand-args'); 4 | var argv = require('minimist')(process.argv.slice(2)); 5 | var base = require('base'); 6 | var store = require('base-store'); 7 | var cli = require('..'); 8 | var app = base() 9 | .use(store('base-cli-test')) 10 | .use(cli()) 11 | 12 | // Try: 13 | // 14 | // $ node examples/use.js --cwd=examples/plugins --use=a,b,c --get=a --get=b --get=c 15 | // 16 | 17 | app.store.on('use', function (key) { 18 | console.log('[plugin]', key); 19 | }); 20 | app.on('set', function (key, val) { 21 | console.log('[set]', key, '=>', val); 22 | }); 23 | app.on('get', function (key, val) { 24 | console.log('[get]', key, '=>', val); 25 | }); 26 | 27 | app.cli.process(argv, function(err) { 28 | if (err) throw err; 29 | }); 30 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var gulp = require('gulp'); 4 | var mocha = require('gulp-mocha'); 5 | var istanbul = require('gulp-istanbul'); 6 | var eslint = require('gulp-eslint'); 7 | 8 | gulp.task('coverage', function() { 9 | return gulp.src('index.js') 10 | .pipe(istanbul()) 11 | .pipe(istanbul.hookRequire()); 12 | }); 13 | 14 | gulp.task('test', ['coverage'], function() { 15 | return gulp.src('test/*.js') 16 | .pipe(mocha({reporter: 'spec'})) 17 | .pipe(istanbul.writeReports()); 18 | }); 19 | 20 | gulp.task('lint', function() { 21 | return gulp.src(['*.js', 'test/*.js']) 22 | .pipe(eslint()) 23 | .pipe(eslint.format()); 24 | }); 25 | 26 | gulp.task('default', ['test', 'lint']); 27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * base-cli 3 | * 4 | * Copyright (c) 2015-2016, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var config = require('base-config'); 11 | var argv = require('base-argv'); 12 | 13 | module.exports = function(options) { 14 | return function(app, base) { 15 | this.use(argv(options)); 16 | this.use(config.create('cli')); 17 | }; 18 | }; 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "base-cli", 3 | "description": "Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a few plugins, like 'base-store', 'base-options' and 'base-data'.", 4 | "version": "0.5.0", 5 | "homepage": "https://github.com/node-base/base-cli", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "node-base/base-cli", 8 | "bugs": { 9 | "url": "https://github.com/node-base/base-cli/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js", 14 | "lib/" 15 | ], 16 | "main": "index.js", 17 | "engines": { 18 | "node": ">=0.10.0" 19 | }, 20 | "scripts": { 21 | "test": "mocha" 22 | }, 23 | "dependencies": { 24 | "base-argv": "^0.4.2", 25 | "base-config": "^0.5.2" 26 | }, 27 | "devDependencies": { 28 | "ansi-green": "^0.1.1", 29 | "base": "^0.8.1", 30 | "base-option": "^0.7.0", 31 | "base-pkg": "^0.2.0", 32 | "base-plugins": "^0.4.10", 33 | "base-store": "^0.4.2", 34 | "expand-args": "^0.4.1", 35 | "gulp": "^3.9.1", 36 | "gulp-eslint": "^2.0.0", 37 | "gulp-format-md": "^0.1.7", 38 | "gulp-istanbul": "^0.10.4", 39 | "gulp-mocha": "^2.2.0", 40 | "minimist": "^1.2.0", 41 | "mocha": "^2.4.5", 42 | "through2": "^2.0.1" 43 | }, 44 | "keywords": [ 45 | "app", 46 | "application", 47 | "arg", 48 | "args", 49 | "base", 50 | "baseplugin", 51 | "cache", 52 | "cli", 53 | "command", 54 | "command-line", 55 | "config", 56 | "data", 57 | "extend", 58 | "flag", 59 | "flags", 60 | "line", 61 | "merge", 62 | "method", 63 | "methods", 64 | "option", 65 | "options", 66 | "opts", 67 | "plugin", 68 | "store", 69 | "terminal" 70 | ], 71 | "verb": { 72 | "run": true, 73 | "toc": false, 74 | "layout": "default", 75 | "tasks": [ 76 | "readme" 77 | ], 78 | "plugins": [ 79 | "gulp-format-md" 80 | ], 81 | "related": { 82 | "list": [ 83 | "base", 84 | "base-config", 85 | "base-data", 86 | "base-generators", 87 | "base-plugins", 88 | "base-task" 89 | ], 90 | "highlight": "base-config", 91 | "description": "Other useful [base] plugins:" 92 | }, 93 | "reflinks": [ 94 | "assemble", 95 | "base", 96 | "base-runner", 97 | "generate", 98 | "verb" 99 | ], 100 | "lint": { 101 | "reflinks": true 102 | } 103 | }, 104 | "lintDeps": { 105 | "ignore": [ 106 | "examples" 107 | ] 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /test/command.cwd.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var path = require('path'); 5 | var assert = require('assert'); 6 | var base = require('base'); 7 | var pkg = require('base-pkg'); 8 | var option = require('base-option'); 9 | var plugins = require('base-plugins'); 10 | var expandArgs = require('expand-args'); 11 | var minimist = require('minimist'); 12 | var cli = require('..'); 13 | var app; 14 | 15 | function expand(argv, opts) { 16 | return expandArgs(minimist(argv, opts), opts); 17 | } 18 | 19 | describe('cli commands', function() { 20 | beforeEach(function() { 21 | app = base(); 22 | app.use(pkg()); 23 | app.use(option()); 24 | app.use(plugins()); 25 | app.use(cli()); 26 | 27 | app.cli.map('cwd', function(val, key, config, next) { 28 | val = config[key] = path.resolve(val); 29 | if (app.option) { 30 | app.option('cwd', val); 31 | } else { 32 | app.options.cwd = val; 33 | } 34 | app.cwd = val; 35 | next(); 36 | }); 37 | }); 38 | 39 | describe('--cwd', function() { 40 | it('should update `app.cwd` with the given cwd', function(cb) { 41 | var args = expand(['--cwd=test/fixtures']); 42 | 43 | app.cli.process(args, function(err) { 44 | if (err) return cb(err); 45 | assert.equal(app.cwd, path.resolve(__dirname, 'fixtures')); 46 | cb(); 47 | }); 48 | }); 49 | 50 | it('should set a cwd on app.options', function(cb) { 51 | var count = 0; 52 | 53 | app.on('option', function(key, val) { 54 | assert.equal(key, 'cwd'); 55 | assert.equal(val, process.cwd()); 56 | count++; 57 | }); 58 | 59 | app.cli.process({cwd: process.cwd()}, function(err) { 60 | if (err) return cb(err); 61 | assert.equal(count, 1); 62 | cb(); 63 | }); 64 | }); 65 | 66 | it('should update `app.options.cwd` with the given cwd', function(cb) { 67 | var args = expand(['--cwd=test/fixtures']); 68 | 69 | app.cli.process(args, function(err) { 70 | if (err) return cb(err); 71 | assert.equal(app.options.cwd, path.resolve(__dirname, 'fixtures')); 72 | cb(); 73 | }); 74 | }); 75 | }); 76 | }); 77 | -------------------------------------------------------------------------------- /test/command.get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var path = require('path'); 5 | var assert = require('assert'); 6 | var base = require('base'); 7 | var plugins = require('base-plugins'); 8 | var expandArgs = require('expand-args'); 9 | var minimist = require('minimist'); 10 | var cli = require('..'); 11 | var app; 12 | 13 | function expand(argv, opts) { 14 | return expandArgs(minimist(argv, opts), opts); 15 | } 16 | 17 | describe('--get', function() { 18 | beforeEach(function() { 19 | app = base(); 20 | app.use(plugins()); 21 | app.use(cli()); 22 | }); 23 | 24 | describe('get', function() { 25 | it('should emit a get event', function(cb) { 26 | app.cli.map('get', function(val, key, config, next) { 27 | app.get(val); 28 | next(); 29 | }); 30 | 31 | var argv = expand(['--get=a']); 32 | app.set('a', 'b'); 33 | var count = 0; 34 | 35 | app.on('get', function(key, val) { 36 | assert(key); 37 | assert(val); 38 | assert.equal(key, 'a'); 39 | assert.equal(val, 'b'); 40 | count++; 41 | }); 42 | 43 | app.cli.process(argv, function(err) { 44 | if (err) return cb(err); 45 | assert.equal(count, 1); 46 | cb(); 47 | }); 48 | }); 49 | 50 | it('should emit multiple get events', function(cb) { 51 | app.cli.map('set', function(val, key, config, next) { 52 | app.set(val); 53 | next(); 54 | }); 55 | app.cli.map('get', function(val, key, config, next) { 56 | if (Array.isArray(val)) { 57 | val.forEach(app.get.bind(app)); 58 | } else { 59 | app.get(val); 60 | } 61 | next(); 62 | }); 63 | 64 | var argv = expand(['--get=a,b,c']); 65 | app.set('a', 'aaa'); 66 | app.set('b', 'bbb'); 67 | app.set('c', 'ccc'); 68 | var keys = []; 69 | 70 | app.on('get', function(key, val) { 71 | if (key === 'a') assert.equal(val, 'aaa'); 72 | if (key === 'b') assert.equal(val, 'bbb'); 73 | if (key === 'c') assert.equal(val, 'ccc'); 74 | keys.push(key); 75 | }); 76 | 77 | app.cli.process(argv, function(err) { 78 | if (err) return cb(err); 79 | assert.equal(keys.length, 3); 80 | cb(); 81 | }); 82 | }); 83 | }); 84 | }); 85 | -------------------------------------------------------------------------------- /test/command.has.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var path = require('path'); 5 | var assert = require('assert'); 6 | var base = require('base'); 7 | var plugins = require('base-plugins'); 8 | var expandArgs = require('expand-args'); 9 | var minimist = require('minimist'); 10 | var cli = require('..'); 11 | var app; 12 | 13 | function expand(argv, opts) { 14 | return expandArgs(minimist(argv, opts), opts); 15 | } 16 | 17 | describe('--has', function() { 18 | beforeEach(function() { 19 | app = base(); 20 | app.use(plugins()); 21 | app.use(cli()); 22 | }); 23 | 24 | describe('has', function() { 25 | it('should emit a has event', function(cb) { 26 | app.cli.map('has', function(val, key, config, next) { 27 | app.has(val); 28 | next(); 29 | }); 30 | 31 | var count = 0; 32 | var argv = expand(['--has=a']); 33 | app.set('a', 'b'); 34 | 35 | app.on('has', function(key, val) { 36 | assert.equal(key, 'a'); 37 | assert.equal(val, true); 38 | count++; 39 | }); 40 | 41 | app.cli.process(argv, function(err) { 42 | if (err) return cb(err); 43 | assert.equal(count, 1); 44 | cb(); 45 | }); 46 | }); 47 | 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /test/command.store.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var path = require('path'); 5 | var assert = require('assert'); 6 | var base = require('base'); 7 | var option = require('base-option'); 8 | var store = require('base-store'); 9 | var plugins = require('base-plugins'); 10 | var expandArgs = require('expand-args'); 11 | var minimist = require('minimist'); 12 | var pkg = require('base-pkg'); 13 | var cli = require('..'); 14 | var app; 15 | 16 | function expand(argv, opts) { 17 | return expandArgs(minimist(argv, opts), opts); 18 | } 19 | 20 | describe('--store', function() { 21 | describe('store.map', function() { 22 | beforeEach(function() { 23 | app = base(); 24 | app.use(pkg()); 25 | app.use(plugins()); 26 | app.use(option()); 27 | app.use(store('base-cli-tests')); 28 | app.use(cli()); 29 | }); 30 | 31 | it('should expose `store.cli', function() { 32 | assert(app.store.cli); 33 | assert.equal(typeof app.store.cli, 'function'); 34 | }); 35 | 36 | it('should not blow up if store plugin is not used', function() { 37 | var foo = base(); 38 | foo.use(cli()); 39 | assert.equal(typeof foo.store, 'undefined'); 40 | }); 41 | 42 | it('should add properties to app.store.cli.config', function(cb) { 43 | app.store.cli.map('set', function(val, key, config, next) { 44 | app.store.set(val); 45 | next(); 46 | }); 47 | app.store.cli.map('get', function(val, key, config, next) { 48 | app.store.get(val); 49 | next(); 50 | }); 51 | app.store.cli.alias('foo', 'set'); 52 | app.store.cli.alias('bar', 'get'); 53 | var called = 0; 54 | 55 | app.store.on('set', function(key, val) { 56 | assert(key); 57 | assert.equal(key, 'a'); 58 | assert.equal(val, 'b'); 59 | called++; 60 | }); 61 | 62 | app.store.on('get', function(key, val) { 63 | assert(key); 64 | assert.equal(key, 'a'); 65 | assert.equal(val, 'b'); 66 | called++; 67 | }); 68 | 69 | app.store.cli.process({foo: {a: 'b'}, bar: 'a'}, function(err) { 70 | if (err) return cb(err); 71 | 72 | assert.equal(called, 2); 73 | cb(); 74 | }); 75 | }); 76 | 77 | it('should work as a function', function(cb) { 78 | app.store.cli.map('set', function(val, key, config, next) { 79 | app.store.set(val); 80 | next(); 81 | }); 82 | app.store.cli.map('get', function(val, key, config, next) { 83 | app.store.get(val); 84 | next(); 85 | }); 86 | 87 | app.store.cli({ 88 | foo: 'set', 89 | bar: 'get' 90 | }); 91 | 92 | var called = 0; 93 | 94 | app.store.on('set', function(key, val) { 95 | assert(key); 96 | assert.equal(key, 'a'); 97 | assert.equal(val, 'b'); 98 | called++; 99 | }); 100 | 101 | app.store.on('get', function(key, val) { 102 | assert(key); 103 | assert.equal(key, 'a'); 104 | assert.equal(val, 'b'); 105 | called++; 106 | }); 107 | 108 | app.store.cli.process({foo: {a: 'b'}, bar: 'a'}, function(err) { 109 | if (err) return cb(err); 110 | 111 | assert.equal(called, 2); 112 | cb(); 113 | }); 114 | }); 115 | }); 116 | }); 117 | -------------------------------------------------------------------------------- /test/command.use.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var path = require('path'); 5 | var assert = require('assert'); 6 | var base = require('base'); 7 | var store = require('base-store'); 8 | var option = require('base-option'); 9 | var plugins = require('base-plugins'); 10 | var expandArgs = require('expand-args'); 11 | var minimist = require('minimist'); 12 | var cli = require('..'); 13 | var app; 14 | 15 | function expand(argv, opts) { 16 | return expandArgs(minimist(argv, opts), opts); 17 | } 18 | 19 | describe('--use', function() { 20 | beforeEach(function(cb) { 21 | app = base(); 22 | app.cwd = app.cwd || process.cwd(); 23 | app.use(plugins()); 24 | app.use(option()); 25 | app.use(store('base-cli-tests')); 26 | app.use(cli()); 27 | app.cli.map('use', function(val, key, config, next) { 28 | if (typeof val === 'string') { 29 | val = val.split(','); 30 | } 31 | 32 | if (Array.isArray(val)) { 33 | val.forEach(function(ele) { 34 | var fp = path.resolve(config.cwd || app.cwd, ele); 35 | var fn = require(fp); 36 | app.use(fn); 37 | }); 38 | } 39 | next(); 40 | }); 41 | cb(); 42 | }); 43 | 44 | it('should use a plugin', function(cb) { 45 | var count = 0; 46 | app.once('use', function() { 47 | count++ 48 | }); 49 | 50 | app.cli.process({use: 'test/fixtures/plugins/a'}, function(err) { 51 | if (err) return cb(err); 52 | assert.equal(count, 1); 53 | cb(); 54 | }); 55 | }); 56 | 57 | it('should use a plugin from a cwd', function(cb) { 58 | var count = 0; 59 | app.once('use', function(key, val) { 60 | count++; 61 | }); 62 | 63 | app.cli.process({ 64 | cwd: 'test/fixtures/plugins', 65 | use: 'a' 66 | }, function(err) { 67 | if (err) return cb(err); 68 | assert.equal(count, 1); 69 | cb(); 70 | }); 71 | }); 72 | 73 | it('should use an array of plugins from a cwd', function(cb) { 74 | var n = 0; 75 | app.on('use', function() { 76 | n++; 77 | }); 78 | 79 | app.cli.process({ 80 | cwd: 'test/fixtures/plugins', 81 | use: 'a,b,c' 82 | }, function(err) { 83 | if (err) return cb(err); 84 | assert.equal(n, 3); 85 | cb(); 86 | }); 87 | }); 88 | }); 89 | -------------------------------------------------------------------------------- /test/fixtures/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "foo", 3 | "private": true 4 | } -------------------------------------------------------------------------------- /test/fixtures/plugins/a.js: -------------------------------------------------------------------------------- 1 | module.exports = function (app) { 2 | console.log('plugin A'); 3 | app.set('a', 'AAA'); 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/plugins/b.js: -------------------------------------------------------------------------------- 1 | module.exports = function (app) { 2 | console.log('plugin B'); 3 | app.set('b', 'BBB'); 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/plugins/c.js: -------------------------------------------------------------------------------- 1 | module.exports = function (app) { 2 | console.log('plugin C'); 3 | app.set('c', 'CCC'); 4 | }; 5 | -------------------------------------------------------------------------------- /verbfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var through = require('through2'); 4 | var green = require('ansi-green'); 5 | 6 | module.exports = function(app, base) { 7 | app.extendWith('verb-readme-generator'); 8 | 9 | app.plugin('toFlag', function(options) { 10 | return through.obj(function(file, enc, next) { 11 | var str = file.contents.toString(); 12 | str = str.replace(/^(#+ \[)\./gm, '$1--'); 13 | console.log(green(' ✔'), 'plugin toFlag: converted titles to flags'); 14 | file.contents = new Buffer(str); 15 | next(null, file); 16 | }); 17 | }); 18 | 19 | app.task('default', ['readme']); 20 | }; 21 | 22 | /** 23 | * Function to be called before the generator is invoked. 24 | * Experimental! not implemented yet! 25 | */ 26 | 27 | module.exports.setup = function(Base, base, ctx) { 28 | Base.on('runner', function(event, ctx) { 29 | console.log(event, ctx); 30 | }); 31 | base.on('generator', function(name) { 32 | console.log(name); 33 | }); 34 | base.files('foo', {content: 'whatever'}); 35 | }; 36 | --------------------------------------------------------------------------------