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

chore: use node:process cwd #1215

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
7 changes: 4 additions & 3 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 Down Expand Up @@ -261,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 @@ -438,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
9 changes: 5 additions & 4 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import { createReadStream, createWriteStream } from 'node:fs';
import { mkdir, readdir } from 'node:fs/promises';
import { basename, extname, resolve } from 'node:path';
import { basename, extname, join, 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 @@ -136,15 +137,15 @@ export class Migration implements RunMigration {

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

// file name looks like migrations/1391877300255_migration-title.js
const newFile = `${directory}/${time}${SEPARATOR}${name}.${suffix}`;
const newFile = join(directory, `${time}${SEPARATOR}${name}.${suffix}`);

// copy the default migration template to the new file location
await new Promise((resolve, reject) => {
Expand Down