Skip to content

Commit 388ba2d

Browse files
authored
add withEnvironmentRun helper (#241)
1 parent 27aa61b commit 388ba2d

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/run-context.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
103103
private ran = false;
104104
private errored = false;
105105
private readonly beforePrepareCallbacks: Array<(this: this) => void | Promise<void>> = [];
106+
private environmentRun?: (this: this, env: DefaultEnvironmentApi, gen: GeneratorType) => void;
106107

107108
/**
108109
* This class provide a run context object to façade the complexity involved in setting
@@ -151,7 +152,8 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
151152
}
152153

153154
try {
154-
await this.env.runGenerator(this.generator as any);
155+
const environmentRun = this.environmentRun ?? ((env, generator) => env.runGenerator(generator));
156+
await environmentRun.call(this, this.env, this.generator);
155157
} finally {
156158
this.helpers.restorePrompt(this.env);
157159
this.completed = true;
@@ -301,6 +303,17 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
301303
return this;
302304
}
303305

306+
/**
307+
* Customize enviroment run method.
308+
*
309+
* @param callback
310+
* @return {this} run context instance
311+
*/
312+
withEnvironmentRun(callback: (this: this, env: DefaultEnvironmentApi, gen: GeneratorType) => void) {
313+
this.environmentRun = callback;
314+
return this;
315+
}
316+
304317
/**
305318
* Run lookup on the environment.
306319
*

test/run-context.spec.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import process from 'node:process';
66
import { createRequire } from 'node:module';
77
import { mock } from 'node:test';
88
import { promisify as promisify_ } from 'node:util';
9-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
9+
import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
1010
import Generator from 'yeoman-generator';
1111
import tempDirectory from 'temp-dir';
1212
import { RunContextBase as RunContext } from '../src/run-context.js';
1313
import helpers from '../src/helpers.js';
1414
import { DummyPrompt } from '../src/adapter.js';
15+
import { BaseEnvironmentOptions } from '@yeoman/types';
1516

1617
/* Remove argument from promisify return */
1718
const promisify = function_ => () => promisify_(function_)();
@@ -21,7 +22,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
2122
const tmpdir = path.join(tempDirectory, 'yeoman-run-context');
2223

2324
describe('RunContext', () => {
24-
const environmentOptions = { foo: 'bar' };
25+
let environmentOptions: BaseEnvironmentOptions | undefined;
2526
let context: RunContext;
2627
let execSpy;
2728
let Dummy;
@@ -61,6 +62,9 @@ describe('RunContext', () => {
6162
);
6263

6364
describe('constructor', () => {
65+
beforeAll(() => {
66+
environmentOptions = { foo: 'bar' };
67+
});
6468
it(
6569
'forwards envOptions to the environment',
6670
promisify(done => {
@@ -856,6 +860,32 @@ describe('RunContext', () => {
856860
);
857861
});
858862

863+
describe('#withEnvironmentRun()', () => {
864+
it('calls runGenerator by default', async () => {
865+
let mockedRunGenerator: ReturnType<typeof mock.fn>;
866+
await context
867+
.withEnvironment(environment => {
868+
mockedRunGenerator = mock.method(environment, 'runGenerator');
869+
})
870+
.toPromise();
871+
expect(mockedRunGenerator!.mock.callCount()).toBe(1);
872+
});
873+
874+
it('calls custom environment run method', async () => {
875+
let mockedRunGenerator: ReturnType<typeof mock.fn>;
876+
const mockedEnvironmentRun = mock.fn();
877+
await context
878+
.withEnvironment(environment => {
879+
mockedRunGenerator = mock.method(environment, 'runGenerator');
880+
})
881+
.withEnvironmentRun(mockedEnvironmentRun)
882+
.toPromise();
883+
884+
expect(mockedRunGenerator!.mock.callCount()).toBe(0);
885+
expect(mockedEnvironmentRun!.mock.callCount()).toBe(1);
886+
});
887+
});
888+
859889
describe('#withLocalConfig()', () => {
860890
it(
861891
'provides config to the generator',

0 commit comments

Comments
 (0)