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(runner): resolve migration file for esm #1207

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions bin/node-pg-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type { DotenvConfigOptions } from 'dotenv';
import { default as migrationRunner, Migration } from 'node-pg-migrate';
import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { resolve } from 'node:path';
import { join, resolve } from 'node:path';
import { cwd } from 'node:process';
import { format } from 'node:util';
import type { ClientConfig } from 'pg';
// This needs to be imported with .js extension, otherwise it will fail in esm
Expand All @@ -21,10 +22,7 @@ process.on('uncaughtException', (err) => {
process.exit(1);
});

const crossRequire = createRequire(
// @ts-expect-error: ignore until esm only
import.meta.url || __dirname
);
const crossRequire = createRequire(resolve('non-existing-file.js'));

function tryRequire<TModule = unknown>(moduleName: string): TModule | null {
try {
Expand Down Expand Up @@ -264,7 +262,7 @@ function readTsconfig() {
const json5 = tryRequire<typeof import('json5')>('json5');

try {
const config = readFileSync(resolve(process.cwd(), tsconfigPath), {
const config = readFileSync(resolve(cwd(), tsconfigPath), {
encoding: 'utf8',
});
tsconfig = json5 ? json5.parse(config) : JSON.parse(config);
Expand Down Expand Up @@ -441,7 +439,7 @@ readTsconfig();
const action = argv._.shift();

// defaults
MIGRATIONS_DIR ??= `${process.cwd()}/migrations`;
MIGRATIONS_DIR ??= join(cwd(), 'migrations');
MIGRATIONS_FILE_LANGUAGE ??= 'js';
MIGRATIONS_FILENAME_FORMAT ??= 'timestamp';
MIGRATIONS_TABLE ??= 'pgmigrations';
Expand Down
21 changes: 5 additions & 16 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import { createReadStream, createWriteStream } from 'node:fs';
import { mkdir, readdir } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { basename, dirname, extname, join, resolve } from 'node:path';
import { basename, extname, resolve } from 'node:path';
import { cwd } from 'node:process';
import type { QueryResult } from 'pg';
import type { DBConnection } from './db';
import MigrationBuilder from './migrationBuilder';
Expand Down Expand Up @@ -135,23 +135,12 @@ export class Migration implements RunMigration {
? now.toISOString().replace(/\D/g, '')
: now.valueOf();

const crossRequire = createRequire(
// @ts-expect-error: ignore until esm only
import.meta.url || __dirname
);
const moduleDir = dirname(
crossRequire.resolve('node-pg-migrate/package.json')
);

const templateFileName =
'templateFileName' in options
? resolve(process.cwd(), options.templateFileName)
? resolve(cwd(), options.templateFileName)
: resolve(
moduleDir,
join(
'templates',
`migration-template.${await resolveSuffix(directory, options)}`
)
'node_modules/node-pg-migrate/templates',
`migration-template.${await resolveSuffix(directory, options)}`
);
const suffix = getSuffixFromFileName(templateFileName);

Expand Down
5 changes: 3 additions & 2 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { extname, relative } from 'node:path';
import { createRequire } from 'node:module';
import { extname, resolve } from 'node:path';
import type { DBConnection } from './db';
import Db from './db';
import type { RunMigration } from './migration';
Expand Down Expand Up @@ -39,7 +40,7 @@ async function loadMigrations(
const actions: MigrationBuilderActions =
extname(filePath) === '.sql'
? await migrateSqlFile(filePath)
: require(relative(__dirname, filePath));
: createRequire(resolve('non-existing-file.js'))(filePath);
shorthands = { ...shorthands, ...actions.shorthands };

return new Migration(
Expand Down
Loading