Socket
Socket
Sign inDemoInstall

base-argv

Package Overview
Dependencies
39
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.0 to 0.4.0

utils.js

209

index.js

@@ -10,122 +10,149 @@ /*!

var minimist = require('minimist');
var toTasks = require('./lib/to-tasks');
var utils = require('./lib/utils');
var utils = require('./utils');
module.exports = function(options) {
return function(app) {
var opts = utils.extend({}, options);
if (opts.prop) {
opts[opts.prop] = app[opts.prop];
}
module.exports = function(config) {
return function() {
if (this.isRegistered('base-argv')) return;
opts.commands = app.commands || [];
this.define('argv', function(argv, options) {
var orig = Array.isArray(argv) ? argv.slice() : utils.extend({}, argv);
var opts = utils.extend({}, config, this.options, options, argv);
var args = processArgv(this, argv, opts);
if (args.expand === 'false' || opts.expand === false) {
delete args.expand;
return args;
}
// add `keys` from base-config plugin (map-config)
if (app.config && Array.isArray(app.config.keys)) {
opts.commands = utils.union(opts.commands, app.config.keys);
}
// add `keys` from base-cli plugin (map-config)
if (app.cli && Array.isArray(app.cli.keys)) {
opts.commands = utils.union(opts.commands, app.cli.keys);
}
opts.tasks = app.tasks;
app.define('processArgv', processArgv(opts));
utils.define(args, 'orig', orig);
return args;
});
};
};
function processArgv(options) {
/**
* Expand command line arguments into the format we need to pass
* to `app.cli.process()`.
*
* Add a `default` task is added to the `tasks` array if no tasks
* were defined, and only whitelisted flags are passed.
*
* @param {Object} `app` Application instance
* @param {Object} `argv` argv object, parsed by minimist
* @param {Array} `options.first` The keys to pass to `app.cli.process()` first.
* @param {Array} `options.last` The keys to pass to `app.cli.process()` last.
* @param {Array} `options.keys` Flags to whitelist
* @return {Object} Returns the `argv` object with sorted keys.
*/
function processArgv(app, argv, options) {
var opts = utils.extend({}, options);
var commands = opts.commands;
var tasks = opts.tasks;
var prop = opts.prop;
return function(argv) {
argv = !utils.isObject(argv)
? minimist(utils.arrayify(argv))
: argv;
if (Array.isArray(argv)) {
argv = utils.minimist(argv, options);
}
var res = {};
res.orig = utils.extend({}, argv);
res._ = [];
res.tasks = [];
res.commands = {};
res.options = {};
if (prop) res[prop] = [];
if (opts.expand === false || argv.expand === 'false') {
return argv;
}
var appKeys = (prop && opts.hasOwnProperty(prop))
? Object.keys(opts[prop])
: [];
// shallow clone parsed args from minimist
var parsed = utils.extend({}, argv);
var taskKeys = !Array.isArray(tasks)
? (tasks ? Object.keys(tasks) : [])
: tasks;
// move the splat array
var tasks = argv._;
delete argv._;
var arr = argv._;
var len = arr.length, i = -1;
var keys = Object.keys(argv);
var len = keys.length;
while (++i < len) {
var ele = arr[i];
// expand args with "expand-args"
argv = utils.expandArgs(argv);
if (/,/.test(ele) && !/[.|:]/.test(ele)) {
res.tasks = utils.union(res.tasks, ele.split(','));
continue;
}
// union array tasks with tasks passed with `--tasks` flag
argv.tasks = utils.union(utils.arrayify(argv.tasks), tasks);
if (isApp(appKeys, ele) || isTask(taskKeys, ele)) {
if (prop) {
var obj = toTasks(ele, opts, prop);
res[prop] = res[prop].concat(obj);
} else {
res.tasks = utils.union(res.tasks, ele.split(','));
}
continue;
}
res._.push(ele);
// allow a default task to be set when only whitelisted flags
// are passed (and no other tasks are defined)
var whitelist = utils.arrayify(opts.whitelist);
var wlen = whitelist.length;
while (wlen--) {
var ele = whitelist[wlen];
if (argv.hasOwnProperty(ele)) {
len--;
}
}
for (var key in argv) {
if (key === '_') {
continue;
}
if ((len === 0 || argv.run) && argv.tasks.length === 0) {
argv.tasks = ['default'];
}
var argvArr = utils.arrayify(argv[key]);
var expanded = utils.expandArgs(argvArr)[0];
if (argv.tasks.length === 0 && len > 0) {
delete argv.tasks;
}
if (isCommand(commands, key)) {
res.commands[key] = expanded;
} else {
res.options[key] = expanded;
}
}
return res;
};
var res = sortArgs(app, argv, options);
utils.define(res, 'minimist', parsed);
return res;
}
module.exports.processArgv = processArgv;
/**
* Utils
* Sort arguments so that `app.cli.process` executes commands
* in the order specified.
*
* @param {Object} `app` Application instance
* @param {Object} `argv` The expanded argv object
* @param {Object} `options`
* @param {Array} `options.first` The keys to run first.
* @param {Array} `options.last` The keys to run last.
* @return {Object} Returns the `argv` object with sorted keys.
*/
function isApp(apps, key) {
return is(apps, key);
}
function sortArgs(app, argv, options) {
options = options || [];
function isCommand(commands, key) {
return is(commands, key);
}
var first = options.first || [];
var last = options.last || [];
var cliKeys = [];
function isTask(tasks, key) {
return is(tasks, key);
if (app.cli && app.cli.keys) {
cliKeys = app.cli.keys;
}
var keys = utils.union(first, cliKeys, Object.keys(argv));
keys = utils.diff(keys, last);
keys = utils.union(keys, last);
var len = keys.length;
var idx = -1;
var res = {};
while (++idx < len) {
var key = keys[idx];
if (argv.hasOwnProperty(key)) {
res[key] = argv[key];
}
}
return toBoolean(res);
}
function is(arr, key, ch) {
if (!arr) return false;
if (arr.indexOf(key) > -1) {
return true;
function toBoolean(argv) {
var special = ['set', 'option', 'options', 'data'];
for (var key in argv) {
var val = argv[key];
if (~special.indexOf(key) && typeof val === 'string') {
var obj = {};
var isFalse = val.indexOf('no') === 0;
if (isFalse) {
val = val.slice(2);
obj[val] = false;
} else {
obj[val] = true;
}
val = obj;
}
argv[key] = val;
}
var seg = key.split(/\W/).shift();
return arr.indexOf(seg) > -1;
return argv;
}
{
"name": "base-argv",
"description": "Plugin for base-methods that simplifies mapping argv arguments to tasks, commands, and options",
"version": "0.3.0",
"description": "Plugin that post-processes the argv object from simplify how args are mapped to options, tasks and generators.",
"version": "0.4.0",
"homepage": "https://github.com/jonschlinkert/base-argv",

@@ -14,3 +14,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"index.js",
"lib/"
"utils.js"
],

@@ -25,14 +25,15 @@ "main": "index.js",

"dependencies": {
"arr-union": "^3.0.0",
"arrayify-compact": "^0.2.0",
"expand-args": "^0.3.0",
"arr-diff": "^2.0.0",
"array-union": "^1.0.1",
"define-property": "^0.2.5",
"expand-args": "^0.3.3",
"extend-shallow": "^2.0.1",
"isobject": "^2.0.0",
"lazy-cache": "^0.2.4",
"minimist": "^1.2.0"
"lazy-cache": "^1.0.3"
},
"devDependencies": {
"base-config": "^0.3.2",
"base-methods": "^0.6.1",
"base-tasks": "^0.1.2",
"base": "^0.6.7",
"base-cli": "^0.4.1",
"base-tasks": "^0.3.0",
"gulp-format-md": "^0.1.5",
"minimist": "^1.2.0",
"mocha": "*"

@@ -57,10 +58,15 @@ },

"list": [
"base-cli",
"base-config",
"base-methods",
"base-plugins",
"base-config",
"base-cli",
"base-options"
"base-options",
"base-plugins"
]
}
},
"toc": false,
"layout": "default",
"plugins": [
"gulp-format-md"
]
}
}

