Skip to content

Commit

Permalink
Merge pull request #61 from mysticatea/issue60
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Sep 1, 2016
2 parents f873b35 + 09507bc commit 6a3697d
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 26 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
9 changes: 6 additions & 3 deletions test-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
"scripts": {
"start": "node tasks/append2.js start",
"stop": "node tasks/append2.js stop",
"test-task:env-check": "node tasks/env-check.js",
"test-task:env-check2": "node tasks/env-check2.js",
"test-task:nested-env-check": "babel-node ../src/bin/npm-run-all/index.js test-task:env-check",
"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)
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,22 @@ describe("[common]", () => {

describe("should run a task by npm (check an environment variable):", () => {
it("Node API", () =>
nodeApi("test-task:env-check")
nodeApi("test-task:package-config")
.then(() => assert(result() === "OK"))
)

it("npm-run-all command", () =>
runAll(["test-task:env-check"])
runAll(["test-task:package-config"])
.then(() => assert(result() === "OK"))
)

it("run-s command", () =>
runSeq(["test-task:env-check"])
runSeq(["test-task:package-config"])
.then(() => assert(result() === "OK"))
)

it("run-p command", () =>
runPar(["test-task:env-check"])
runPar(["test-task:package-config"])
.then(() => assert(result() === "OK"))
)
})
Expand Down
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 6a3697d

Please sign in to comment.