Skip to content

Commit 09507bc

Browse files
committed
New: add supporting $npm_config_xxx (fixes #60)
1 parent a32ca2f commit 09507bc

File tree

9 files changed

+154
-4
lines changed

9 files changed

+154
-4
lines changed

src/bin/common/parse-cli-args.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const assign = require("object-assign")
1818
//------------------------------------------------------------------------------
1919

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

2425
/**
@@ -49,7 +50,7 @@ function createPackageConfig() {
4950
}
5051

5152
Object.keys(process.env).forEach(key => {
52-
const m = CONFIG_PATTERN.exec(key)
53+
const m = PACKAGE_CONFIG_PATTERN.exec(key)
5354
if (m != null) {
5455
overwriteConfig(retv, packageName, m[1], process.env[key])
5556
}
@@ -91,6 +92,7 @@ class ArgumentSet {
9192
this.silent = process.env.npm_config_loglevel === "silent"
9293
this.singleMode = Boolean(options.singleMode)
9394
this.packageConfig = createPackageConfig()
95+
this.config = {}
9496

9597
addGroup(this.groups, initialValues)
9698
}
@@ -178,15 +180,18 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
178180
break
179181

180182
default: {
181-
const matched = OVERWRITE_OPTION.exec(arg)
182-
if (matched) {
183+
let matched = null
184+
if ((matched = OVERWRITE_OPTION.exec(arg))) {
183185
overwriteConfig(
184186
set.packageConfig,
185187
matched[1],
186188
matched[2],
187189
matched[3] || args[++i]
188190
)
189191
}
192+
else if ((matched = CONFIG_OPTION.exec(arg))) {
193+
set.config[matched[1]] = matched[2]
194+
}
190195
else if (CONCAT_OPTIONS.test(arg)) {
191196
parseCLIArgsCore(
192197
set,

src/bin/npm-run-all/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
3232
const {
3333
continueOnError,
3434
groups,
35+
config,
3536
packageConfig,
3637
printLabel,
3738
printName,
@@ -55,6 +56,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
5556
continueOnError,
5657
printLabel,
5758
printName,
59+
config,
5860
packageConfig,
5961
silent,
6062
arguments: rest,

src/bin/run-p/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
3232
const {
3333
lastGroup: {patterns, parallel},
3434
continueOnError,
35+
config,
3536
packageConfig,
3637
printLabel,
3738
printName,
@@ -54,6 +55,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
5455
continueOnError,
5556
printLabel,
5657
printName,
58+
config,
5759
packageConfig,
5860
silent,
5961
arguments: rest,

src/bin/run-s/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
3232
const {
3333
lastGroup: {patterns, parallel},
3434
continueOnError,
35+
config,
3536
packageConfig,
3637
printLabel,
3738
printName,
@@ -53,6 +54,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
5354
continueOnError,
5455
printLabel,
5556
printName,
57+
config,
5658
packageConfig,
5759
silent,
5860
arguments: rest,

src/lib/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ function toOverwriteOptions(config) {
125125
return options
126126
}
127127

128+
/**
129+
* Converts a given config object to an `--a=b` style option array.
130+
*
131+
* @param {object|null} config -
132+
* A map-like object to set configs.
133+
* @returns {string[]} `--a=b` style options.
134+
*/
135+
function toConfigOptions(config) {
136+
return Object.keys(config).map(key => `--${key}=${config[key]}`)
137+
}
138+
128139
/**
129140
* Gets the maximum length.
130141
*
@@ -203,6 +214,7 @@ module.exports = function npmRunAll(
203214
stdout = null,
204215
stderr = null,
205216
taskList = null,
217+
config = null,
206218
packageConfig = null,
207219
silent = false,
208220
continueOnError = false,
@@ -229,6 +241,9 @@ module.exports = function npmRunAll(
229241
if (packageConfig != null) {
230242
prefixOptions.push(...toOverwriteOptions(packageConfig))
231243
}
244+
if (config != null) {
245+
prefixOptions.push(...toConfigOptions(config))
246+
}
232247

233248
return Promise.resolve(taskList)
234249
.then(taskList => { // eslint-disable-line no-shadow

test-workspace/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"test-task:package-config": "node tasks/package-config1.js",
1414
"test-task:package-config2": "node tasks/package-config2.js",
1515
"test-task:nested-package-config": "babel-node ../src/bin/npm-run-all/index.js test-task:package-config",
16+
"test-task:config": "node tasks/config1.js",
17+
"test-task:config2": "node tasks/config2.js",
18+
"test-task:nested-config": "babel-node ../src/bin/npm-run-all/index.js test-task:config",
1619
"test-task:append": "node tasks/append2.js",
1720
"test-task:append:a": "node tasks/append2.js a",
1821
"test-task:append:a:c": "node tasks/append2.js ac",

test-workspace/tasks/config1.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict"
2+
3+
var appendResult = require("./lib/util").appendResult
4+
appendResult(String(process.env.npm_config_test))

test-workspace/tasks/config2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict"
2+
3+
var appendResult = require("./lib/util").appendResult
4+
appendResult(process.env.npm_config_test + "\n" + process.env.npm_config_test2 + "\n" + process.env.npm_config_test3)

test/config.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2016 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const assert = require("power-assert")
13+
const {result, removeResult} = require("./lib/util")
14+
15+
// Test targets.
16+
const nodeApi = require("../src/lib")
17+
const runAll = require("../src/bin/npm-run-all")
18+
const runSeq = require("../src/bin/run-s")
19+
const runPar = require("../src/bin/run-p")
20+
21+
//------------------------------------------------------------------------------
22+
// Test
23+
//------------------------------------------------------------------------------
24+
25+
describe("[config] it should have an ability to set config variables:", () => {
26+
before(() => process.chdir("test-workspace"))
27+
after(() => process.chdir(".."))
28+
29+
beforeEach(removeResult)
30+
31+
it("Node API should address \"config\" option", () =>
32+
nodeApi("test-task:config", {config: {test: "this is a config"}})
33+
.then(() => {
34+
assert(result() === "this is a config")
35+
})
36+
)
37+
38+
it("Node API should address \"config\" option for multiple variables", () =>
39+
nodeApi("test-task:config2", {config: {test: "1", test2: "2", test3: "3"}})
40+
.then(() => {
41+
assert(result() === "1\n2\n3")
42+
})
43+
)
44+
45+
describe("CLI commands should address \"--a=b\" style options", () => {
46+
it("npm-run-all command", () =>
47+
runAll(["test-task:config", "--test=GO"])
48+
.then(() => {
49+
assert(result() === "GO")
50+
})
51+
)
52+
53+
it("run-s command", () =>
54+
runSeq(["test-task:config", "--test=GO"])
55+
.then(() => {
56+
assert(result() === "GO")
57+
})
58+
)
59+
60+
it("run-p command", () =>
61+
runPar(["test-task:config", "--test=GO"])
62+
.then(() => {
63+
assert(result() === "GO")
64+
})
65+
)
66+
})
67+
68+
describe("CLI commands should address \"--b=c\" style options for multiple variables", () => {
69+
it("npm-run-all command", () =>
70+
runAll(["test-task:config2", "--test=1", "--test2=2", "--test3=3"])
71+
.then(() => {
72+
assert(result() === "1\n2\n3")
73+
})
74+
)
75+
76+
it("run-s command", () =>
77+
runSeq(["test-task:config2", "--test=1", "--test2=2", "--test3=3"])
78+
.then(() => {
79+
assert(result() === "1\n2\n3")
80+
})
81+
)
82+
83+
it("run-p command", () =>
84+
runPar(["test-task:config2", "--test=1", "--test2=2", "--test3=3"])
85+
.then(() => {
86+
assert(result() === "1\n2\n3")
87+
})
88+
)
89+
})
90+
91+
describe("CLI commands should transfar configs to nested commands.", () => {
92+
it("npm-run-all command", () =>
93+
runAll(["test-task:nested-config", "--test=GO DEEP"])
94+
.then(() => {
95+
assert(result() === "GO DEEP")
96+
})
97+
)
98+
99+
it("run-s command", () =>
100+
runSeq(["test-task:nested-config", "--test=GO DEEP"])
101+
.then(() => {
102+
assert(result() === "GO DEEP")
103+
})
104+
)
105+
106+
it("run-p command", () =>
107+
runPar(["test-task:nested-config", "--test=GO DEEP"])
108+
.then(() => {
109+
assert(result() === "GO DEEP")
110+
})
111+
)
112+
})
113+
})

0 commit comments

Comments
 (0)