Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve esm support. #304

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ module.exports = cls => class EnvironmentCommand extends cls {
const generator = await this.create(namespace.namespace, {help: true});
EnvironmentCommand.addGeneratorOptions(namespaceCommand, generator);

namespaceCommand.action(async function () {
debug('Running generator with arguments %o, options %o', this.args, this.opts());
return env.run([namespace.namespace, ...this.args], {...this.opts()});
});
namespaceCommand._parseCommand([], args);
debug('Running generator with arguments %o, options %o', namespaceCommand.args, namespaceCommand.opts());
return env.run([namespace.namespace, ...namespaceCommand.args], {...namespaceCommand.opts()});
}
};
27 changes: 20 additions & 7 deletions lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,16 +673,28 @@ class Environment extends Base {
if (!Generator) {
return Generator;
}
let meta = Generator;
if (Array.isArray(Generator)) {
meta = Generator[1];
Generator = Generator[0];
}
if (typeof Generator.default === 'function') {
Generator.default.resolved = Generator.resolved;
Generator.default.namespace = Generator.namespace;
Generator.default.resolved = meta.resolved;
Generator.default.namespace = meta.namespace;
return Generator.default;
}
if (typeof Generator.createGenerator === 'function') {
const Gen = Generator.createGenerator(this);
Gen.resolved = Generator.resolved;
Gen.namespace = Generator.namespace;
return Gen;
const maybeGenerator = Generator.createGenerator(this);
if (maybeGenerator.then) {
return maybeGenerator.then(Gen => {
Gen.resolved = meta.resolved;
Gen.namespace = meta.namespace;
return Gen;
});
}
maybeGenerator.resolved = meta.resolved;
maybeGenerator.namespace = meta.namespace;
return maybeGenerator;
}
if (typeof Generator !== 'function') {
throw new TypeError('The generator doesn\'t provides a constructor.');
Expand Down Expand Up @@ -999,8 +1011,9 @@ class Environment extends Base {
*
* @param {Object} generator
*/
runGenerator(generator) {
async runGenerator(generator) {
try {
generator = await generator;
generator = this.queueGenerator(generator);
} catch (error) {
return Promise.reject(error);
Expand Down
29 changes: 5 additions & 24 deletions lib/store.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const debug = require('debug')('yeoman:environment:store');
const path = require('path');
const {pathToFileURL} = require('url');

const {requireOrImport} = require('./util/esm');

/**
* The Generator store
Expand Down Expand Up @@ -46,22 +46,7 @@ class Store {

Object.defineProperty(this._generators, namespace, {
get() {
if (path.extname(resolvedPath) === '.cjs') {
return require(resolvedPath);
}
if (path.extname(resolvedPath) === '.mjs') {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
return import(pathToFileURL(resolvedPath));
}
try {
return require(resolvedPath);
} catch (error) {
if (error.code !== 'ERR_REQUIRE_ESM') {
throw error;
}
// eslint-disable-next-line node/no-unsupported-features/es-syntax
return import(pathToFileURL(resolvedPath));
}
return requireOrImport(resolvedPath);
},
enumerable: true,
configurable: true
Expand Down Expand Up @@ -90,17 +75,13 @@ class Store {
return;
}

// Assign even to Promises for sync compatibility
Object.assign(maybeGenerator, this._meta[namespace]);

if (maybeGenerator.then) {
return maybeGenerator.then(esmGenerator => {
const Generator = esmGenerator.default;
Object.assign(Generator, this._meta[namespace]);
return Generator;
return [esmGenerator, {...this._meta[namespace]}];
});
}

Object.assign(maybeGenerator, this._meta[namespace]);
return maybeGenerator;
}

Expand Down
23 changes: 23 additions & 0 deletions lib/util/esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('path');
const {pathToFileURL} = require('url');

module.exports = {
requireOrImport(fileToImport) {
if (path.extname(fileToImport) === '.cjs') {
return require(fileToImport);
}
if (path.extname(fileToImport) === '.mjs') {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
return import(pathToFileURL(fileToImport));
}
try {
return require(fileToImport);
} catch (error) {
if (error.code !== 'ERR_REQUIRE_ESM') {
throw error;
}
// eslint-disable-next-line node/no-unsupported-features/es-syntax
return import(pathToFileURL(fileToImport));
}
}
};
3 changes: 2 additions & 1 deletion lib/util/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const debug = require('debug')('yeoman:environment:repository');
const Arborist = require('@npmcli/arborist');

const logger = require('./log');
const {requireOrImport} = require('./esm');

const REPOSITORY_FOLDER = '.yo-repository';

Expand Down Expand Up @@ -223,7 +224,7 @@ class YeomanRepository {
}
const absolutePath = this.resolvePackagePath(packageName, modulePath);
debug('Loading module at %s', absolutePath);
return require(absolutePath);
return requireOrImport(absolutePath);
}
}

Expand Down
35 changes: 35 additions & 0 deletions test/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ const GROUPED_QUEUE_VERSION = require('grouped-queue/package.json').version;
const sinonTest = sinonTestFactory(sinon);

describe('Environment', () => {
let mockedDefault;
let MockedGenerator;

beforeEach(function () {
this.env = new Environment({skipInstall: true, sharedOptions: {sharedConstructorData: {}}});

MockedGenerator = class MockedGenerator extends Generator {};
mockedDefault = sinon.stub();
MockedGenerator.prototype.mockedDefault = mockedDefault;
});

afterEach(function () {
Expand Down Expand Up @@ -514,6 +521,34 @@ describe('Environment', () => {
});
});
});
describe('with createGenerator', () => {
beforeEach(async function () {
this.env.registerStub(MockedGenerator, 'mocked-generator');
this.env
.register(path.join(__dirname, './fixtures/generator-esm/generators/create/index.js'), 'esm:create');
});

it('runs a registered generator', function () {
return this.env.run(['esm:create']).then(() => {
assert.ok(mockedDefault.calledOnce);
});
});
});
describe('with inherited createGenerator', () => {
beforeEach(async function () {
this.env.registerStub(MockedGenerator, 'mocked-generator');
this.env
.register(path.join(__dirname, './fixtures/generator-esm/generators/create/index.js'), 'esm:create');
this.env
.register(path.join(__dirname, './fixtures/generator-esm/generators/create-inherited/index.js'), 'esm:create-inherited');
});

it('runs a registered generator', function () {
return this.env.run(['esm:create-inherited']).then(() => {
assert.ok(mockedDefault.calledOnce);
});
});
});
});

describe('#registerModulePath()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export async function createGenerator(env) {
const ParentGenerator = await env.requireGenerator('esm:create');
return class NewGenerator extends ParentGenerator {
default() {
super.mockedDefault();
}
};
}
8 changes: 8 additions & 0 deletions test/fixtures/generator-esm/generators/create/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export async function createGenerator(env) {
const ParentGenerator = await env.requireGenerator('mocked-generator');
return class NewGenerator extends ParentGenerator {
default() {
super.mockedDefault();
}
};
}