Skip to content

Commit

Permalink
allow to run generators using relative paths. (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jan 15, 2024
1 parent 94f554c commit cd68282
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/environment-full.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash } from 'node:crypto';
import { join } from 'node:path';
import { join, resolve } from 'node:path';
import type { BaseGeneratorConstructor, InputOutputAdapter } from '@yeoman/types';
import { type YeomanNamespace, requireNamespace, toNamespace } from '@yeoman/namespace';
import { flyImport } from 'fly-import';
Expand Down Expand Up @@ -343,11 +343,15 @@ class FullEnvironment extends EnvironmentBase {
args = Array.isArray(args) ? args : splitArgsFromString(args as unknown as string);
options = { ...options };

const name = args.shift();
let name = args.shift();
if (!name) {
throw new Error('Must provide at least one argument, the generator namespace to invoke.');
}

if (name.startsWith('.')) {
name = resolve(name);
}

this.loadEnvironmentOptions(options);

if (this.experimental && !this.getGeneratorMeta(name)) {
Expand Down
15 changes: 15 additions & 0 deletions test/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import events from 'node:events';
import fs from 'node:fs';
import path, { dirname, join } from 'node:path';
import process from 'node:process';
import util from 'node:util';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
Expand Down Expand Up @@ -381,6 +382,20 @@ for (const generatorVersion of allVersions) {
});
});

describe('using relative paths', () => {
let oldCwd;
before(() => {
oldCwd = process.cwd();
process.chdir(dirname(fileURLToPath(import.meta.url)));
});
after(() => {
process.chdir(oldCwd);
});
it('runs a generator', async function () {
return this.env.run(['./fixtures/generator-esm/generators/app/index.js']);
});
});

it('runs a registered writing generator with bail option', async function () {
if (isLegacyVersion(generatorVersion)) {
this.skip();
Expand Down

0 comments on commit cd68282

Please sign in to comment.