Comparing version 2.3.3 to 2.4.0
History | ||
======= | ||
## 2.4.0 | ||
* Add `builder <action> <task> [-- <args>...]` support. | ||
[builder-react-component#27](https://github.com/FormidableLabs/builder-react-component/issues/27) | ||
## 2.3.3 | ||
@@ -5,0 +10,0 @@ |
@@ -104,2 +104,14 @@ "use strict"; | ||
// Capture any flags after `--` like `npm run <task> -- <args>` does. | ||
// See: https://docs.npmjs.com/cli/run-script#description | ||
var customArgs = []; | ||
var customIdx = argv.indexOf("--"); | ||
if (customIdx > -1) { | ||
// Update custom args. | ||
customArgs = argv.slice(customIdx + 1); | ||
// Remove custom args from input. | ||
argv = argv.slice(0, customIdx); | ||
} | ||
// Parse. | ||
@@ -119,6 +131,8 @@ var parsedOpts = nopt(opts, {}, argv); | ||
// Camel-case flags. | ||
return _.mapKeys(parsedOpts, function (val, key) { | ||
return _.camelCase(key); | ||
}); | ||
return _(parsedOpts) | ||
// Camel-case flags. | ||
.mapKeys(function (val, key) { return _.camelCase(key); }) | ||
// Add in custom flags if found earlier. | ||
.merge(customArgs.length > 0 ? { _customArgs: customArgs } : {}) | ||
.value(); | ||
}; | ||
@@ -125,0 +139,0 @@ }; |
@@ -8,2 +8,3 @@ "use strict"; | ||
var chalk = require("chalk"); | ||
var argvSplit = require("argv-split"); | ||
var log = require("./log"); | ||
@@ -32,2 +33,38 @@ var Tracker = require("./utils/tracker"); | ||
// Helper for merging in custom options. | ||
var cmdWithCustom = function (cmd, opts) { | ||
opts = opts || {}; | ||
var customArgs = (opts || {})._customArgs || []; | ||
// Base case: No custom arguments to add. | ||
if (customArgs.length === 0) { | ||
return cmd; | ||
} | ||
// Scenario: A base command may have `--` already like `foo -- --bar` which | ||
// we need to add to. The hard part is the command may alternately be | ||
// something perverse like: `foo "totally -- not extra args"` where we need | ||
// to add the `--` to. | ||
// | ||
// This means _parsing_ a full command string, which we've tried to avoid | ||
// doing. So, current library of choice is: | ||
// - https://github.com/kaelzhang/node-argv-split | ||
// | ||
// Other working candidates: | ||
// - https://github.com/gabrieleds/node-argv (brings in `minimist` too) | ||
// | ||
// All of these libraries are a bit wonky / incomplete, so we only _detect_ | ||
// if `--` is pre-existing before appending to the existing command. But, | ||
// for safety we don't mutate the original command (besides appending). | ||
var parsed = argvSplit(cmd); | ||
var haveCustom = parsed.indexOf("--") > 0; | ||
// Only add the `--` token if _not_ already there and _is_ a builder task. | ||
var isBuilderTask = opts._isBuilderTask === true; | ||
var addCustomToken = isBuilderTask && !haveCustom; | ||
// Add in the custom args with/without `--` token. | ||
return cmd + (addCustomToken ? " -- " : " ") + customArgs.join(" "); | ||
}; | ||
/** | ||
@@ -43,2 +80,4 @@ * Run a single task. | ||
var run = function (cmd, shOpts, opts, callback) { | ||
cmd = cmdWithCustom(cmd, opts); | ||
// Update shell options. | ||
@@ -200,2 +239,5 @@ shOpts = _.extend({ | ||
module.exports = { | ||
// Helpers. | ||
_cmdWithCustom: cmdWithCustom, | ||
/** | ||
@@ -202,0 +244,0 @@ * Run a single task. |
@@ -59,2 +59,17 @@ "use strict"; | ||
/** | ||
* Is this task another builder command? | ||
* | ||
* @param {String} task Task | ||
* @returns {Boolean} Is this task a passthrough? | ||
*/ | ||
Task.prototype.isBuilderTask = function (task) { | ||
var builder = path.basename(this._script); | ||
var taskParts = task.split(/\s+/); | ||
var taskBin = taskParts[0]; | ||
// Note: Assumes a binary script match without `.js` extension. | ||
return builder === taskBin; | ||
}; | ||
/** | ||
* Is this task a simple passthrough to another builder command? | ||
@@ -66,10 +81,7 @@ * | ||
Task.prototype.isPassthrough = function (task) { | ||
var builder = path.basename(this._script); | ||
var taskParts = task.split(/\s+/); | ||
var taskBin = taskParts[0]; | ||
var taskAction = taskParts[1]; | ||
var taskCommand = taskParts[2]; | ||
// Note: Assumes a binary script match without `.js` extension. | ||
return builder === taskBin && | ||
return this.isBuilderTask(task) && | ||
this._action === taskAction && | ||
@@ -101,2 +113,15 @@ this._command === taskCommand; | ||
/** | ||
* Merge base options with custom options. | ||
* | ||
* @param {String} task Task | ||
* @param {Object} opts Custom options | ||
* @returns {Object} Combined options | ||
*/ | ||
Task.prototype.getOpts = function (task, opts) { | ||
return _.extend({ | ||
_isBuilderTask: this.isBuilderTask(task) | ||
}, opts); | ||
}; | ||
/** | ||
* Help. | ||
@@ -155,3 +180,3 @@ * | ||
var flags = args.run(this.argv); | ||
var opts = _.extend({}, flags); | ||
var opts = this.getOpts(task, flags); | ||
@@ -174,3 +199,3 @@ log.info(this._action, this._command + chalk.gray(" - " + task)); | ||
var flags = args.concurrent(this.argv); | ||
var opts = _.extend({}, flags); | ||
var opts = this.getOpts(tasks[0] || "", flags); | ||
@@ -214,3 +239,3 @@ log.info(this._action, cmds.join(", ") + tasks.map(function (t, i) { | ||
var flags = args.envs(this.argv); | ||
var opts = _.extend({}, flags); | ||
var opts = this.getOpts(task, flags); | ||
@@ -217,0 +242,0 @@ // Get task environment array. |
The MIT License (MIT) | ||
Copyright (c) 2015 Formidable Labs | ||
Copyright (c) 2015-2016 Formidable Labs | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "builder", | ||
"version": "2.3.3", | ||
"version": "2.4.0", | ||
"description": "An NPM-based task runner", | ||
@@ -9,3 +9,3 @@ "repository": { | ||
}, | ||
"author": "Ryan Roemer <ryan.roemer@formidablelabs.com>", | ||
"author": "Ryan Roemer <ryan.roemer@formidable.com>", | ||
"license": "MIT", | ||
@@ -21,3 +21,3 @@ "bugs": { | ||
"builder:test": "mocha --opts test/server/mocha.opts test/server/spec", | ||
"builder:test-cov": "istanbul cover --config .istanbul.server.yml _mocha -- --opts test/server/mocha.opts test/server/spec", | ||
"builder:test-cov": "istanbul cover --config .istanbul.server.yml _mocha -- --opts test/server/mocha.opts test/server/spec", | ||
"builder:check": "npm run builder:lint && npm run builder:test", | ||
@@ -30,2 +30,3 @@ "builder:check-ci": "npm run builder:lint && npm run builder:test-cov" | ||
"dependencies": { | ||
"argv-split": "^1.0.0", | ||
"async": "^1.4.2", | ||
@@ -32,0 +33,0 @@ "chalk": "^1.1.1", |
@@ -123,3 +123,2 @@ [![Travis Status][trav_img]][trav_site] | ||
#### Configuration | ||
@@ -223,2 +222,43 @@ | ||
###### Custom Flags | ||
Just like [`npm run <task> [-- <args>...]`](https://docs.npmjs.com/cli/run-script), | ||
flags after a ` -- ` token in a builder task or from the command line are passed | ||
on to the underlying tasks. This is slightly more complicated for builder in | ||
that composed tasks pass on the flags _all the way down_. So, for tasks like: | ||
```js | ||
"scripts": { | ||
"down": "echo down", | ||
"way": "builder run down -- --way", | ||
"the": "builder run way -- --the", | ||
"all": "builder run the -- --all" | ||
} | ||
``` | ||
We can run some basics (alone and with a user-added flag): | ||
```sh | ||
$ builder run down | ||
down | ||
$ builder run down -- --my-custom-flag | ||
down --my-custom-flag | ||
``` | ||
If we run the composed commands, the `--` flags are accumulated: | ||
```sh | ||
$ builder run all | ||
down --way --the --all | ||
$ builder run all -- --my-custom-flag | ||
down --way --the --all --my-custom-flag | ||
``` | ||
The rough heuristic here is if we have custom arguments: | ||
1. If a `builder <action>` command, append with ` -- ` to pass through. | ||
2. If a non-`builder` command, then append without ` -- ` token. | ||
## Tasks | ||
@@ -225,0 +265,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55596
1040
504
7
+ Addedargv-split@^1.0.0
+ Addedargv-split@1.0.1(transitive)