Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import defu from "defu";

// Import utility functions
import {
checkIfMigrationsFolderExists,
checkIfPrismaSchemaExists,
getMigrationsFolder,
getPrismaSchema,
formatSchema,
initPrisma,
installStudio,
Expand Down Expand Up @@ -79,10 +79,6 @@ export default defineNuxtModule<PrismaExtendedModule>({
const skipAllPrompts =
options.skipPrompts || npmLifecycleEvent === "dev:build";

const PRISMA_SCHEMA_CMD = options.prismaSchemaPath
? ["--schema", options.prismaSchemaPath]
: [];

/**
* Helper function to prepare the module configuration
*/
Expand Down Expand Up @@ -136,14 +132,26 @@ export default defineNuxtModule<PrismaExtendedModule>({
? 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);

try {
await ensureDependencyInstalled("prisma", {
cwd: PROJECT_PATH,
dev: true
dev: true,
});
log(PREDEFINED_LOG_MESSAGES.installPrismaCLI.success);
} catch (error) {
Expand All @@ -156,21 +164,16 @@ export default defineNuxtModule<PrismaExtendedModule>({
);
}

// 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;
}
Expand Down Expand Up @@ -206,7 +209,10 @@ export default defineNuxtModule<PrismaExtendedModule>({
rootDir: PROJECT_PATH,
provider: "sqlite",
});
await writeToSchema(`${LAYER_PATH}/prisma/schema.prisma`);

if (resolvedPrismaSchema) {
await writeToSchema(resolvedPrismaSchema);
}
};

/**
Expand Down Expand Up @@ -240,7 +246,7 @@ export default defineNuxtModule<PrismaExtendedModule>({
};

// Execute workflows sequentially
if (!prismaSchemaExists) {
if (!resolvedPrismaSchema) {
await prismaInitWorkflow();
}
await prismaMigrateWorkflow();
Expand All @@ -249,7 +255,7 @@ export default defineNuxtModule<PrismaExtendedModule>({
if (options.generateClient) {
if (options.installClient) {
await ensureDependencyInstalled("@prisma/client", {
cwd: PROJECT_PATH
cwd: PROJECT_PATH,
});
}
await generatePrismaClient(
Expand Down
4 changes: 2 additions & 2 deletions src/package-utils/log-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ 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.",
},
initPrisma: {
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.",
},
Expand Down
28 changes: 14 additions & 14 deletions src/package-utils/setup-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down