Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace own installation functions with nypm ensureDependencyInstalled #78

Merged
merged 4 commits into from
Jan 4, 2025
Merged
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
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
Loading