@@ -1,8 +0,9 @@

# base-argv [![NPM version](https://img.shields.io/npm/v/base-argv.svg)](https://www.npmjs.com/package/base-argv) [![Build Status](https://img.shields.io/travis/jonschlinkert/base-argv.svg)](https://travis-ci.org/jonschlinkert/base-argv)
# base-argv
> Plugin for base-methods that simplifies mapping argv arguments to tasks, commands, and options
> Plugin that post-processes the argv object from simplify how args are mapped to options, tasks and generators.
## Install
Install with [npm](https://www.npmjs.com/)
Install with [npm](https://www.npmjs.com/):
```sh

@@ -13,2 +14,3 @@ $ npm i base-argv --save

## Usage
```js

@@ -42,17 +44,28 @@ var Base = require('base-methods');

```js
{ orig: { _: [ 'foo', 'bar' ], set: 'a:b' },
_: [ 'bar' ],
tasks: ['foo'],
commands: { set: { a: 'b' } },
options: {} }
{ set: { a: 'b' }, tasks: [ 'foo', 'bar' ] }
```
Additionally, the original `argv` object and the parsed minimist args are expose on non-enumerable properties, `orig` and `minimist` respectively.
**Example**
```js
{ set: { a: 'b' },
tasks: [ 'foo', 'bar' ],
// non-enumerable
minimist: { _: [ 'foo', 'bar' ], set: 'a:b' },
// non-enumerable
orig: [ 'foo', 'bar', '--set=a:b' ] }
```
## Related projects
* [base-cli](https://www.npmjs.com/package/base-cli): Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a… [more](https://www.npmjs.com/package/base-cli) | [homepage](https://github.com/jonschlinkert/base-cli)
* [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/jonschlinkert/base-config)
* [base-methods](https://www.npmjs.com/package/base-methods): Starter for creating a node.js application with a handful of common methods, like `set`, `get`,… [more](https://www.npmjs.com/package/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods)
* [base-methods](https://www.npmjs.com/package/base-methods): base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://www.npmjs.com/package/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods)
* [base-options](https://www.npmjs.com/package/base-options): Adds a few options methods to base-methods, like `option`, `enable` and `disable`. See the readme… [more](https://www.npmjs.com/package/base-options) | [homepage](https://github.com/jonschlinkert/base-options)
* [base-plugins](https://www.npmjs.com/package/base-plugins): Upgrade's plugin support in base-methods to allow plugins to be called any time after init. | [homepage](https://github.com/jonschlinkert/base-plugins)
* [base-plugins](https://www.npmjs.com/package/base-plugins): Upgrade's plugin support in base-methods to allow plugins to be called any time after init. | [homepage](https://github.com/jonschlinkert/base-plugins)
## Running tests
Install dev dependencies:

@@ -65,12 +78,15 @@

## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/base-argv/issues/new).
## Author
**Jon Schlinkert**
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
Copyright © 2015-2016 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the MIT license.

@@ -80,16 +96,2 @@

_This file was generated by [verb](https://github.com/verbose/verb) on Tue Dec 01 2015 12:01:27 GMT-0500 (EST)._
[arr-union]: https://github.com/jonschlinkert/arr-union
[arrayify-compact]: https://github.com/jonschlinkert/arrayify-compact
[base-config]: https://github.com/jonschlinkert/base-config
[base-methods]: https://github.com/jonschlinkert/base-methods
[base-tasks]: https://github.com/jonschlinkert/base-tasks
[expand-args]: https://github.com/jonschlinkert/expand-args
[extend-shallow]: https://github.com/jonschlinkert/extend-shallow
[generate]: https://github.com/generate/generate
[isobject]: https://github.com/jonschlinkert/isobject
[lazy-cache]: https://github.com/jonschlinkert/lazy-cache
[minimist]: https://github.com/substack/minimist
[mocha]: https://github.com/mochajs/mocha
[verb]: https://github.com/verbose/verb
_This file was generated by [verb](https://github.com/verbose/verb) on January 29, 2016._

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc