diff --git a/bin/cli.js b/bin/cli.js index e97bbd1df9..4393e4406c 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -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); @@ -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') diff --git a/package.json b/package.json index 19f843ac0c..a214c878df 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/jake-util/knexfile-esm/.eslintrc.json b/test/jake-util/knexfile-esm/.eslintrc.json new file mode 100644 index 0000000000..73cdf1a0f8 --- /dev/null +++ b/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": {} +} diff --git a/test/jake-util/knexfile-esm/knexfile.js b/test/jake-util/knexfile-esm/knexfile.js new file mode 100644 index 0000000000..d10dfaff63 --- /dev/null +++ b/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; diff --git a/test/jake-util/knexfile-esm/migrations/one.js b/test/jake-util/knexfile-esm/migrations/one.js new file mode 100644 index 0000000000..39d6bd5dbc --- /dev/null +++ b/test/jake-util/knexfile-esm/migrations/one.js @@ -0,0 +1,2 @@ +export function up(...args) {} +export function down(...args) {} diff --git a/test/jake-util/knexfile-esm/seeds/one.js b/test/jake-util/knexfile-esm/seeds/one.js new file mode 100644 index 0000000000..711411f249 --- /dev/null +++ b/test/jake-util/knexfile-esm/seeds/one.js @@ -0,0 +1 @@ +export function seed(...args) {} diff --git a/test/jake/jakelib/migrate-test.js b/test/jake/jakelib/migrate-test.js index 68fa67f3d4..d61b3c4b3c 100644 --- a/test/jake/jakelib/migrate-test.js +++ b/test/jake/jakelib/migrate-test.js @@ -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, }; diff --git a/test/jake/jakelib/seed-test.js b/test/jake/jakelib/seed-test.js index f996db2127..b7e15a2f82 100644 --- a/test/jake/jakelib/seed-test.js +++ b/test/jake/jakelib/seed-test.js @@ -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, };