From dbaf207f0cd8491e0ddce6e09aee37807e3f4619 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Tue, 19 Feb 2019 14:43:45 -0500 Subject: [PATCH] Support --environment CLI option and Brocfile argument (#387) * Support --environment CLI option and Brocfile argument * Update readme * Move line * Reinstate test case * Update test * Move options to builder method * Add -e alias --- README.md | 29 +++++++++++++++ lib/cli.js | 26 +++++++++++++- test/cli_test.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d9128fa..b9f47bc7 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,35 @@ the following folder within your project folder: ├─ main.js └─ helper.js +### Options + +The function that is exported from `module.exports` is passed an options hash by Broccoli +that can be used when assembling the build. + +The options hash is populated by the CLI environment when running `broccoli build` +or `broccoli serve`. It currently only accepts a single option `--environment`, which +is passed as `env` in the `options` hash. + +Additionally `--prod` and `--dev` are available aliases to `--environment=production` +and `--environment=development` respectively. + +* `options`: + * `env`: Defaults to `development`, and can be overridden with the CLI argument `--environment=X` + +For example: +```js +module.exports = (options) => { + // tree = ... assemble tree + + // In production environment, minify the files + if (options.environment === 'production') { + tree = minify(tree); + } + + return tree; +} +``` + ### Using plugins in a `Brocfile.js` The following `Brocfile.js` exports the `app/` subdirectory as `appkit/`: diff --git a/lib/cli.js b/lib/cli.js index 0a8a13b5..e11ffa19 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -31,7 +31,16 @@ module.exports = function broccoliCLI(args) { .option('--no-watch', 'turn off the watcher') .option('--watcher ', 'select sane watcher mode') .option('--overwrite', 'overwrite the [target/--output-path] directory') + .option('-e, --environment ', 'build environment [development]', 'development') + .option('--prod', 'alias for --environment=production') + .option('--dev', 'alias for --environment=development') .action(options => { + if (options.prod) { + options.environment = 'production'; + } else if (options.dev) { + options.environment = 'development'; + } + const builder = getBuilder(options); const Watcher = getWatcher(options); const outputDir = options.outputPath; @@ -70,6 +79,9 @@ module.exports = function broccoliCLI(args) { .option('--watch', 'turn on the watcher') .option('--watcher ', 'select sane watcher mode') .option('--overwrite', 'overwrite the [target/--output-path] directory') + .option('-e, --environment ', 'build environment [development]', 'development') + .option('--prod', 'alias for --environment=production') + .option('--dev', 'alias for --environment=development') .action((outputDir, options) => { if (outputDir && options.outputPath) { console.error('option --output-path and [target] cannot be passed at same time'); @@ -84,6 +96,12 @@ module.exports = function broccoliCLI(args) { outputDir = 'dist'; } + if (options.prod) { + options.environment = 'production'; + } else if (options.dev) { + options.environment = 'development'; + } + try { guardOutputDir(outputDir, options.overwrite); } catch (e) { @@ -142,7 +160,7 @@ module.exports = function broccoliCLI(args) { function getBuilder(options) { const brocfile = broccoli.loadBrocfile(options); - return new broccoli.Builder(brocfile()); + return new broccoli.Builder(brocfile(buildBrocfileOptions(options))); } function getWatcher(options) { @@ -176,6 +194,12 @@ function buildWatcherOptions(options) { }; } +function buildBrocfileOptions(options) { + return { + env: options.environment, + }; +} + function guardOutputDir(outputDir, overwrite) { if (!fs.existsSync(outputDir)) { return; diff --git a/test/cli_test.js b/test/cli_test.js index 29d1fa80..8127f9ac 100644 --- a/test/cli_test.js +++ b/test/cli_test.js @@ -191,6 +191,53 @@ describe('cli', function() { }); }); + context('with param --environment', function() { + it('defaults to --environment=development: { env: "development" }', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + return cli(['node', 'broccoli', 'build', 'dist']).then(() => + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'development')) + ); + }); + + it('with --environment=production passes { env: "production" }', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + return cli(['node', 'broccoli', 'build', 'dist', '--environment=production']).then(() => + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'production')) + ); + }); + + it('with -e production passes { env: "production" }', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + return cli(['node', 'broccoli', 'build', 'dist', '-e', 'production']).then(() => + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'production')) + ); + }); + + it('aliases --dev to --environment=development', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + return cli(['node', 'broccoli', 'build', 'dist', '--dev']).then(() => + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'development')) + ); + }); + + it('aliases --prod to --environment=production', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + return cli(['node', 'broccoli', 'build', 'dist', '--prod']).then(() => + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'production')) + ); + }); + }); + it('supports `b` alias', function() { return cli(['node', 'broccoli', 'b']).then(() => { chai.expect(exitStub).to.be.calledWith(0); @@ -445,6 +492,53 @@ describe('cli', function() { server.verify(); }); }); + + context('with param --environment', function() { + it('defaults to --environment=development: { env: "development" }', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'server').value({ serve() {} }); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + cli(['node', 'broccoli', 'serve']); + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'development')); + }); + + it('with --environment=production passes { env: "production" }', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'server').value({ serve() {} }); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + cli(['node', 'broccoli', 'serve', '--environment=production']); + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'production')); + }); + + it('with -e production passes { env: "production" }', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'server').value({ serve() {} }); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + cli(['node', 'broccoli', 'serve', '-e', 'production']); + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'production')); + }); + + it('aliases --dev to --environment=development', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'server').value({ serve() {} }); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + cli(['node', 'broccoli', 'serve', '--dev']); + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'development')); + }); + + it('aliases --prod to --environment=production', function() { + const spy = sinon.spy(loadBrocfile()); + sinon.stub(broccoli, 'server').value({ serve() {} }); + sinon.stub(broccoli, 'loadBrocfile').value(() => spy); + + cli(['node', 'broccoli', 'serve', '--prod']); + chai.expect(spy).to.be.calledWith(sinon.match.has('env', 'production')); + }); + }); }); context('with param --watcher', function() {