Skip to content

Commit 1c3ff69

Browse files
add callback function support for generator scheduling (#521)
--------- Co-authored-by: Marcelo Shima <[email protected]>
1 parent 0218d49 commit 1c3ff69

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/environment-base.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,12 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
422422
): Promise<G>;
423423
async composeWith<G extends BaseGenerator = BaseGenerator>(generator: string | GetGeneratorConstructor<G>, ...args: any[]): Promise<G> {
424424
const options = getComposeOptions(...args) as ComposeOptions<G>;
425-
const { schedule = true, ...instantiateOptions } = options;
425+
const { schedule: passedSchedule = true, ...instantiateOptions } = options;
426426

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

431433
/**

test/environment.js

+12
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ for (const generatorVersion of allVersions) {
272272
}
273273
});
274274
});
275+
describe('passing function schedule parameter', () => {
276+
it('returning false should not schedule generator', async function () {
277+
this.env.queueTask = sinon.spy();
278+
await this.env.composeWith('stub', { generatorArgs: [], schedule: () => false });
279+
if (isGreaterThan6(generatorVersion)) {
280+
assert(this.env.queueTask.calledOnce);
281+
assert(this.env.queueTask.getCall(0).firstArg !== 'environment:run');
282+
} else {
283+
assert(this.env.queueTask.notCalled);
284+
}
285+
});
286+
});
275287

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

0 commit comments

Comments
 (0)