From 840149456de7d83bd8a5d41ac0fdd9e893101105 Mon Sep 17 00:00:00 2001 From: Alex Kanunnikov Date: Wed, 29 Sep 2021 08:06:56 +0300 Subject: [PATCH] async function as entrypoint for Brocfile (#486) --- lib/cli.ts | 46 +++++++++++++++--------- test/cli_test.js | 16 +++++++++ test/fixtures/async-brocfile/Brocfile.js | 3 ++ 3 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/async-brocfile/Brocfile.js diff --git a/lib/cli.ts b/lib/cli.ts index 3753d1e5..63b4ea0b 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -9,6 +9,17 @@ import ConsoleUI from '../types/console-ui'; import WatchDetector from 'watch-detector'; import UI from 'console-ui'; +enum EnvironmentType { + PRODUCTION = 'production', + DEVELOPMENT = 'development', +} + +enum WatcherType { + POLLING = 'polling', + WATCHMAN = 'watchman', + NODE = 'node', +} + interface ServeOptions { host: string; port: string; @@ -19,8 +30,8 @@ interface ServeOptions { outputPath?: string; cwd?: string; noWatch?: boolean; - watcher?: string; - environment: string; + watcher?: WatcherType, + environment: EnvironmentType; prod?: boolean; dev?: boolean; @@ -32,8 +43,8 @@ interface BuildOptions { outputPath?: string; cwd?: string; watch?: boolean; - watcher?: string; - environment: string; + watcher?: WatcherType; + environment: EnvironmentType; prod?: boolean; dev?: boolean; } @@ -44,9 +55,10 @@ function buildBrocfileOptions(options: { environment: string }) { }; } -function getBuilder(options: { environment: string }) { +async function getBuilder(options: { environment: string }) { const brocfile = broccoli.loadBrocfile(options); - return new broccoli.Builder(brocfile(buildBrocfileOptions(options))); + const instance = await Promise.resolve(brocfile(buildBrocfileOptions(options))); + return new broccoli.Builder(instance); } function getWatcher(options: { watch?: boolean }) { @@ -73,9 +85,9 @@ function buildWatcherOptions(options: { watcher?: string }, ui: ConsoleUI) { return { saneOptions: { - poll: watcher === 'polling', - watchman: watcher === 'watchman', - node: watcher === 'node' || !watcher, + poll: watcher === WatcherType.POLLING, + watchman: watcher === WatcherType.WATCHMAN, + node: watcher === WatcherType.NODE || !watcher, }, }; } @@ -135,14 +147,14 @@ export = function broccoliCLI(args: string[], ui = new UI()) { .option('-e, --environment ', 'build environment [development]', 'development') .option('--prod', 'alias for --environment=production') .option('--dev', 'alias for --environment=development') - .action((options: ServeOptions) => { + .action(async (options: ServeOptions) => { if (options.prod) { - options.environment = 'production'; + options.environment = EnvironmentType.PRODUCTION; } else if (options.dev) { - options.environment = 'development'; + options.environment = EnvironmentType.DEVELOPMENT; } - const builder = getBuilder(options); + const builder = await getBuilder(options); const Watcher = getWatcher(options); const outputDir = options.outputPath; const watcher = new Watcher( @@ -193,7 +205,7 @@ export = function broccoliCLI(args: string[], ui = new UI()) { .option('-e, --environment ', 'build environment [development]', 'development') .option('--prod', 'alias for --environment=production') .option('--dev', 'alias for --environment=development') - .action((outputDir: string, options: BuildOptions) => { + .action(async (outputDir: string, options: BuildOptions) => { if (outputDir && options.outputPath) { ui.writeLine('option --output-path and [target] cannot be passed at same time', 'ERROR'); return process.exit(1); @@ -208,9 +220,9 @@ export = function broccoliCLI(args: string[], ui = new UI()) { } if (options.prod) { - options.environment = 'production'; + options.environment = EnvironmentType.PRODUCTION; } else if (options.dev) { - options.environment = 'development'; + options.environment = EnvironmentType.DEVELOPMENT; } try { @@ -224,7 +236,7 @@ export = function broccoliCLI(args: string[], ui = new UI()) { throw e; } - const builder = getBuilder(options); + const builder = await getBuilder(options); const Watcher = getWatcher(options); const outputTree = new TreeSync(builder.outputPath, outputDir); const watcher = new Watcher( diff --git a/test/cli_test.js b/test/cli_test.js index 4c61775c..802f39bf 100644 --- a/test/cli_test.js +++ b/test/cli_test.js @@ -221,6 +221,22 @@ describe('cli', function() { }); }); + context('with promise in a Brocfile default export', function() { + it('closes process on completion', async function() { + await cli([ + 'node', + 'broccoli', + 'build', + 'dist', + '--brocfile-path', + '../../async-brocfile/Brocfile.js', + ]); + chai.expect(process.exitCode).to.eql(0); + chai.expect(exitStub).to.be.calledWith(0); + chai.expect(fs.existsSync('dist')).to.be.true; + }); + }); + context('with param --brocfile-path', function() { it('closes process on completion', async function() { await cli(['node', 'broccoli', 'build', 'dist', '--brocfile-path', '../Brocfile.js']); diff --git a/test/fixtures/async-brocfile/Brocfile.js b/test/fixtures/async-brocfile/Brocfile.js new file mode 100644 index 00000000..476254d1 --- /dev/null +++ b/test/fixtures/async-brocfile/Brocfile.js @@ -0,0 +1,3 @@ +module.exports = async function() { + return '42'; +} \ No newline at end of file