Skip to content

Commit

Permalink
async function as entrypoint for Brocfile (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Sep 29, 2021
1 parent 83fcd48 commit 8401494
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
46 changes: 29 additions & 17 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,8 +30,8 @@ interface ServeOptions {
outputPath?: string;
cwd?: string;
noWatch?: boolean;
watcher?: string;
environment: string;
watcher?: WatcherType,

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 8)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 10)

Expected a semicolon

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Replace `·WatcherType,` with `WatcherType;`

Check failure on line 33 in lib/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 12)

Expected a semicolon
environment: EnvironmentType;
prod?: boolean;
dev?: boolean;

Expand All @@ -32,8 +43,8 @@ interface BuildOptions {
outputPath?: string;
cwd?: string;
watch?: boolean;
watcher?: string;
environment: string;
watcher?: WatcherType;
environment: EnvironmentType;
prod?: boolean;
dev?: boolean;
}
Expand All @@ -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 }) {
Expand All @@ -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,
},
};
}
Expand Down Expand Up @@ -135,14 +147,14 @@ export = function broccoliCLI(args: string[], ui = new UI()) {
.option('-e, --environment <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(
Expand Down Expand Up @@ -193,7 +205,7 @@ export = function broccoliCLI(args: string[], ui = new UI()) {
.option('-e, --environment <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);
Expand All @@ -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 {
Expand All @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions test/cli_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/async-brocfile/Brocfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async function() {
return '42';
}

0 comments on commit 8401494

Please sign in to comment.