Skip to content

Commit

Permalink
add callback function support for generator scheduling (#521)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Marcelo Shima <marceloshima@gmail.com>
  • Loading branch information
dwarakaprasad and mshima committed Mar 9, 2024
1 parent 0218d49 commit 1c3ff69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/environment-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,12 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
): Promise<G>;
async composeWith<G extends BaseGenerator = BaseGenerator>(generator: string | GetGeneratorConstructor<G>, ...args: any[]): Promise<G> {
const options = getComposeOptions(...args) as ComposeOptions<G>;
const { schedule = true, ...instantiateOptions } = options;
const { schedule: passedSchedule = true, ...instantiateOptions } = options;

const generatorInstance = await this.create(generator, instantiateOptions);
return this.queueGenerator(generatorInstance, { schedule });
// Convert to function to keep type compatibility with old @yeoman/types where schedule is boolean only
const schedule: (gen: G) => boolean = typeof passedSchedule === 'function' ? passedSchedule : () => passedSchedule;
return this.queueGenerator(generatorInstance, { schedule: schedule(generatorInstance) });
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ for (const generatorVersion of allVersions) {
}
});
});
describe('passing function schedule parameter', () => {
it('returning false should not schedule generator', async function () {
this.env.queueTask = sinon.spy();
await this.env.composeWith('stub', { generatorArgs: [], schedule: () => false });
if (isGreaterThan6(generatorVersion)) {
assert(this.env.queueTask.calledOnce);
assert(this.env.queueTask.getCall(0).firstArg !== 'environment:run');
} else {
assert(this.env.queueTask.notCalled);
}
});
});

it('should emit a compose event', function (done) {
this.env.once('compose', (namespace, generator) => {
Expand Down

0 comments on commit 1c3ff69

Please sign in to comment.