From 66969753b1c1f35959456de43ff03ee2ebf84d10 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Sun, 30 Nov 2025 22:59:48 +0200 Subject: [PATCH] fix: seeds file-system directory --- .../src/libs/migration-seed/FsMigrations.ts | 8 ++++- .../server/src/libs/migration-seed/Utils.ts | 29 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/server/src/libs/migration-seed/FsMigrations.ts b/packages/server/src/libs/migration-seed/FsMigrations.ts index f5785e857..353e06c24 100644 --- a/packages/server/src/libs/migration-seed/FsMigrations.ts +++ b/packages/server/src/libs/migration-seed/FsMigrations.ts @@ -11,6 +11,7 @@ class FsMigrations { private sortDirsSeparately: boolean; private migrationsPaths: string[]; private loadExtensions: string[]; + private seedsDirectory: string; /** * Constructor method. @@ -30,6 +31,8 @@ class FsMigrations { } this.migrationsPaths = migrationDirectories; this.loadExtensions = loadExtensions || DEFAULT_LOAD_EXTENSIONS; + // Store the seeds directory (first path is the seeds directory) + this.seedsDirectory = migrationDirectories[0] || ''; } /** @@ -93,7 +96,10 @@ class FsMigrations { * @returns {string} */ public getMigration(migration: MigrateItem): string { - return importWebpackSeedModule(migration.file.replace('.ts', '')); + return importWebpackSeedModule( + migration.file.replace('.ts', ''), + this.seedsDirectory, + ); } } diff --git a/packages/server/src/libs/migration-seed/Utils.ts b/packages/server/src/libs/migration-seed/Utils.ts index d6f99b1de..b44258a5e 100644 --- a/packages/server/src/libs/migration-seed/Utils.ts +++ b/packages/server/src/libs/migration-seed/Utils.ts @@ -1,5 +1,6 @@ // @ts-nocheck import * as fs from 'fs'; +import * as path from 'path'; /** * Detarmines the module type of the given file path. @@ -35,8 +36,32 @@ export async function importFile(filepath: string): any { /** * * @param {string} moduleName + * @param {string} seedsDirectory - The seeds directory path from config * @returns */ -export async function importWebpackSeedModule(moduleName: string): any { - return import(`../../database/seeds/core/${moduleName}`); +export async function importWebpackSeedModule( + moduleName: string, + seedsDirectory: string, +): any { + // Convert the seeds directory to a relative path from this file's location + const utilsDir = __dirname; + const seedsDirAbsolute = path.isAbsolute(seedsDirectory) + ? seedsDirectory + : path.resolve(process.cwd(), seedsDirectory); + + // Get relative path from Utils.js location to seeds directory + const relativePath = path.relative(utilsDir, seedsDirAbsolute); + + // Convert to forward slashes for import (works on all platforms) + const importPath = relativePath.split(path.sep).join('/'); + + // Construct the import path (add ./ prefix if not already present, or handle empty/current dir) + let finalPath = importPath; + if (!finalPath || finalPath === '.') { + finalPath = './'; + } else if (!finalPath.startsWith('.')) { + finalPath = `./${finalPath}`; + } + + return import(`${finalPath}/${moduleName}`); }