diff --git a/src/module.ts b/src/module.ts index 966f90a..d06da5c 100644 --- a/src/module.ts +++ b/src/module.ts @@ -10,8 +10,8 @@ import defu from "defu"; // Import utility functions import { - checkIfMigrationsFolderExists, - checkIfPrismaSchemaExists, + getMigrationsFolder, + getPrismaSchema, formatSchema, initPrisma, installStudio, @@ -79,10 +79,6 @@ export default defineNuxtModule({ const skipAllPrompts = options.skipPrompts || npmLifecycleEvent === "dev:build"; - const PRISMA_SCHEMA_CMD = options.prismaSchemaPath - ? ["--schema", options.prismaSchemaPath] - : []; - /** * Helper function to prepare the module configuration */ @@ -136,6 +132,18 @@ export default defineNuxtModule({ ? resolveProject(options.prismaRoot) // Combines paths safely : PROJECT_PATH; + // Check if the Prisma schema exists + const resolvedPrismaSchema = getPrismaSchema([ + resolveProject(LAYER_PATH, "schema.prisma"), + resolveProject(LAYER_PATH, "prisma", "schema.prisma"), + resolveProject(LAYER_PATH, "prisma", "schema"), + ]); + + const PRISMA_SCHEMA_CMD = [ + "--schema", + options.prismaSchemaPath || resolvedPrismaSchema || "", + ]; + // Ensure Prisma CLI is installed if required if (options.installCLI) { log(PREDEFINED_LOG_MESSAGES.installPrismaCLI.action); @@ -143,7 +151,7 @@ export default defineNuxtModule({ try { await ensureDependencyInstalled("prisma", { cwd: PROJECT_PATH, - dev: true + dev: true, }); log(PREDEFINED_LOG_MESSAGES.installPrismaCLI.success); } catch (error) { @@ -156,21 +164,16 @@ export default defineNuxtModule({ ); } - // Check if Prisma schema exists - const prismaSchemaExists = checkIfPrismaSchemaExists([ - resolveProject(LAYER_PATH, "prisma", "schema.prisma"), - resolveProject(LAYER_PATH, "prisma", "schema"), - ]); - /** * Handle Prisma migrations workflow */ const prismaMigrateWorkflow = async () => { - const migrationFolderExists = checkIfMigrationsFolderExists( + const migrationFolder = getMigrationsFolder([ + resolveProject(LAYER_PATH, "migrations"), resolveProject(LAYER_PATH, "prisma", "migrations"), - ); + ]); - if (migrationFolderExists || !options.runMigration) { + if (migrationFolder || !options.runMigration) { log(PREDEFINED_LOG_MESSAGES.skipMigrations); return; } @@ -206,7 +209,10 @@ export default defineNuxtModule({ rootDir: PROJECT_PATH, provider: "sqlite", }); - await writeToSchema(`${LAYER_PATH}/prisma/schema.prisma`); + + if (resolvedPrismaSchema) { + await writeToSchema(resolvedPrismaSchema); + } }; /** @@ -240,7 +246,7 @@ export default defineNuxtModule({ }; // Execute workflows sequentially - if (!prismaSchemaExists) { + if (!resolvedPrismaSchema) { await prismaInitWorkflow(); } await prismaMigrateWorkflow(); @@ -249,7 +255,7 @@ export default defineNuxtModule({ if (options.generateClient) { if (options.installClient) { await ensureDependencyInstalled("@prisma/client", { - cwd: PROJECT_PATH + cwd: PROJECT_PATH, }); } await generatePrismaClient( diff --git a/src/package-utils/log-helpers.ts b/src/package-utils/log-helpers.ts index 5fbbeac..bcb6102 100644 --- a/src/package-utils/log-helpers.ts +++ b/src/package-utils/log-helpers.ts @@ -26,7 +26,7 @@ export const PREDEFINED_LOG_MESSAGES = { success: `Successfully installed Prisma CLI.`, error: `Failed to install Prisma CLI.`, }, - checkIfPrismaSchemaExists: { + getPrismaSchema: { yes: "Prisma schema file exists.", no: "Prisma schema file does not exist.", }, @@ -34,7 +34,7 @@ export const PREDEFINED_LOG_MESSAGES = { action: "Initializing Prisma project...\n", error: "Failed to initialize Prisma project.", }, - checkIfMigrationsFolderExists: { + getMigrationsFolder: { success: "Database migrations folder exists.", error: "Database migrations folder does not exist.", }, diff --git a/src/package-utils/setup-helpers.ts b/src/package-utils/setup-helpers.ts index 1c63321..5442d65 100644 --- a/src/package-utils/setup-helpers.ts +++ b/src/package-utils/setup-helpers.ts @@ -23,17 +23,15 @@ export type PrismaInitOptions = { rootDir: string; }; -export function checkIfPrismaSchemaExists(paths: string[]) { - const exists = paths.reduce((prev, current) => { - return existsSync(current) || prev; - }, false); - - if (exists) { - logSuccess(PREDEFINED_LOG_MESSAGES.checkIfPrismaSchemaExists.yes); - return true; +export function getPrismaSchema(paths: string[]): string | false { + for (const path of paths) { + if (existsSync(path)) { + logSuccess(PREDEFINED_LOG_MESSAGES.getPrismaSchema.yes); + return path; + } } - logError(PREDEFINED_LOG_MESSAGES.checkIfPrismaSchemaExists.no); + logError(PREDEFINED_LOG_MESSAGES.getPrismaSchema.no); return false; } @@ -106,13 +104,15 @@ export async function initPrisma({ } } -export function checkIfMigrationsFolderExists(path: string) { - if (existsSync(path)) { - logSuccess(PREDEFINED_LOG_MESSAGES.checkIfMigrationsFolderExists.success); - return true; +export function getMigrationsFolder(paths: string[]): string | false { + for (const path of paths) { + if (existsSync(path)) { + logSuccess(PREDEFINED_LOG_MESSAGES.getMigrationsFolder.success); + return path; + } } - logError(PREDEFINED_LOG_MESSAGES.checkIfMigrationsFolderExists.error); + logError(PREDEFINED_LOG_MESSAGES.getMigrationsFolder.error); return false; }