Skip to content

Commit

Permalink
ESM support for migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-knight committed May 5, 2021
1 parent 0f75ada commit f1da725
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/env/migrationsDir.js
@@ -1,5 +1,6 @@
const fs = require("fs-extra");
const path = require("path");
const url = require("url");
const crypto = require("crypto");
const config = require("./config");

Expand Down Expand Up @@ -94,11 +95,19 @@ module.exports = {

async loadMigration(fileName) {
const migrationsDir = await resolveMigrationsDirPath();
return require(path.join(migrationsDir, fileName)); // eslint-disable-line
const migrationPath = path.join(migrationsDir, fileName);
try {
return require(migrationPath); // eslint-disable-line
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
return import(url.pathToFileURL(migrationPath));
}
throw e;
}
},

async loadFileHash(fileName) {
const migrationsDir = await resolveMigrationsDirPath();
const migrationsDir = await resolveMigrationsDirPath();
const filePath = path.join(migrationsDir, fileName)
const hash = crypto.createHash('sha256');
const input = await fs.readFile(filePath);
Expand Down

0 comments on commit f1da725

Please sign in to comment.