Skip to content

Commit

Permalink
add CLI option for selecting a config from a multi-config
Browse files Browse the repository at this point in the history
  • Loading branch information
rrharvey committed Jul 20, 2017
1 parent 048b300 commit 3c77b07
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/config-optimist.js
Expand Up @@ -2,6 +2,7 @@ module.exports = function(optimist) {
optimist
.boolean("help").alias("help", "h").alias("help", "?").describe("help")
.string("config").describe("config", "Path to the config file")
.string("config-name").describe("config-name", "Name of the config to use")
.string("env").describe("env", "Environment passed to the config, when it is a function")
.string("context").describe("context", "The root directory for resolving entry point and stats")
.string("entry").describe("entry", "The entry point")
Expand Down
6 changes: 6 additions & 0 deletions bin/config-yargs.js
Expand Up @@ -20,6 +20,12 @@ module.exports = function(yargs) {
defaultDescription: "webpack.config.js or webpackfile.js",
requiresArg: true
},
"config-name": {
type: "string",
describe: "Name of the config to use",
group: CONFIG_GROUP,
requiresArg: true
},
"env": {
describe: "Environment passed to the config, when it is a function",
group: CONFIG_GROUP
Expand Down
14 changes: 14 additions & 0 deletions bin/convert-argv.js
Expand Up @@ -130,6 +130,20 @@ module.exports = function(yargs, argv, convertOptions) {
return processConfiguredOptions(options.default);
}

// filter multi-config by name
if(Array.isArray(options) && argv["config-name"]) {
var namedOptions = options.filter(function(opt) {
return opt.name === argv["config-name"];
});
if(namedOptions.length === 0) {
console.error("Configuration with name '" + argv["config-name"] + "' was not found.");
process.exit(-1); // eslint-disable-line
} else if(namedOptions.length === 1) {
return processConfiguredOptions(namedOptions[0]);
}
options = namedOptions;
}

if(Array.isArray(options)) {
options.forEach(processOptions);
} else {
Expand Down
1 change: 1 addition & 0 deletions test/binCases/config-name/found-many/index.js
@@ -0,0 +1 @@
module.exports = "foo";
1 change: 1 addition & 0 deletions test/binCases/config-name/found-many/index2.js
@@ -0,0 +1 @@
module.exports = "bar";
Empty file.
11 changes: 11 additions & 0 deletions test/binCases/config-name/found-many/test.js
@@ -0,0 +1,11 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
code.should.be.exactly(0);

stdout.should.be.ok();
stdout[7].should.containEql("./index2.js");
stdout[13].should.containEql("./index3.js");
stderr.should.be.empty();
};

5 changes: 5 additions & 0 deletions test/binCases/config-name/found-many/test.opts
@@ -0,0 +1,5 @@
--config ./webpack.config.js
--config-name bar
--output-filename [name].js
--output-chunk-filename [id].chunk.js
--target async-node
16 changes: 16 additions & 0 deletions test/binCases/config-name/found-many/webpack.config.js
@@ -0,0 +1,16 @@
var path = require("path");

module.exports = [
{
name: "foo",
entry: path.resolve(__dirname, "./index")
},
{
name: "bar",
entry: path.resolve(__dirname, "./index2")
},
{
name: "bar",
entry: path.resolve(__dirname, "./index3.js")
}
];
1 change: 1 addition & 0 deletions test/binCases/config-name/found-one/index.js
@@ -0,0 +1 @@
module.exports = "foo";
1 change: 1 addition & 0 deletions test/binCases/config-name/found-one/index2.js
@@ -0,0 +1 @@
module.exports = "bar";
10 changes: 10 additions & 0 deletions test/binCases/config-name/found-one/test.js
@@ -0,0 +1,10 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
code.should.be.exactly(0);

stdout.should.be.ok();
stdout[5].should.containEql("./index2.js");
stderr.should.be.empty();
};

5 changes: 5 additions & 0 deletions test/binCases/config-name/found-one/test.opts
@@ -0,0 +1,5 @@
--config ./webpack.config.js
--config-name bar
--output-filename [name].js
--output-chunk-filename [id].chunk.js
--target async-node
12 changes: 12 additions & 0 deletions test/binCases/config-name/found-one/webpack.config.js
@@ -0,0 +1,12 @@
var path = require("path");

module.exports = [
{
name: "foo",
entry: path.resolve(__dirname, "./index")
},
{
name: "bar",
entry: path.resolve(__dirname, "./index2")
}
];
9 changes: 9 additions & 0 deletions test/binCases/config-name/not-found/test.js
@@ -0,0 +1,9 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
code.should.be.exactly(255);

stdout.should.be.empty();
stderr[0].should.containEql("Configuration with name \'foo\' was not found.");
};

2 changes: 2 additions & 0 deletions test/binCases/config-name/not-found/test.opts
@@ -0,0 +1,2 @@
--config ./webpack.config.js
--config-name foo
3 changes: 3 additions & 0 deletions test/binCases/config-name/not-found/webpack.config.js
@@ -0,0 +1,3 @@
module.exports = [{
name: "bar"
}];

0 comments on commit 3c77b07

Please sign in to comment.