Skip to content

Commit 5279fb9

Browse files
committed
fix import module at windows
1 parent 6bacec6 commit 5279fb9

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/environment.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
createYoRcTransform,
3434
createYoResolveTransform
3535
} = require('./util/transform');
36+
const {requireOrImport} = require('./util/esm');
3637

3738
const {isFilePending} = FileEditor.State;
3839

@@ -564,13 +565,15 @@ class Environment extends Base {
564565
return;
565566
}
566567

567-
// eslint-disable-next-line node/no-unsupported-features/es-syntax
568-
const importModule = async () => import(meta.resolved);
569-
const instantiate = async (args, options) => this.instantiate(await this._findGeneratorClass(await meta.importGenerator()), args, options);
568+
const {importGenerator, resolved} = meta;
569+
const importModule = async () => requireOrImport(resolved);
570+
const importGeneratorClass = async () => this._findGeneratorClass(await importGenerator(), meta);
571+
const instantiate = async (args, options) => this.instantiate(await importGeneratorClass(), args, options);
570572
const instantiateHelp = async () => instantiate([], {help: true});
571573
const newMeta = {
572574
...meta,
573575
importModule,
576+
importGeneratorClass,
574577
instantiate,
575578
instantiateHelp
576579
};

test/environment.js

+32
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,38 @@ describe('Environment', () => {
462462
});
463463
});
464464

465+
describe.only('#getGeneratorMeta{}', () => {
466+
it('importGenerator should return a class', async function () {
467+
this.env
468+
.register(path.join(__dirname, './fixtures/generator-module/generators/app'), 'fixtures:generator-module');
469+
const meta = this.env.getGeneratorMeta('fixtures:generator-module');
470+
assert.equal(typeof await meta.importGeneratorClass(), 'function');
471+
});
472+
it('importModule should return the generator module', async function () {
473+
this.env
474+
.register(path.join(__dirname, './fixtures/generator-module/generators/app'), 'fixtures:generator-module');
475+
const meta = this.env.getGeneratorMeta('fixtures:generator-module');
476+
const Generator = await meta.importGeneratorClass();
477+
const module = await meta.importModule();
478+
assert.strictEqual(Generator, module.default);
479+
});
480+
it('intantiate should return an instance', async function () {
481+
this.env
482+
.register(path.join(__dirname, './fixtures/generator-module/generators/app'), 'fixtures:generator-module');
483+
const meta = this.env.getGeneratorMeta('fixtures:generator-module');
484+
const Generator = await meta.importGeneratorClass();
485+
const generator = await meta.instantiate();
486+
assert.ok(generator instanceof Generator);
487+
});
488+
it('intantiateHelp should return an instance with help option', async function () {
489+
this.env
490+
.register(path.join(__dirname, './fixtures/generator-module/generators/app'), 'fixtures:generator-module');
491+
const meta = this.env.getGeneratorMeta('fixtures:generator-module');
492+
const generator = await meta.instantiateHelp();
493+
assert.strictEqual(generator.options.help, true);
494+
});
495+
});
496+
465497
describe('#run() a ts generator', () => {
466498
beforeEach(function () {
467499
this.env

0 commit comments

Comments
 (0)