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

Fix for ES Module detection using npm@7 (#4295) #4296

Merged
merged 6 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions lib/migrations/migrate/Migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,16 @@ class Migrator {

const transactionForAll =
!this.config.disableTransactions &&
!migrations.some((migration) => {
const migrationContents = this.config.migrationSource.getMigration(
migration
);
return !this._useTransaction(migrationContents);
});
!(
await Promise.all(
migrations.map(async (migration) => {
const migrationContents = await this.config.migrationSource.getMigration(
migration
);
return !this._useTransaction(migrationContents);
})
)
).some((e) => e);
richardsimko marked this conversation as resolved.
Show resolved Hide resolved

if (transactionForAll) {
return this.knex.transaction((trx) => {
Expand Down Expand Up @@ -133,17 +137,22 @@ class Migrator {
migrationToRun = newMigrations[0];
}

return {
migrationToRun,
useTransaction: this._useTransaction(
this.config.migrationSource.getMigration(migrationToRun)
),
};
})
.then(({ migrationToRun, useTransaction }) => {
const migrationsToRun = [];
if (migrationToRun) {
migrationsToRun.push(migrationToRun);
}

const transactionForAll =
!this.config.disableTransactions &&
(!migrationToRun ||
this._useTransaction(
this.config.migrationSource.getMigration(migrationToRun)
));
(!migrationToRun || useTransaction);

if (transactionForAll) {
return this.knex.transaction((trx) => {
Expand Down
7 changes: 3 additions & 4 deletions lib/migrations/util/import-file.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// When run via npm, we can leverage the injected environment variables to infer the import type
const isTypeModule = process.env.npm_package_type === 'module';
const isModuleType = require('./is-module-type');

/**
* imports 'mjs', else requires.
* NOTE: require me late!
* @param {string} filepath
* @todo WARN on version 10 and '--experimental-modules' and '--esm'
*/
module.exports = function importFile(filepath) {
return isTypeModule || filepath.endsWith('.mjs')
module.exports = async function importFile(filepath) {
return (await isModuleType(filepath))
? import(require('url').pathToFileURL(filepath))
: require(filepath);
};
14 changes: 14 additions & 0 deletions lib/migrations/util/is-module-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { readFile } = require('./fs');

module.exports = async function isModuleType(filepath) {
if (process.env.npm_package_json) {
// npm >= 7.0.0
const packageJson = JSON.parse(
await readFile(process.env.npm_package_json, 'utf-8')
);
if (packageJson.type === 'module') {
return true;
}
}
return process.env.npm_package_type === 'module' || filepath.endsWith('.mjs');
};
1 change: 1 addition & 0 deletions test/db-less-test-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('Util Tests', function () {
// Unit Tests for utilities.
require('./unit/query/string');
require('./unit/migrations/util/fs');
require('./unit/migrations/util/is-module-type');
require('./unit/util/nanoid');
require('./unit/util/save-async-stack');
require('./unit/util/comma-no-paren-regex');
Expand Down
51 changes: 51 additions & 0 deletions test/unit/migrations/util/is-module-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const path = require('path');

const { expect } = require('chai');
const isModuleType = require('../../../../lib/migrations/util/is-module-type.js');
require('../../../util/chai-setup');

describe('isModuleType', () => {
let originalEnv = {};

before(() => {
originalEnv = { ...process.env };
process.env = {};
});

after(() => {
process.env = { ...originalEnv };
});

beforeEach(() => {
delete process.env.npm_package_type;
delete process.env.npm_package_json;
});

it('should return true if the file is a .mjs file', async () => {
expect(await isModuleType('test.mjs')).to.be.true;
});

it('should return true if type=module with npm < 7.0.0', async () => {
process.env.npm_package_type = 'module';
expect(await isModuleType('test.js')).to.be.true;
});

it('should return false if type=commonjs with npm < 7.0.0', async () => {
process.env.npm_package_type = 'commonjs';
expect(await isModuleType('test.js')).to.be.false;
});

it('should return true if type=module with npm >= 7.0.0', async () => {
process.env.npm_package_json = path.normalize(
__dirname + '/test/package-module.json'
);
expect(await isModuleType('test.js')).to.be.true;
});

it('should return false if type=commonjs with npm >= 7.0.0', async () => {
process.env.npm_package_json = path.normalize(
__dirname + '/test/package-commonjs.json'
);
expect(await isModuleType('test.js')).to.be.false;
});
});
3 changes: 3 additions & 0 deletions test/unit/migrations/util/test/package-commonjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
3 changes: 3 additions & 0 deletions test/unit/migrations/util/test/package-module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}