diff --git a/src/modules/migrations/run.ts b/src/modules/migrations/run.ts index 9dadfc3f..eab6ae25 100644 --- a/src/modules/migrations/run.ts +++ b/src/modules/migrations/run.ts @@ -3,6 +3,7 @@ import * as path from "path"; import { logInfo, LogOptions } from "../../log.js"; import { createClient } from "../../utils/client.js"; +import { apply } from "../../utils/function.js"; import { AnyOnePropertyOf } from "../../utils/types.js"; import { Migration, MigrationOrder } from "./models/migration.js"; import { MigrationOperation, MigrationStatus, SaveStatus, Status, StatusPlugin } from "./models/status.js"; @@ -75,16 +76,17 @@ export const withMigrationsToRun = async ( ) => { const operation = params.rollback ? "rollback" : "run"; + const absoluteDirectoryPath = path.resolve(params.migrationsFolder); + const absoluteStatusPath = apply(p => path.resolve(p), params.statusPlugins); + const { readStatus, saveStatus } = handleErr( - await loadStatusFunctions(params.statusPlugins, params.migrationsFolder), + await loadStatusFunctions(absoluteStatusPath, absoluteDirectoryPath), params, ); const status = handleErr(await loadStatus(readStatus), params); const environmentStatus = status[params.environmentId] ?? []; - const absoluteDirectoryPath = path.resolve(params.migrationsFolder); - const migrations = handleErr(await loadMigrationFiles(absoluteDirectoryPath), params); const migrationsToRun = filterMigrationsToRun(migrations, environmentStatus, operation, params); @@ -100,16 +102,15 @@ export const withMigrationsToRun = async ( const loadStatusFunctions = async ( pluginsPath: string | undefined, migrationsFolder: string, -): Promise> => { - const absoluteDirectoryPath = path.resolve(migrationsFolder); - - return pluginsPath ? await loadStatusPlugin(pluginsPath) : Promise.resolve({ - value: { - readStatus: createDefaultReadStatus(absoluteDirectoryPath), - saveStatus: createDefaultWriteStatus(absoluteDirectoryPath), - }, - }); -}; +): Promise> => + pluginsPath + ? await loadStatusPlugin(pluginsPath) + : Promise.resolve({ + value: { + readStatus: createDefaultReadStatus(migrationsFolder), + saveStatus: createDefaultWriteStatus(migrationsFolder), + }, + }); const filterMigrationsToRun = ( migrations: ReadonlyArray, diff --git a/src/modules/migrations/utils/statusUtils.ts b/src/modules/migrations/utils/statusUtils.ts index 1413a049..e198d4f0 100644 --- a/src/modules/migrations/utils/statusUtils.ts +++ b/src/modules/migrations/utils/statusUtils.ts @@ -1,5 +1,5 @@ -import * as fs from "fs"; -import * as path from "path"; +import * as fs from "node:fs"; +import * as path from "node:path"; import { MigrationOperation, @@ -38,12 +38,12 @@ export const createDefaultWriteStatus = (folderPath: string) => async (data: Sta fs.writeFileSync(statusPath, JSON.stringify(data, null, 2), { flag: "w" }); }; -export const loadStatusPlugin = async (path: string): Promise> => { - if (!fs.existsSync(path)) { - return { err: `Provided plugins path ${path} does not exists` }; +export const loadStatusPlugin = async (pluginsPath: string): Promise> => { + if (!fs.existsSync(pluginsPath)) { + return { err: `Provided plugins path ${pluginsPath} does not exists` }; } - const pluginModule = await import(path); + const pluginModule = await import(pluginsPath); return { value: statusPluginSchema.parse(pluginModule) }; };