Skip to content

Commit

Permalink
fixup! Use await import in findBuildFile
Browse files Browse the repository at this point in the history
  • Loading branch information
hjdivad committed Oct 18, 2022
1 parent 6384eb0 commit b2c2b37
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 130 deletions.
10 changes: 7 additions & 3 deletions lib/models/builder.js
Expand Up @@ -51,6 +51,12 @@ class Builder extends CoreObject {
throw new SilentError('No ember-cli-build.js found.');
}

async ensureBroccoliBuilder() {
if (this.builder === undefined) {
await this.setupBroccoliBuilder();
}
}

/**
* @private
* @method setupBroccoliBuilder
Expand Down Expand Up @@ -166,9 +172,7 @@ class Builder extends CoreObject {
* @return {Promise}
*/
async build(addWatchDirCallback, resultAnnotation) {
if (this.builder === undefined) {
await this.setupBroccoliBuilder();
}
await this.ensureBroccoliBuilder();

let buildResults, uiProgressIntervalID;

Expand Down
4 changes: 2 additions & 2 deletions lib/models/server-watcher.js
Expand Up @@ -3,8 +3,8 @@
const Watcher = require('./watcher');

module.exports = class ServerWatcher extends Watcher {
constructor(options) {
super(options);
constructor(options, build) {
super(options, build);

this.watcher.on('add', this.didAdd.bind(this));
this.watcher.on('delete', this.didDelete.bind(this));
Expand Down
29 changes: 23 additions & 6 deletions lib/models/watcher.js
Expand Up @@ -12,19 +12,35 @@ const eventTypeNormalization = {
change: 'changed',
};

const ConstructedFromBuilder = Symbol('Watcher.build');

module.exports = class Watcher extends CoreObject {
constructor(_options) {
constructor(_options, build) {
if (build !== ConstructedFromBuilder) {
throw new Error('instantiate Watcher with (await Watcher.build()).watcher, not new Watcher()');
}

super(_options);

this.verbose = true;
this.serving = _options.serving;
}

let options = this.buildOptions();
static async build(_options) {
let watcher = new this(_options, ConstructedFromBuilder);
await watcher.setupBroccoliWatcher(_options);

logger.info('initialize %o', options);
// This indirection is because Watcher instances are themselves spec
// noncompliant thennables (see the then() method) so returning watcher
// directly will interfere with `await Watcher.build()`
return { watcher };
}

this.serving = _options.serving;
async setupBroccoliWatcher() {
let options = this.buildOptions();

this.watcher = this.watcher || this.constructBroccoliWatcher(options);
logger.info('initialize %o', options);
this.watcher = this.watcher || (await this.constructBroccoliWatcher(options));

this.setupBroccoliChangeEvent();
this.watcher.on('buildStart', this._setupBroccoliWatcherBuild.bind(this));
Expand All @@ -38,8 +54,9 @@ module.exports = class Watcher extends CoreObject {
this.serveURL = serveURL;
}

constructBroccoliWatcher(options) {
async constructBroccoliWatcher(options) {
const { Watcher } = require('broccoli');
await this.builder.ensureBroccoliBuilder();
const { watchedSourceNodeWrappers } = this.builder.builder;

let watcher = new Watcher(this.builder, watchedSourceNodeWrappers, { saneOptions: options, ignored: this.ignored });
Expand Down
16 changes: 9 additions & 7 deletions lib/tasks/build-watch.js
Expand Up @@ -35,13 +35,15 @@ class BuildWatchTask extends Task {

let watcher =
options._watcher ||
new Watcher({
ui,
builder,
analytics: this.analytics,
options,
ignored: [path.resolve(this.project.root, options.outputPath)],
});
(
await Watcher.build({
ui,
builder,
analytics: this.analytics,
options,
ignored: [path.resolve(this.project.root, options.outputPath)],
})
).watcher;

await watcher;
// Run until failure or signal to exit
Expand Down
18 changes: 10 additions & 8 deletions lib/tasks/serve.js
Expand Up @@ -55,14 +55,16 @@ class ServeTask extends Task {

let watcher =
options._watcher ||
new Watcher({
ui: this.ui,
builder,
analytics: this.analytics,
options,
serving: true,
ignored: [path.resolve(this.project.root, options.outputPath)],
});
(
await Watcher.build({
ui: this.ui,
builder,
analytics: this.analytics,
options,
serving: true,
ignored: [path.resolve(this.project.root, options.outputPath)],
})
).watcher;

let serverRoot = './server';
let serverWatcher = null;
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/models/server-watcher-test.js
Expand Up @@ -12,12 +12,12 @@ describe('Server Watcher', function () {
let analytics;
let watcher;

beforeEach(function () {
beforeEach(async function () {
ui = new MockUI();
analytics = new MockAnalytics();
watcher = new MockServerWatcher();

new ServerWatcher({
await ServerWatcher.build({
ui,
analytics,
watcher,
Expand Down

0 comments on commit b2c2b37

Please sign in to comment.