Skip to content

Commit

Permalink
refactor: replace own installation functions with nypm ensureDependen…
Browse files Browse the repository at this point in the history
…cyInstalled (#78)

* refactor: replace own installation functions with nypm ensureDependencyInstalled

* style(log): fix typo

* fix(installation): install @prisma/client not as dev

---------

Co-authored-by: Kuuzoo <nilscosmo@hotmail.com>
  • Loading branch information
ankur-arch and Kuuzoo authored Jan 4, 2025
1 parent 6e616be commit 2179d37
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 63 deletions.
33 changes: 21 additions & 12 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ import {
checkIfMigrationsFolderExists,
checkIfPrismaSchemaExists,
formatSchema,
installPrismaClient,
initPrisma,
installPrismaCLI,
installStudio,
isPrismaCLIInstalled,
runMigration,
writeClientInLib,
writeToSchema,
Expand All @@ -26,6 +23,7 @@ import {
import { log, PREDEFINED_LOG_MESSAGES } from "./package-utils/log-helpers";
import type { Prisma } from "@prisma/client";
import { executeRequiredPrompts } from "./package-utils/prompts";
import { ensureDependencyInstalled } from "nypm";

// Module configuration interface
interface ModuleOptions extends Prisma.PrismaClientOptions {
Expand Down Expand Up @@ -140,15 +138,22 @@ export default defineNuxtModule<PrismaExtendedModule>({

// Ensure Prisma CLI is installed if required
if (options.installCLI) {
const prismaInstalled = await isPrismaCLIInstalled(PROJECT_PATH);
if (!prismaInstalled) {
await installPrismaCLI(PROJECT_PATH);
await generatePrismaClient(
PROJECT_PATH,
PRISMA_SCHEMA_CMD,
options.log?.includes("error"),
);
log(PREDEFINED_LOG_MESSAGES.installPrismaCLI.action);

try {
await ensureDependencyInstalled("prisma", {
cwd: PROJECT_PATH,
dev: true
});
log(PREDEFINED_LOG_MESSAGES.installPrismaCLI.success);
} catch (error) {
log(PREDEFINED_LOG_MESSAGES.installPrismaCLI.error);
}
await generatePrismaClient(
PROJECT_PATH,
PRISMA_SCHEMA_CMD,
options.log?.includes("error"),
);
}

// Check if Prisma schema exists
Expand Down Expand Up @@ -242,7 +247,11 @@ export default defineNuxtModule<PrismaExtendedModule>({
await writeClientInLib(LAYER_PATH);

if (options.generateClient) {
await installPrismaClient(PROJECT_PATH, options.installClient);
if (options.installClient) {
await ensureDependencyInstalled("@prisma/client", {
cwd: PROJECT_PATH
});
}
await generatePrismaClient(
PROJECT_PATH,
PRISMA_SCHEMA_CMD,
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 @@ -23,8 +23,8 @@ export const PREDEFINED_LOG_MESSAGES = {
},
installPrismaCLI: {
action: "Installing Prisma CLI...",
yes: `Successfully installed "Prisma CLI.`,
no: `Failed to install Prisma CLI.`,
success: `Successfully installed Prisma CLI.`,
error: `Failed to install Prisma CLI.`,
},
checkIfPrismaSchemaExists: {
yes: "Prisma schema file exists.",
Expand Down
49 changes: 0 additions & 49 deletions src/package-utils/setup-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from "./log-helpers";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { join } from "pathe";
import { addDependency, addDevDependency } from "nypm";

export type DatabaseProviderType =
| "sqlite"
Expand All @@ -24,33 +23,6 @@ export type PrismaInitOptions = {
rootDir: string;
};

export async function isPrismaCLIInstalled(
directory: string,
): Promise<boolean> {
try {
await execa("npx", ["prisma", "version"], { cwd: directory });
logSuccess(PREDEFINED_LOG_MESSAGES.isPrismaCLIinstalled.yes);
return true;
} catch (error) {
logError(PREDEFINED_LOG_MESSAGES.isPrismaCLIinstalled.no);
// log(error);
return false;
}
}

export async function installPrismaCLI(directory: string) {
try {
await addDevDependency("prisma", {
cwd: directory,
});

logSuccess(PREDEFINED_LOG_MESSAGES.installPrismaCLI.yes);
} catch (err) {
logError(PREDEFINED_LOG_MESSAGES.installPrismaCLI.no);
log(err);
}
}

export function checkIfPrismaSchemaExists(paths: string[]) {
const exists = paths.reduce((prev, current) => {
return existsSync(current) || prev;
Expand Down Expand Up @@ -215,27 +187,6 @@ export async function formatSchema(directory: string, schemaPath: string[]) {
}
}

export async function installPrismaClient(
directory: string,
installPrismaClient: boolean = true,
) {
log(PREDEFINED_LOG_MESSAGES.generatePrismaClient.action);

if (installPrismaClient) {
try {
await addDependency("@prisma/client", {
cwd: directory,
});
} catch (error) {
logError(
PREDEFINED_LOG_MESSAGES.generatePrismaClient
.prismaClientInstallationError,
);
// log(error);
}
}
}

export async function generatePrismaClient(
directory: string,
prismaSchemaPath: string[],
Expand Down

0 comments on commit 2179d37

Please sign in to comment.