diff --git a/lib/environment.js b/lib/environment.js index 54774985..15acba6a 100644 --- a/lib/environment.js +++ b/lib/environment.js @@ -33,6 +33,7 @@ const { createYoRcTransform, createYoResolveTransform } = require('./util/transform'); +const {requireOrImport} = require('./util/esm'); const {isFilePending} = FileEditor.State; @@ -564,13 +565,15 @@ class Environment extends Base { return; } - // eslint-disable-next-line node/no-unsupported-features/es-syntax - const importModule = async () => import(meta.resolved); - const instantiate = async (args, options) => this.instantiate(await this._findGeneratorClass(await meta.importGenerator()), args, options); + const {importGenerator, resolved} = meta; + const importModule = async () => requireOrImport(resolved); + const importGeneratorClass = async () => this._findGeneratorClass(await importGenerator(), meta); + const instantiate = async (args, options) => this.instantiate(await importGeneratorClass(), args, options); const instantiateHelp = async () => instantiate([], {help: true}); const newMeta = { ...meta, importModule, + importGeneratorClass, instantiate, instantiateHelp }; diff --git a/test/environment.js b/test/environment.js index 84c6dc64..6eff6e9f 100644 --- a/test/environment.js +++ b/test/environment.js @@ -462,6 +462,38 @@ describe('Environment', () => { }); }); + describe.only('#getGeneratorMeta{}', () => { + it('importGenerator should return a class', async function () { + this.env + .register(path.join(__dirname, './fixtures/generator-module/generators/app'), 'fixtures:generator-module'); + const meta = this.env.getGeneratorMeta('fixtures:generator-module'); + assert.equal(typeof await meta.importGeneratorClass(), 'function'); + }); + it('importModule should return the generator module', async function () { + this.env + .register(path.join(__dirname, './fixtures/generator-module/generators/app'), 'fixtures:generator-module'); + const meta = this.env.getGeneratorMeta('fixtures:generator-module'); + const Generator = await meta.importGeneratorClass(); + const module = await meta.importModule(); + assert.strictEqual(Generator, module.default); + }); + it('intantiate should return an instance', async function () { + this.env + .register(path.join(__dirname, './fixtures/generator-module/generators/app'), 'fixtures:generator-module'); + const meta = this.env.getGeneratorMeta('fixtures:generator-module'); + const Generator = await meta.importGeneratorClass(); + const generator = await meta.instantiate(); + assert.ok(generator instanceof Generator); + }); + it('intantiateHelp should return an instance with help option', async function () { + this.env + .register(path.join(__dirname, './fixtures/generator-module/generators/app'), 'fixtures:generator-module'); + const meta = this.env.getGeneratorMeta('fixtures:generator-module'); + const generator = await meta.instantiateHelp(); + assert.strictEqual(generator.options.help, true); + }); + }); + describe('#run() a ts generator', () => { beforeEach(function () { this.env