Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add callback function support for generator scheduling #521

Merged
merged 9 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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