From adbe5264d1032f710ebd4b1034fc30112774ec36 Mon Sep 17 00:00:00 2001 From: Anik Ghosh Date: Fri, 26 Jul 2024 16:33:00 +0600 Subject: [PATCH 1/6] fix: resolve esm file path issue on windows --- examples/2-es-modules/umzug.mjs | 3 ++- src/umzug.ts | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/2-es-modules/umzug.mjs b/examples/2-es-modules/umzug.mjs index 56b7954e..24ae8af3 100644 --- a/examples/2-es-modules/umzug.mjs +++ b/examples/2-es-modules/umzug.mjs @@ -1,6 +1,7 @@ import { Umzug, SequelizeStorage } from 'umzug'; import { Sequelize, DataTypes } from 'sequelize'; import * as path from 'path'; +import os from 'os'; const sequelize = new Sequelize({ dialect: 'sqlite', @@ -10,7 +11,7 @@ const sequelize = new Sequelize({ export const migrator = new Umzug({ migrations: { - glob: ['migrations/*.{js,cjs,mjs}', { cwd: path.dirname(import.meta.url.replace('file://', '')) }], + glob: ['migrations/*.{js,cjs,mjs}', { cwd: path.dirname(import.meta.url.replace( os.platform() === 'win32' ? 'file:///' : 'file://', '')) }], }, context: { sequelize, DataTypes }, storage: new SequelizeStorage({ diff --git a/src/umzug.ts b/src/umzug.ts index 4f4ecc0d..59302238 100644 --- a/src/umzug.ts +++ b/src/umzug.ts @@ -2,6 +2,7 @@ import emittery from 'emittery' import {glob} from 'fast-glob' import * as fs from 'fs' import * as path from 'path' +import { pathToFileURL } from 'url' import * as errorCause from 'pony-cause' import type {CommandLineParserOptions} from './cli' import {UmzugCLI} from './cli' @@ -132,7 +133,8 @@ export class Umzug extends emittery require(filepath) as RunnableMigration } else if (jsExt === '.js' || jsExt === '.mjs') { - loadModule = async () => import(filepath) as Promise> + const fileUrl = pathToFileURL(filepath).href + loadModule = async () => import(fileUrl) as Promise> } else { loadModule = async () => { throw new MissingResolverError(filepath) From d3ec5ad5eea6507bb2380e122584b12a8480d95c Mon Sep 17 00:00:00 2001 From: Anik Ghosh Date: Fri, 26 Jul 2024 17:19:10 +0600 Subject: [PATCH 2/6] fix: resolve '<>' on windows --- test/examples.test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/examples.test.ts b/test/examples.test.ts index 8451b53a..f1e13506 100644 --- a/test/examples.test.ts +++ b/test/examples.test.ts @@ -23,6 +23,11 @@ const cleanup = (cwd: string) => { ) } +// Convert path to use forward slashes and normalize it +const normalizePath = (str: string) => { + return str.replace(/\\+/g, '/') // Convert backslashes to forward slashes +} + examples.forEach(ex => { test(`example ${ex}`, async () => { const dir = path.join(examplesDir, ex) @@ -32,6 +37,9 @@ examples.forEach(ex => { cleanup(dir) + const cwd = path.resolve(process.cwd()) // Get absolute path + const normalizedCwd = normalizePath(cwd) // Normalize cwd path + const stdout = bash .split('\n') .map(line => line.split('#')[0].trim()) @@ -41,7 +49,8 @@ examples.forEach(ex => { let output = execa.sync('sh', ['-c', `${cmd} 2>&1`], {cwd: dir}).stdout output = stripAnsi(output) output = cmd.startsWith('npm') || cmd.endsWith('--help') ? '...' : output // npm commands and `--help` are formatted inconsistently and aren't v relevant - output = output.split(process.cwd()).join('<>') // cwd varies by machine + output = normalizePath(output) + output = output.split(normalizedCwd).join('<>') // cwd varies by machine output = output.replaceAll(/durationSeconds: .*/g, 'durationSeconds: ???') // migrations durations vary by a few milliseconds output = output.replaceAll(/\d{4}.\d{2}.\d{2}T\d{2}.\d{2}.\d{2}/g, '<>') // the river of time flows only in one direction return [`\`${cmd}\` output:`, output] From 0f11ee6d8b1eb1d08e5bb1508a85f7f0bccd9cf7 Mon Sep 17 00:00:00 2001 From: Anik Ghosh Date: Fri, 26 Jul 2024 17:39:22 +0600 Subject: [PATCH 3/6] fix: resolve lint issues with import order and formatting --- src/umzug.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/umzug.ts b/src/umzug.ts index 59302238..a520c325 100644 --- a/src/umzug.ts +++ b/src/umzug.ts @@ -2,8 +2,8 @@ import emittery from 'emittery' import {glob} from 'fast-glob' import * as fs from 'fs' import * as path from 'path' -import { pathToFileURL } from 'url' import * as errorCause from 'pony-cause' +import {pathToFileURL} from 'url' import type {CommandLineParserOptions} from './cli' import {UmzugCLI} from './cli' import type {UmzugStorage} from './storage' From b95aa8060c6d4dab983f59cdfc8d79251dd09d49 Mon Sep 17 00:00:00 2001 From: Misha Kaletsky Date: Tue, 27 Aug 2024 09:02:52 -0400 Subject: [PATCH 4/6] test on windows-latest --- .github/workflows/ci.yml | 7 +++++++ examples/0.5-vanilla-esm/migrate.mjs | 2 ++ 2 files changed, 9 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b02eff0..48089838 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,6 +12,13 @@ jobs: - run: pnpm test -- --coverage - name: Coverage uses: codecov/codecov-action@v3 + run_windows: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + - run: corepack enable + - run: pnpm install + - run: pnpm test create_tgz: runs-on: ubuntu-latest steps: diff --git a/examples/0.5-vanilla-esm/migrate.mjs b/examples/0.5-vanilla-esm/migrate.mjs index 03691b5f..bbecb417 100644 --- a/examples/0.5-vanilla-esm/migrate.mjs +++ b/examples/0.5-vanilla-esm/migrate.mjs @@ -2,6 +2,8 @@ import { Umzug, JSONStorage } from 'umzug'; const __dirname = new URL('.', import.meta.url).pathname.replace(/\/$/, ''); +if (Math.random()) throw new Error(`__dirname: ${__dirname}`) + export const migrator = new Umzug({ migrations: { glob: 'migrations/*.*js', From 7b23a1176d152d75821e1d95b7856b8df0f74aa7 Mon Sep 17 00:00:00 2001 From: Misha Kaletsky Date: Tue, 27 Aug 2024 10:09:23 -0400 Subject: [PATCH 5/6] fileURLToPath --- examples/0.5-vanilla-esm/migrate.mjs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/0.5-vanilla-esm/migrate.mjs b/examples/0.5-vanilla-esm/migrate.mjs index bbecb417..9c12ac15 100644 --- a/examples/0.5-vanilla-esm/migrate.mjs +++ b/examples/0.5-vanilla-esm/migrate.mjs @@ -1,8 +1,7 @@ import { Umzug, JSONStorage } from 'umzug'; +import { fileURLToPath } from 'url' -const __dirname = new URL('.', import.meta.url).pathname.replace(/\/$/, ''); - -if (Math.random()) throw new Error(`__dirname: ${__dirname}`) +const __dirname = fileURLToPath(new URL('.', import.meta.url)).replace(/\/$/, '') export const migrator = new Umzug({ migrations: { From a5198db214036da9937e319763f661b78f0c747e Mon Sep 17 00:00:00 2001 From: Misha Kaletsky Date: Tue, 27 Aug 2024 10:22:24 -0400 Subject: [PATCH 6/6] use a regex --- examples/2-es-modules/umzug.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/2-es-modules/umzug.mjs b/examples/2-es-modules/umzug.mjs index 24ae8af3..23ec4f65 100644 --- a/examples/2-es-modules/umzug.mjs +++ b/examples/2-es-modules/umzug.mjs @@ -11,7 +11,7 @@ const sequelize = new Sequelize({ export const migrator = new Umzug({ migrations: { - glob: ['migrations/*.{js,cjs,mjs}', { cwd: path.dirname(import.meta.url.replace( os.platform() === 'win32' ? 'file:///' : 'file://', '')) }], + glob: ['migrations/*.{js,cjs,mjs}', { cwd: path.dirname(import.meta.url.replace(os.platform() === 'win32' ? 'file:///' : 'file://', '')) }], }, context: { sequelize, DataTypes }, storage: new SequelizeStorage({