Skip to content

Commit

Permalink
Support --environment CLI option and Brocfile argument (#387)
Browse files Browse the repository at this point in the history
* Support --environment CLI option and Brocfile argument

* Update readme

* Move line

* Reinstate test case

* Update test

* Move options to builder method

* Add -e alias
  • Loading branch information
oligriffiths authored Feb 19, 2019
1 parent d7375be commit dbaf207
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/`:
Expand Down
26 changes: 25 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ module.exports = function broccoliCLI(args) {
.option('--no-watch', 'turn off the watcher')
.option('--watcher <watcher>', 'select sane watcher mode')
.option('--overwrite', 'overwrite the [target/--output-path] directory')
.option('-e, --environment <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;
Expand Down Expand Up @@ -70,6 +79,9 @@ module.exports = function broccoliCLI(args) {
.option('--watch', 'turn on the watcher')
.option('--watcher <watcher>', 'select sane watcher mode')
.option('--overwrite', 'overwrite the [target/--output-path] directory')
.option('-e, --environment <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');
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -176,6 +194,12 @@ function buildWatcherOptions(options) {
};
}

function buildBrocfileOptions(options) {
return {
env: options.environment,
};
}

function guardOutputDir(outputDir, overwrite) {
if (!fs.existsSync(outputDir)) {
return;
Expand Down
94 changes: 94 additions & 0 deletions test/cli_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit dbaf207

Please sign in to comment.