Skip to content

Commit

Permalink
feat: allow nested env properties
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Oct 15, 2020
1 parent 2396ec5 commit 0187b6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
9 changes: 5 additions & 4 deletions packages/webpack-cli/lib/utils/arg-parser.js
Expand Up @@ -95,17 +95,18 @@ const argParser = (options, args, argsOnly = false, name = '') => {
// so you can do `--env platform=staging --env production`
// { platform: "staging", production: true }
const multiArg = (value, previous = {}) => {
const [allKeys, val] = value.split('=');
// this ensures we're only splitting by the first `=`
const [allKeys, val] = value.split(/=(.+)/, 2);
const splitKeys = allKeys.split('.');
let prevRef = previous;
splitKeys.forEach((someKey, index) => {
if (!prevRef[someKey]) prevRef[someKey] = {};
if ('string' === typeof prevRef[someKey]) {
prevRef[someKey] = {};
}
if (index === splitKeys.length - 1) {
prevRef[someKey] = val || true;
} else {
prevRef[someKey] = typeof prevRef[someKey] === 'string' ? {} : { ...prevRef[someKey] };
}
console.log({ prevRef, previous, someKey });
prevRef = prevRef[someKey];
});
return previous;
Expand Down
21 changes: 21 additions & 0 deletions test/config/type/function-with-env/function-with-env.test.js
Expand Up @@ -4,6 +4,13 @@ const { resolve } = require('path');
const { run } = require('../../../utils/test-utils');

describe('function configuration', () => {
it('should throw when env is not supplied', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--env'], false);
expect(stdout).toBeFalsy();
expect(stderr).toBeTruthy();
expect(stderr).toContain(`option '--env <value>' argument missing`);
expect(exitCode).toEqual(1);
});
it('is able to understand a configuration file as a function', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'isProd']);
expect(stderr).toBeFalsy();
Expand Down Expand Up @@ -32,6 +39,20 @@ describe('function configuration', () => {
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/Luffy.js'))).toBeTruthy();
});
it('Supports long nested values in env', () => {
const { stderr, stdout } = run(__dirname, [
'--env',
'file.name.is.this=Atsumu',
'--env',
'environment=production',
'-c',
'webpack.env.config.js',
]);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/Atsumu.js'))).toBeTruthy();
});
it('is able to understand multiple env flags', (done) => {
const { stderr, stdout } = run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);
expect(stderr).toBeFalsy();
Expand Down
10 changes: 5 additions & 5 deletions test/config/type/function-with-env/webpack.env.config.js
@@ -1,13 +1,13 @@
module.exports = (env) => {
const {
environment,
app: { title },
} = env;
const { environment, app, file } = env;
const customName = file && file.name && file.name.is && file.name.is.this;
const appTitle = app && app.title;
console.log(env);
if (environment === 'production') {
return {
entry: './a.js',
output: {
filename: `${title}.js`,
filename: `${customName ? customName : appTitle}.js`,
},
};
}
Expand Down

0 comments on commit 0187b6c

Please sign in to comment.