Skip to content

Commit 68fd0d5

Browse files
authored
Merge pull request #724 from joeldenning/issue-721
feat: support ESM projects. resolves #721
2 parents 41ab671 + 05fde89 commit 68fd0d5

File tree

2 files changed

+79
-22
lines changed

2 files changed

+79
-22
lines changed

lib/commands/create-migration.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,38 @@ const log = require('db-migrate-shared').log;
55
const mkdirp = Promise.promisify(require('mkdirp'));
66
const fs = require('fs');
77
const stat = Promise.promisify(fs.stat);
8+
const writeFile = Promise.promisify(fs.writeFile);
89
const yargs = require('yargs');
910
const util = require('util');
11+
const path = require('path');
1012

1113
async function createMigrationDir (dir) {
1214
const res = await stat(dir).catch(_ => {
1315
return { err: true };
1416
});
1517
if (res && res.err === true) {
16-
return mkdirp(dir);
18+
await mkdirp(dir);
1719
}
1820

19-
return Promise.resolve();
21+
// Create migrations/package.json to ensure migration files are
22+
// executed as CJS and not ESM
23+
// https://github.com/db-migrate/node-db-migrate/issues/721
24+
const packageJsonPath = path.resolve(dir, 'package.json');
25+
26+
await stat(packageJsonPath).then(
27+
async () => {
28+
const packageJson = require(packageJsonPath);
29+
packageJson.type = 'commonjs';
30+
const packageJsonStr = JSON.stringify(packageJson, null, 2);
31+
await writeFile(packageJsonPath, packageJsonStr, 'utf-8');
32+
},
33+
async (err) => {
34+
const packageJson = JSON.stringify({
35+
type: 'commonjs'
36+
}, null, 2)
37+
await writeFile(packageJsonPath, packageJson, 'utf-8');
38+
}
39+
)
2040
}
2141

2242
async function executeCreateMigration (internals, config) {

test/integration/create_test.js

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ const fs = require('fs');
88
const path = require('path');
99
const cp = require('child_process');
1010
const dbmUtil = require('db-migrate-shared').util;
11+
const mkdirp = Promise.promisify(require('mkdirp'));
1112

1213
const rmdir = Promise.promisify(require('rimraf'));
14+
const writeFile = Promise.promisify(fs.writeFile);
1315

1416
function wipeMigrations () {
1517
const dir = path.join(__dirname, 'migrations');
@@ -49,9 +51,48 @@ lab.experiment('create', function () {
4951

5052
lab.test('will create a new migration', () => {
5153
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
52-
Code.expect(files.length).to.equal(1);
53-
const file = files[0];
54-
Code.expect(file).to.match(/first-migration\.js$/);
54+
const file = files.find(file => /first-migration\.js$/.test(file));
55+
Code.expect(file).to.exist();
56+
});
57+
58+
lab.test('will create migrations/package.json', () => {
59+
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
60+
const packageJson = files.find(file => /package\.json$/.test(file));
61+
Code.expect(packageJson).to.exist();
62+
})
63+
});
64+
65+
lab.experiment(
66+
'with existing migrations/package.json file', () => {
67+
let exitCode;
68+
69+
lab.before(async () => {
70+
await wipeMigrations();
71+
72+
await mkdirp(path.join(__dirname, 'migrations'));
73+
74+
await writeFile(path.join(__dirname, 'migrations', 'package.json'), '{"name": "test", "type": "module"}');
75+
76+
const db = dbMigrate('create', 'first migration');
77+
// db.stderr.on('data', data => console.log(data.toString()));
78+
// db.stdout.on('data', data => console.log(data.toString()));
79+
80+
const exitCodePromise = new Promise((resolve) => db.on('exit', resolve));
81+
exitCode = await exitCodePromise;
82+
});
83+
84+
lab.test('does not cause an error', () => {
85+
Code.expect(exitCode).to.equal(0);
86+
});
87+
88+
lab.test('will modify an existing migrations/package.json\'s type field', () => {
89+
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'migrations', 'package.json')))
90+
Code.expect(packageJson.type).to.equal("commonjs");
91+
});
92+
93+
lab.test('will preserve other properties in an existing package.json', () => {
94+
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'migrations', 'package.json')))
95+
Code.expect(packageJson.name).to.equal("test");
5596
});
5697
});
5798

