Skip to content

Commit

Permalink
New: add supporting $npm_config_xxx (fixes #60)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Aug 31, 2016
1 parent a32ca2f commit 09507bc
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/bin/common/parse-cli-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const assign = require("object-assign")
//------------------------------------------------------------------------------

const OVERWRITE_OPTION = /^--([^:]+?):([^=]+?)(?:=(.+))?$/
const CONFIG_PATTERN = /^npm_package_config_(.+)$/
const CONFIG_OPTION = /^--([^=]+?)(?:=(.+))$/
const PACKAGE_CONFIG_PATTERN = /^npm_package_config_(.+)$/
const CONCAT_OPTIONS = /^-[clnprs]+$/

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ function createPackageConfig() {
}

Object.keys(process.env).forEach(key => {
const m = CONFIG_PATTERN.exec(key)
const m = PACKAGE_CONFIG_PATTERN.exec(key)
if (m != null) {
overwriteConfig(retv, packageName, m[1], process.env[key])
}
Expand Down Expand Up @@ -91,6 +92,7 @@ class ArgumentSet {
this.silent = process.env.npm_config_loglevel === "silent"
this.singleMode = Boolean(options.singleMode)
this.packageConfig = createPackageConfig()
this.config = {}

addGroup(this.groups, initialValues)
}
Expand Down Expand Up @@ -178,15 +180,18 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
break

default: {
const matched = OVERWRITE_OPTION.exec(arg)
if (matched) {
let matched = null
if ((matched = OVERWRITE_OPTION.exec(arg))) {
overwriteConfig(
set.packageConfig,
matched[1],
matched[2],
matched[3] || args[++i]
)
}
else if ((matched = CONFIG_OPTION.exec(arg))) {
set.config[matched[1]] = matched[2]
}
else if (CONCAT_OPTIONS.test(arg)) {
parseCLIArgsCore(
set,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/npm-run-all/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
const {
continueOnError,
groups,
config,
packageConfig,
printLabel,
printName,
Expand All @@ -55,6 +56,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
continueOnError,
printLabel,
printName,
config,
packageConfig,
silent,
arguments: rest,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/run-p/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
const {
lastGroup: {patterns, parallel},
continueOnError,
config,
packageConfig,
printLabel,
printName,
Expand All @@ -54,6 +55,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
continueOnError,
printLabel,
printName,
config,
packageConfig,
silent,
arguments: rest,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/run-s/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
const {
lastGroup: {patterns, parallel},
continueOnError,
config,
packageConfig,
printLabel,
printName,
Expand All @@ -53,6 +54,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
continueOnError,
printLabel,
printName,
config,
packageConfig,
silent,
arguments: rest,
Expand Down
15 changes: 15 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ function toOverwriteOptions(config) {
return options
}

/**
* Converts a given config object to an `--a=b` style option array.
*
* @param {object|null} config -
* A map-like object to set configs.
* @returns {string[]} `--a=b` style options.
*/
function toConfigOptions(config) {
return Object.keys(config).map(key => `--${key}=${config[key]}`)
}

/**
* Gets the maximum length.
*
Expand Down Expand Up @@ -203,6 +214,7 @@ module.exports = function npmRunAll(
stdout = null,
stderr = null,
taskList = null,
config = null,
packageConfig = null,
silent = false,
continueOnError = false,
Expand All @@ -229,6 +241,9 @@ module.exports = function npmRunAll(
if (packageConfig != null) {
prefixOptions.push(...toOverwriteOptions(packageConfig))
}
if (config != null) {
prefixOptions.push(...toConfigOptions(config))
}

return Promise.resolve(taskList)
.then(taskList => { // eslint-disable-line no-shadow
Expand Down
3 changes: 3 additions & 0 deletions test-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"test-task:package-config": "node tasks/package-config1.js",
"test-task:package-config2": "node tasks/package-config2.js",
"test-task:nested-package-config": "babel-node ../src/bin/npm-run-all/index.js test-task:package-config",
"test-task:config": "node tasks/config1.js",
"test-task:config2": "node tasks/config2.js",
"test-task:nested-config": "babel-node ../src/bin/npm-run-all/index.js test-task:config",
"test-task:append": "node tasks/append2.js",
"test-task:append:a": "node tasks/append2.js a",
"test-task:append:a:c": "node tasks/append2.js ac",
Expand Down
4 changes: 4 additions & 0 deletions test-workspace/tasks/config1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict"

var appendResult = require("./lib/util").appendResult
appendResult(String(process.env.npm_config_test))
4 changes: 4 additions & 0 deletions test-workspace/tasks/config2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict"

var appendResult = require("./lib/util").appendResult
appendResult(process.env.npm_config_test + "\n" + process.env.npm_config_test2 + "\n" + process.env.npm_config_test3)
113 changes: 113 additions & 0 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const assert = require("power-assert")
const {result, removeResult} = require("./lib/util")

// Test targets.
const nodeApi = require("../src/lib")
const runAll = require("../src/bin/npm-run-all")
const runSeq = require("../src/bin/run-s")
const runPar = require("../src/bin/run-p")

//------------------------------------------------------------------------------
// Test
//------------------------------------------------------------------------------

describe("[config] it should have an ability to set config variables:", () => {
before(() => process.chdir("test-workspace"))
after(() => process.chdir(".."))

beforeEach(removeResult)

it("Node API should address \"config\" option", () =>
nodeApi("test-task:config", {config: {test: "this is a config"}})
.then(() => {
assert(result() === "this is a config")
})
)

it("Node API should address \"config\" option for multiple variables", () =>
nodeApi("test-task:config2", {config: {test: "1", test2: "2", test3: "3"}})
.then(() => {
assert(result() === "1\n2\n3")
})
)

describe("CLI commands should address \"--a=b\" style options", () => {
it("npm-run-all command", () =>
runAll(["test-task:config", "--test=GO"])
.then(() => {
assert(result() === "GO")
})
)

it("run-s command", () =>
runSeq(["test-task:config", "--test=GO"])
.then(() => {
assert(result() === "GO")
})
)

it("run-p command", () =>
runPar(["test-task:config", "--test=GO"])
.then(() => {
assert(result() === "GO")
})
)
})

describe("CLI commands should address \"--b=c\" style options for multiple variables", () => {
it("npm-run-all command", () =>
runAll(["test-task:config2", "--test=1", "--test2=2", "--test3=3"])
.then(() => {
assert(result() === "1\n2\n3")
})
)

it("run-s command", () =>
runSeq(["test-task:config2", "--test=1", "--test2=2", "--test3=3"])
.then(() => {
assert(result() === "1\n2\n3")
})
)

it("run-p command", () =>
runPar(["test-task:config2", "--test=1", "--test2=2", "--test3=3"])
.then(() => {
assert(result() === "1\n2\n3")
})
)
})

describe("CLI commands should transfar configs to nested commands.", () => {
it("npm-run-all command", () =>
runAll(["test-task:nested-config", "--test=GO DEEP"])
.then(() => {
assert(result() === "GO DEEP")
})
)

it("run-s command", () =>
runSeq(["test-task:nested-config", "--test=GO DEEP"])
.then(() => {
assert(result() === "GO DEEP")
})
)

it("run-p command", () =>
runPar(["test-task:nested-config", "--test=GO DEEP"])
.then(() => {
assert(result() === "GO DEEP")
})
)
})
})

0 comments on commit 09507bc

Please sign in to comment.