Skip to content

Commit

Permalink
Listen to environment's events instead of generator's
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jul 14, 2020
1 parent ca35526 commit 89cc525
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
62 changes: 32 additions & 30 deletions lib/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,20 @@ RunContext.prototype.async = function() {
};

/**
* Method called when the context is ready to run the generator
* @private
* Build the generator and the environment.
* @return {RunContext|false} this
*/

RunContext.prototype._run = function() {
RunContext.prototype.build = function(async = false) {
if (!this.inDirSet && this.settings.tmpdir) {
this.inTmpDir();
}

if (this._asyncHolds !== 0 || this.ran || this.completed) {
return false;
if (async) {
return false;
}

throw new Error('The context is not ready');
}

this.ran = true;
Expand Down Expand Up @@ -147,44 +150,40 @@ RunContext.prototype._run = function() {
helpers.mockLocalConfig(this.generator, this.localConfig);
}

return this;
};

/**
* Method called when the context is ready to run the generator
* @private
*/

RunContext.prototype._run = function() {
if (this.build(true) === false) return false;

const endCallback = callbackWrapper(envOrGenerator => {
helpers.restorePrompt(envOrGenerator);
this.emit('end');
this.completed = true;
});

if (this.settings.runEnvironment) {
this.env.once('end', () => endCallback(this.env));
return true;
}

const callback = callbackWrapper(
function(err) {
helpers.restorePrompt(this.env);
this.emit('error', err);
}.bind(this)
);
const callback = callbackWrapper(err => {
helpers.restorePrompt(this.env);
this.errored = true;
this.emit('error', err);
});

// Yeoman-generator >=5.0.0 doesn't emits error on itself, it emits on the environment.
this.generator.on('error', callback);
this.generator.once('end', () => endCallback(this.generator));

this.env.on('error', callback);
this.env.runLoop.once('end', () => endCallback(this.generator));
this.emit('ready', this.generator);

this.generator.run().catch(callback);
return true;
};

/**
* Build the generator and the environment.
* @return {RunContext} this
*/
RunContext.prototype.build = function() {
if (!this._run()) {
throw new Error('The context is not ready');
}

return this;
};

/**
* Run the generator on the environment and returns the promise.
* @return {Promise} Promise
Expand All @@ -202,7 +201,10 @@ RunContext.prototype.run = function() {
.runGenerator(this.generator)
.then(
() => new RunResult({ env: this.env, cwd: this.targetDirectory, fs: this.env.fs })
);
)
.finally(() => {
helpers.restorePrompt(this.env);
});
};

/**
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
],
"repository": "yeoman/yeoman-test",
"license": "MIT",
"engines": {
"node": ">=12.10.0"
},
"devDependencies": {
"coveralls": "^3.0.10",
"eslint": "^6.8.0",
Expand Down
18 changes: 10 additions & 8 deletions test/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('RunContext', function() {
afterEach(function(done) {
process.chdir(__dirname);

if (this.ctx.errored) {
throw new Error('The context errored');
}

if (this.ctx.settings.tmpdir) {
this.ctx.cleanTestDirectory();
}
Expand All @@ -47,9 +51,10 @@ describe('RunContext', function() {
});

describe('constructor', function() {
it('forwards envOptions to the environment', function() {
it('forwards envOptions to the environment', function(done) {
this.ctx.on('ready', function() {
assert.equal(this.env.options.foo, envOptions.foo);
done();
});
});

Expand Down Expand Up @@ -166,13 +171,10 @@ describe('RunContext', function() {
});

it('only run a generator once', function(done) {
this.ctx.on(
'end',
function() {
sinon.assert.calledOnce(this.execSpy);
done();
}.bind(this)
);
this.ctx.on('end', () => {
sinon.assert.calledOnce(this.execSpy);
done();
});

this.ctx._run();
this.ctx._run();
Expand Down

0 comments on commit 89cc525

Please sign in to comment.