@@ -84,7 +125,7 @@ lab.experiment('create', function () {
84125
for (let i = 0; i < files.length; i++) {
85126
const file = files[i];
86127
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
87-
if (stats.isFile()) {
128+
if (stats.isFile() && !/package\.json$/.test(file)) {
88129
Code.expect(file).to.match(/second-migration\.js$/);
89130
}
90131
}
@@ -97,9 +138,8 @@ lab.experiment('create', function () {
97138

98139
lab.test('will create a new migration sql up file', () => {
99140
const files = fs.readdirSync(path.join(__dirname, 'migrations/sqls'));
100-
Code.expect(files.length).to.equal(2);
101-
const file = files[1];
102-
Code.expect(file).to.match(/second-migration-up\.sql$/);
141+
const file = files.find(file => /second-migration-up\.sql$/.test(file));
142+
Code.expect(file).to.exist();
103143
});
104144
}
105145
);
@@ -129,7 +169,7 @@ lab.experiment('create', function () {
129169
for (let i = 0; i < files.length; i++) {
130170
const file = files[i];
131171
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
132-
if (stats.isFile()) {
172+
if (stats.isFile() && !/package\.json$/.test(file)) {
133173
Code.expect(file).to.match(/third-migration\.js$/);
134174
}
135175
}
@@ -142,9 +182,8 @@ lab.experiment('create', function () {
142182

143183
lab.test('will create a new migration sql up file', () => {
144184
const files = fs.readdirSync(path.join(__dirname, 'migrations/sqls'));
145-
Code.expect(files.length).to.equal(2);
146-
const file = files[1];
147-
Code.expect(file).to.match(/third-migration-up\.sql$/);
185+
const file = files.find(file => /third-migration-up\.sql$/.test(file));
186+
Code.expect(file).to.exist();
148187
});
149188
}
150189
);
@@ -177,7 +216,7 @@ lab.experiment('create', function () {
177216
for (let i = 0; i < files.length; i++) {
178217
const file = files[i];
179218
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
180-
if (stats.isFile()) {
219+
if (stats.isFile() && !/package\.json$/.test(file)) {
181220
Code.expect(file).to.match(/fourth-migration\.coffee$/);
182221
}
183222
}
@@ -211,19 +250,17 @@ lab.experiment('create', function () {
211250

212251
lab.test('will create a new migration', () => {
213252
const files = fs.readdirSync(path.join(__dirname, 'migrations/test'));
214-
Code.expect(files.length).to.equal(2);
215-
const file = files[0];
216-
Code.expect(file).to.match(/first-migration\.js$/);
253+
const file = files.find(file => /first-migration\.js$/.test(file));
254+
Code.expect(file).to.exist();
217255
});
218256
lab.test('will create a new migration/test/sqls directory', () => {
219257
const stats = fs.statSync(path.join(__dirname, 'migrations/test/sqls'));
220258
Code.expect(stats.isDirectory()).to.be.true();
221259
});
222260
lab.test('will create a new migration sql up file', () => {
223261
const files = fs.readdirSync(path.join(__dirname, 'migrations/test/sqls'));
224-
Code.expect(files.length).to.equal(2);
225-
const file = files[1];
226-
Code.expect(file).to.match(/first-migration-up\.sql$/);
262+
const file = files.find(file => /first-migration-up\.sql$/.test(file));
263+
Code.expect(file).to.exist();
227264
});
228265
});
229266
}
@@ -254,7 +291,7 @@ lab.experiment('create', function () {
254291
for (let i = 0; i < files.length; i++) {
255292
const file = files[i];
256293
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
257-
if (stats.isFile()) {
294+
if (stats.isFile() && !/package\.json$/.test(file)) {
258295
Code.expect(file).to.match(/fifth-migration\.coffee$/);
259296
}
260297
}
@@ -306,7 +343,7 @@ lab.experiment('create', function () {
306343
for (let i = 0; i < files.length; i++) {
307344
const file = files[i];
308345
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
309-
if (stats.isFile()) {
346+
if (stats.isFile() && !/package\.json$/.test(file)) {
310347
Code.expect(file).to.match(/sixth-migration\.js$/);
311348
}
312349
}

0 commit comments

Comments
 (0)