Skip to content

Commit 3baccd6

Browse files
anikghosh256mmkal
andauthored
fix: resolve esm file path issue and '<<cwd>>' (example tests) on windows (#678)
#676 --------- Co-authored-by: Misha Kaletsky <mmkal@users.noreply.github.com>
1 parent e62d7e8 commit 3baccd6

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ jobs:
1212
- run: pnpm test -- --coverage
1313
- name: Coverage
1414
uses: codecov/codecov-action@v3
15+
run_windows:
16+
runs-on: windows-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
- run: corepack enable
20+
- run: pnpm install
21+
- run: pnpm test
1522
create_tgz:
1623
runs-on: ubuntu-latest
1724
steps:

examples/0.5-vanilla-esm/migrate.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Umzug, JSONStorage } from 'umzug';
2+
import { fileURLToPath } from 'url'
23

3-
const __dirname = new URL('.', import.meta.url).pathname.replace(/\/$/, '');
4+
const __dirname = fileURLToPath(new URL('.', import.meta.url)).replace(/\/$/, '')
45

56
export const migrator = new Umzug({
67
migrations: {

examples/2-es-modules/umzug.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Umzug, SequelizeStorage } from 'umzug';
22
import { Sequelize, DataTypes } from 'sequelize';
33
import * as path from 'path';
4+
import os from 'os';
45

56
const sequelize = new Sequelize({
67
dialect: 'sqlite',
@@ -10,7 +11,7 @@ const sequelize = new Sequelize({
1011

1112
export const migrator = new Umzug({
1213
migrations: {
13-
glob: ['migrations/*.{js,cjs,mjs}', { cwd: path.dirname(import.meta.url.replace('file://', '')) }],
14+
glob: ['migrations/*.{js,cjs,mjs}', { cwd: path.dirname(import.meta.url.replace(os.platform() === 'win32' ? 'file:///' : 'file://', '')) }],
1415
},
1516
context: { sequelize, DataTypes },
1617
storage: new SequelizeStorage({

src/umzug.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {glob} from 'fast-glob'
33
import * as fs from 'fs'
44
import * as path from 'path'
55
import * as errorCause from 'pony-cause'
6+
import {pathToFileURL} from 'url'
67
import type {CommandLineParserOptions} from './cli'
78
import {UmzugCLI} from './cli'
89
import type {UmzugStorage} from './storage'
@@ -132,7 +133,8 @@ export class Umzug<Ctx extends object = object> extends emittery<UmzugEvents<Ctx
132133
// eslint-disable-next-line @typescript-eslint/no-var-requires
133134
loadModule = async () => require(filepath) as RunnableMigration<unknown>
134135
} else if (jsExt === '.js' || jsExt === '.mjs') {
135-
loadModule = async () => import(filepath) as Promise<RunnableMigration<unknown>>
136+
const fileUrl = pathToFileURL(filepath).href
137+
loadModule = async () => import(fileUrl) as Promise<RunnableMigration<unknown>>
136138
} else {
137139
loadModule = async () => {
138140
throw new MissingResolverError(filepath)

test/examples.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const cleanup = (cwd: string) => {
2323
)
2424
}
2525

26+
// Convert path to use forward slashes and normalize it
27+
const normalizePath = (str: string) => {
28+
return str.replace(/\\+/g, '/') // Convert backslashes to forward slashes
29+
}
30+
2631
examples.forEach(ex => {
2732
test(`example ${ex}`, async () => {
2833
const dir = path.join(examplesDir, ex)
@@ -32,6 +37,9 @@ examples.forEach(ex => {
3237

3338
cleanup(dir)
3439

40+
const cwd = path.resolve(process.cwd()) // Get absolute path
41+
const normalizedCwd = normalizePath(cwd) // Normalize cwd path
42+
3543
const stdout = bash
3644
.split('\n')
3745
.map(line => line.split('#')[0].trim())
@@ -41,7 +49,8 @@ examples.forEach(ex => {
4149
let output = execa.sync('sh', ['-c', `${cmd} 2>&1`], {cwd: dir}).stdout
4250
output = stripAnsi(output)
4351
output = cmd.startsWith('npm') || cmd.endsWith('--help') ? '...' : output // npm commands and `--help` are formatted inconsistently and aren't v relevant
44-
output = output.split(process.cwd()).join('<<cwd>>') // cwd varies by machine
52+
output = normalizePath(output)
53+
output = output.split(normalizedCwd).join('<<cwd>>') // cwd varies by machine
4554
output = output.replaceAll(/durationSeconds: .*/g, 'durationSeconds: ???') // migrations durations vary by a few milliseconds
4655
output = output.replaceAll(/\d{4}.\d{2}.\d{2}T\d{2}.\d{2}.\d{2}/g, '<<timestamp>>') // the river of time flows only in one direction
4756
return [`\`${cmd}\` output:`, output]

0 commit comments

Comments
 (0)