Skip to content

Commit

Permalink
feat: add support to register a dependency with path and namespace an…
Browse files Browse the repository at this point in the history
…d use passed namespace to register a generator path
  • Loading branch information
mshima committed Jan 12, 2023
1 parent f5d60aa commit 36ef3d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
8 changes: 6 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { RunContextSettings } from './run-context.js';
/**
* Dependencies can be path (autodiscovery) or an array [<generator>, <name>]
*/
export type Dependency = string | Parameters<Environment['registerStub']>;
export type Dependency = string | Parameters<Environment['registerStub']> | Parameters<Environment['register']>;

type GeneratorNew<GenParameter extends YeomanGenerator = YeomanGenerator> = new (
...args: ConstructorParameters<typeof YeomanGenerator<GenParameter['options']>>
Expand Down Expand Up @@ -224,7 +224,11 @@ export class YeomanTest {
registerDependencies(env: Environment, dependencies: Dependency[]) {
for (const dependency of dependencies) {
if (Array.isArray(dependency)) {
env.registerStub(dependency[0] as any, dependency[1]);
if (typeof dependency[0] === 'string') {
env.register(...(dependency as Parameters<Environment['register']>));
} else {
env.registerStub(...(dependency as Parameters<Environment['registerStub']>));
}
} else {
env.register(dependency);
}
Expand Down
43 changes: 23 additions & 20 deletions src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class RunContextBase<GeneratorType extends Generator> extends EventEmitte
private eventListenersSet = false;
private envCB: any;

private built = false;
private ran = false;
private errored = false;

Expand All @@ -100,16 +101,10 @@ export class RunContextBase<GeneratorType extends Generator> extends EventEmitte
) {
super();
this.settings = {
namespace: 'gen:test',
...settings,
};
this.Generator = generatorType;

if (typeof generatorType !== 'string') {
const { namespace, resolved } = this.settings;
this.withGenerators([[generatorType, namespace, resolved]] as any);
}

this.envOptions = envOptions;

this.withOptions({
Expand All @@ -130,7 +125,9 @@ export class RunContextBase<GeneratorType extends Generator> extends EventEmitte
* @return {PromiseRunResult} Promise a RunResult instance.
*/
async run(): PromiseRunResult<GeneratorType> {
if (!this.ran) {
this.ran = true;

if (!this.built) {
await this.build();
}

Expand Down Expand Up @@ -371,12 +368,18 @@ export class RunContextBase<GeneratorType extends Generator> extends EventEmitte

withGenerators(dependencies: Dependency[]): this {
assert(Array.isArray(dependencies), 'dependencies should be an array');
return this.onEnvironment(env => {
return this.onEnvironment(async env => {
for (const dependency of dependencies) {
if (Array.isArray(dependency)) {
env.registerStub(...dependency);
if (typeof dependency[0] === 'string') {
// eslint-disable-next-line no-await-in-loop, @typescript-eslint/await-thenable
await env.register(...(dependency as Parameters<Environment['register']>));
} else {
env.registerStub(...(dependency as Parameters<Environment['registerStub']>));
}
} else {
env.register(dependency);
// eslint-disable-next-line no-await-in-loop, @typescript-eslint/await-thenable
await env.register(dependency);
}
}
});
Expand Down Expand Up @@ -500,7 +503,7 @@ export class RunContextBase<GeneratorType extends Generator> extends EventEmitte
}

protected assertNotBuild() {
if (this.ran || this.completed) {
if (this.built || this.completed) {
throw new Error('The context is already built');
}
}
Expand All @@ -512,7 +515,7 @@ export class RunContextBase<GeneratorType extends Generator> extends EventEmitte
protected async build(): Promise<void> {
this.assertNotBuild();

this.ran = true;
this.built = true;

if (!this.targetDirectory && this.settings.tmpdir !== false) {
this.inTmpDir();
Expand Down Expand Up @@ -551,17 +554,17 @@ export class RunContextBase<GeneratorType extends Generator> extends EventEmitte
await onEnvironmentCallback.call(this, this.env);
}

let { namespace } = this.settings;
if (typeof this.Generator === 'string') {
namespace = this.env.namespace(this.Generator);
if (namespace !== this.Generator) {
// Generator is a file path, it should be registered.
this.env.register(this.Generator);
}
const { namespace = typeof this.Generator === 'string' ? this.env.namespace(this.Generator) : 'gen:test' } = this.settings;
if (typeof this.Generator === 'string' && namespace !== this.Generator) {
// Generator is a file path, it should be registered.
this.env.register(this.Generator, namespace);
} else if (typeof this.Generator !== 'string') {
const { resolved } = this.settings;
this.env.registerStub(this.Generator as unknown as any, namespace, resolved);
}

// eslint-disable-next-line @typescript-eslint/await-thenable
this.generator = (await this.env.create(namespace!, this.args, this.options)) as any;
this.generator = (await this.env.create(namespace, this.args, this.options)) as any;

for (const onGeneratorCallback of this.onGeneratorCallbacks) {
// eslint-disable-next-line no-await-in-loop
Expand Down
11 changes: 9 additions & 2 deletions test/run-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ describe('RunContext', function () {
this.ctx.cleanTestDirectory();
}

if (this.ctx.completed || this.ctx.errored) {
if (this.ctx.completed || this.ctx.errored || !this.ctx.ran) {
done();
return;
}

this.ctx.on('end', done);
try {
this.ctx.on('end', done);
} catch {}
});

describe('constructor', function () {
Expand Down Expand Up @@ -668,6 +670,11 @@ describe('RunContext', function () {
);
});

it('register paths with namespaces', async function () {
await this.ctx.withGenerators([[require.resolve('./fixtures/generator-simple/app'), 'foo:bar']]).build();
assert(this.ctx.env.get('foo:bar'));
});

it('register mocked generator', function (done) {
this.ctx.withGenerators([[helpers.createDummyGenerator(), 'dummy:gen']]).on(
'ready',
Expand Down

0 comments on commit 36ef3d7

Please sign in to comment.