Skip to content

Commit f80ba7f

Browse files
committed
Resolve plugins path properly in migration commands
1 parent ac25622 commit f80ba7f

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/modules/migrations/run.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path";
33

44
import { logInfo, LogOptions } from "../../log.js";
55
import { createClient } from "../../utils/client.js";
6+
import { apply } from "../../utils/function.js";
67
import { AnyOnePropertyOf } from "../../utils/types.js";
78
import { Migration, MigrationOrder } from "./models/migration.js";
89
import { MigrationOperation, MigrationStatus, SaveStatus, Status, StatusPlugin } from "./models/status.js";
@@ -75,16 +76,17 @@ export const withMigrationsToRun = async (
7576
) => {
7677
const operation = params.rollback ? "rollback" : "run";
7778

79+
const absoluteDirectoryPath = path.resolve(params.migrationsFolder);
80+
const absoluteStatusPath = apply(p => path.resolve(p), params.statusPlugins);
81+
7882
const { readStatus, saveStatus } = handleErr(
79-
await loadStatusFunctions(params.statusPlugins, params.migrationsFolder),
83+
await loadStatusFunctions(absoluteStatusPath, absoluteDirectoryPath),
8084
params,
8185
);
8286

8387
const status = handleErr(await loadStatus(readStatus), params);
8488
const environmentStatus = status[params.environmentId] ?? [];
8589

86-
const absoluteDirectoryPath = path.resolve(params.migrationsFolder);
87-
8890
const migrations = handleErr(await loadMigrationFiles(absoluteDirectoryPath), params);
8991
const migrationsToRun = filterMigrationsToRun(migrations, environmentStatus, operation, params);
9092

@@ -100,16 +102,15 @@ export const withMigrationsToRun = async (
100102
const loadStatusFunctions = async (
101103
pluginsPath: string | undefined,
102104
migrationsFolder: string,
103-
): Promise<WithErr<StatusPlugin>> => {
104-
const absoluteDirectoryPath = path.resolve(migrationsFolder);
105-
106-
return pluginsPath ? await loadStatusPlugin(pluginsPath) : Promise.resolve({
107-
value: {
108-
readStatus: createDefaultReadStatus(absoluteDirectoryPath),
109-
saveStatus: createDefaultWriteStatus(absoluteDirectoryPath),
110-
},
111-
});
112-
};
105+
): Promise<WithErr<StatusPlugin>> =>
106+
pluginsPath
107+
? await loadStatusPlugin(pluginsPath)
108+
: Promise.resolve({
109+
value: {
110+
readStatus: createDefaultReadStatus(migrationsFolder),
111+
saveStatus: createDefaultWriteStatus(migrationsFolder),
112+
},
113+
});
113114

114115
const filterMigrationsToRun = (
115116
migrations: ReadonlyArray<Migration>,

src/modules/migrations/utils/statusUtils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as fs from "fs";
2-
import * as path from "path";
1+
import * as fs from "node:fs";
2+
import * as path from "node:path";
33

44
import {
55
MigrationOperation,
@@ -38,12 +38,12 @@ export const createDefaultWriteStatus = (folderPath: string) => async (data: Sta
3838
fs.writeFileSync(statusPath, JSON.stringify(data, null, 2), { flag: "w" });
3939
};
4040

41-
export const loadStatusPlugin = async (path: string): Promise<WithErr<StatusPlugin>> => {
42-
if (!fs.existsSync(path)) {
43-
return { err: `Provided plugins path ${path} does not exists` };
41+
export const loadStatusPlugin = async (pluginsPath: string): Promise<WithErr<StatusPlugin>> => {
42+
if (!fs.existsSync(pluginsPath)) {
43+
return { err: `Provided plugins path ${pluginsPath} does not exist.` };
4444
}
4545

46-
const pluginModule = await import(path);
46+
const pluginModule = await import(pluginsPath);
4747

4848
return { value: statusPluginSchema.parse(pluginModule) };
4949
};

0 commit comments

Comments
 (0)