Skip to content

Commit

Permalink
rework createGenerator parameters to match yeoman-environment
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed May 24, 2023
1 parent c99f984 commit 17ff321
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable max-params */
import { mkdirSync, existsSync, rmSync } from 'node:fs';
import { resolve } from 'node:path';
import process from 'node:process';
Expand All @@ -10,7 +9,7 @@ import type {
BaseGenerator,
BaseGeneratorOptions,
GetGeneratorConstructor,
GetGeneratorOptions,
InstantiateOptions,
PromptAnswers,
PromptQuestion,
} from '@yeoman/types';
Expand Down Expand Up @@ -218,12 +217,13 @@ export class YeomanTest {
* var angular = createGenerator('angular:app', deps);
*/
async createGenerator<GeneratorType extends BaseGenerator = DefaultGeneratorApi>(
name: string,
dependencies: Dependency[],
args?: string[],
options?: GetGeneratorOptions<GeneratorType>,
localConfigOnly = true,
name: string | GetGeneratorConstructor<GeneratorType>,
options: {
dependencies?: Dependency[];
localConfigOnly?: boolean;
} & InstantiateOptions<GeneratorType> = {},
): Promise<GeneratorType> {
const { dependencies = [], localConfigOnly = true, ...instantiateOptions } = options;
const env = await this.createEnv({ sharedOptions: { localConfigOnly } });
for (const dependency of dependencies) {
if (typeof dependency === 'string') {
Expand All @@ -233,7 +233,7 @@ export class YeomanTest {
}
}

return env.create<GeneratorType>(name, { generatorArgs: args, generatorOptions: options });
return env.create<GeneratorType>(name, instantiateOptions);
}

/**
Expand Down
17 changes: 13 additions & 4 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,29 @@ describe('yeoman-test', function () {

describe('.createGenerator()', function () {
it('create a new generator', async function () {
const generator = await helpers.createGenerator('unicorn:app', [[this.StubGenerator, { namespace: 'unicorn:app' }]]);
const generator = await helpers.createGenerator('unicorn:app', {
dependencies: [[this.StubGenerator, { namespace: 'unicorn:app' }]],
});

assert.ok(generator instanceof this.StubGenerator);
});

it('pass args params to the generator', async function () {
const generator = await helpers.createGenerator('unicorn:app', [[this.StubGenerator, { namespace: 'unicorn:app' }]], ['temp']);
const generator = await helpers.createGenerator('unicorn:app', {
dependencies: [[this.StubGenerator, { namespace: 'unicorn:app' }]],
generatorArgs: ['temp'],
});

assert.deepEqual(generator.args, ['temp']);
});

it('pass options param to the generator', async function () {
const generator = await helpers.createGenerator('unicorn:app', [[this.StubGenerator, { namespace: 'unicorn:app' }]], ['temp'], {
ui: 'tdd',
const generator = await helpers.createGenerator('unicorn:app', {
dependencies: [[this.StubGenerator, { namespace: 'unicorn:app' }]],
generatorArgs: ['temp'],
generatorOptions: {
ui: 'tdd',
},
});

assert.equal(generator.options.ui, 'tdd');
Expand Down

0 comments on commit 17ff321

Please sign in to comment.