fix: seeds file-system directory

This commit is contained in:
Ahmed Bouhuolia
2025-11-30 22:59:48 +02:00
parent 3648fb3ffc
commit 66969753b1
2 changed files with 34 additions and 3 deletions

View File

@@ -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}`);
}