Skip to content

Commit

Permalink
fix(esm): dont depend on __dirname (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
aprendendofelipe authored Jun 21, 2024
1 parent 9b784e3 commit 1ed93e2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
5 changes: 1 addition & 4 deletions bin/node-pg-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,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('_'));

function tryRequire<TModule = unknown>(moduleName: string): TModule | null {
try {
Expand Down
18 changes: 3 additions & 15 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

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 type { QueryResult } from 'pg';
import type { DBConnection } from './db';
import MigrationBuilder from './migrationBuilder';
Expand Down Expand Up @@ -135,23 +134,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(
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
7 changes: 4 additions & 3 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 @@ -35,11 +36,11 @@ async function loadMigrations(

const migrations = await Promise.all(
files.map(async (file) => {
const filePath = `${options.dir}/${file}`;
const filePath = resolve(options.dir, file);
const actions: MigrationBuilderActions =
extname(filePath) === '.sql'
? await migrateSqlFile(filePath)
: require(relative(__dirname, filePath));
: createRequire(resolve('_'))(filePath);
shorthands = { ...shorthands, ...actions.shorthands };

return new Migration(
Expand Down

0 comments on commit 1ed93e2

Please sign in to comment.