Skip to content

Commit

Permalink
#3751: Esm interop flag (#3616)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr D authored and kibertoad committed Jan 14, 2020
1 parent 4a2fa3b commit 6508602
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bin/cli.js
Expand Up @@ -30,6 +30,10 @@ const fsPromised = {
};

function initKnex(env, opts) {
if (opts.esm) {
// enable esm interop via 'esm' module
require = require('esm')(module);
}
checkLocalModule(env);
if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
Expand Down Expand Up @@ -110,7 +114,8 @@ function invoke(env) {
.option(
'--env [name]',
'environment, default: process.env.NODE_ENV || development'
);
)
.option('--esm', 'Enable ESM interop.');

commander
.command('init')
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -32,6 +32,7 @@
"colorette": "1.1.0",
"commander": "^4.1.0",
"debug": "4.1.1",
"esm": "^3.2.25",
"getopts": "2.2.5",
"inherits": "~2.0.4",
"interpret": "^2.0.0",
Expand Down
16 changes: 16 additions & 0 deletions test/jake-util/knexfile-esm/.eslintrc.json
@@ -0,0 +1,16 @@
{
"extends": "../../../.eslintrc.js",
"env": {
"browser": true,
"es6": true
},
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {}
}
14 changes: 14 additions & 0 deletions test/jake-util/knexfile-esm/knexfile.js
@@ -0,0 +1,14 @@
const config = {
client: 'sqlite3',
connection: {
filename: '../test.sqlite3',
},
migrations: {
directory: './migrations',
},
seeds: {
directory: './seeds',
},
};
/** Named exports */
export const { client, connection, migrations, seeds } = config;
2 changes: 2 additions & 0 deletions test/jake-util/knexfile-esm/migrations/one.js
@@ -0,0 +1,2 @@
export function up(...args) {}
export function down(...args) {}
1 change: 1 addition & 0 deletions test/jake-util/knexfile-esm/seeds/one.js
@@ -0,0 +1 @@
export function seed(...args) {}
14 changes: 14 additions & 0 deletions test/jake/jakelib/migrate-test.js
Expand Up @@ -734,6 +734,20 @@ test('migrate:list prints migrations both completed and pending', async (temp) =
);
});

test('migrate runs "esm" modules', (temp) => {
const db = knexfile.connection.filename;
if (fs.existsSync(db)) {
fs.unlinkSync(db);
}

return assertExec(
`node ${KNEX} --esm migrate:latest --knexfile=test/jake-util/knexfile-esm/knexfile.js --knexpath=../knex.js`
).then(({ stdout }) => {
assert.include(stdout, 'Batch 1 run: 1 migrations');
assert.include(stdout, 'one.js');
});
});

module.exports = {
taskList,
};
10 changes: 10 additions & 0 deletions test/jake/jakelib/seed-test.js
Expand Up @@ -55,6 +55,16 @@ test(taskList, 'Handles seeding errors correctly', (temp) => {
});
});

test(taskList, 'seed:run runs "esm" files', () => {
return assertExec(
`node ${KNEX} --esm seed:run --knexfile=test/jake-util/knexfile-esm/knexfile.js`
).then(({ stdout }) => {
assert.include(stdout, 'Ran 1 seed files');
assert.notInclude(stdout, 'first.js');
assert.notInclude(stdout, 'second.js');
});
});

module.exports = {
taskList,
};

0 comments on commit 6508602

Please sign in to comment.