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

Package manager and setup improvements #79

Merged
merged 17 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
49 changes: 20 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@
"@nuxt/devtools-kit": "^1.3.3",
"@nuxt/kit": "^3.11.2",
"@prisma/client": "^6.1.0",
"chalk": "^5.3.0",
"consola": "^3.3.3",
"defu": "^6.1.4",
"execa": "^8.0.1",
"nypm": "^0.4.1",
"pathe": "^1.1.2",
"prompts": "^2.4.2"
"tinyexec": "^0.3.2",
"tinyrainbow": "^1.2.0"
},
"devDependencies": {
"@commitlint/cli": "^19.5.0",
Expand Down
75 changes: 20 additions & 55 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
addImportsDir,
addServerImportsDir,
} from "@nuxt/kit";
import { addCustomTab } from "@nuxt/devtools-kit";
import { fileURLToPath } from "url";
import defu from "defu";

Expand All @@ -14,24 +15,22 @@ import {
checkIfPrismaSchemaExists,
formatSchema,
initPrisma,
installStudio,
startPrismaStudio,
runMigration,
writeClientInLib,
writeToSchema,
generatePrismaClient,
} from "./package-utils/setup-helpers";
import { log, PREDEFINED_LOG_MESSAGES } from "./package-utils/log-helpers";
import { PREDEFINED_LOG_MESSAGES } from "./package-utils/log-helpers";
import type { Prisma } from "@prisma/client";
import { executeRequiredPrompts } from "./package-utils/prompts";
import { ensureDependencyInstalled } from "nypm";
import consola from "consola";

// Module configuration interface
interface ModuleOptions extends Prisma.PrismaClientOptions {
writeToSchema: boolean;
formatSchema: boolean;
runMigration: boolean;
installClient: boolean;
installCLI: boolean;
generateClient: boolean;
installStudio: boolean;
autoSetupPrisma: boolean;
Expand Down Expand Up @@ -60,8 +59,6 @@ export default defineNuxtModule<PrismaExtendedModule>({
writeToSchema: true,
formatSchema: true,
runMigration: true,
installClient: true,
installCLI: true,
generateClient: true,
installStudio: true,
autoSetupPrisma: false,
Expand Down Expand Up @@ -123,7 +120,7 @@ export default defineNuxtModule<PrismaExtendedModule>({

if (forceSkipPrismaSetup || npmLifecycleEvent === "postinstall") {
if (npmLifecycleEvent !== "postinstall") {
log(PREDEFINED_LOG_MESSAGES.PRISMA_SETUP_SKIPPED_WARNING);
consola.warn(PREDEFINED_LOG_MESSAGES.PRISMA_SETUP_SKIPPED_WARNING);
}
prepareModule();
return;
Expand All @@ -136,26 +133,6 @@ export default defineNuxtModule<PrismaExtendedModule>({
? resolveProject(options.prismaRoot) // Combines paths safely
: PROJECT_PATH;

// 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
});
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
const prismaSchemaExists = checkIfPrismaSchemaExists([
resolveProject(LAYER_PATH, "prisma", "schema.prisma"),
Expand All @@ -171,7 +148,7 @@ export default defineNuxtModule<PrismaExtendedModule>({
);

if (migrationFolderExists || !options.runMigration) {
log(PREDEFINED_LOG_MESSAGES.skipMigrations);
consola.info(PREDEFINED_LOG_MESSAGES.skipMigrations);
return;
}

Expand All @@ -189,7 +166,6 @@ export default defineNuxtModule<PrismaExtendedModule>({

const promptResult = await executeRequiredPrompts({
promptForMigrate: true && !skipAllPrompts,
promptForPrismaStudio: false && !skipAllPrompts,
});

if (promptResult?.promptForPrismaMigrate && options.runMigration) {
Expand All @@ -214,29 +190,23 @@ export default defineNuxtModule<PrismaExtendedModule>({
*/
const prismaStudioWorkflow = async () => {
if (!options.installStudio || npmLifecycleEvent !== "dev") {
log(PREDEFINED_LOG_MESSAGES.skipInstallingPrismaStudio);
consola.info(PREDEFINED_LOG_MESSAGES.skipInstallingPrismaStudio);
return;
}

const installAndStartPrismaStudio = async () => {
await installStudio(PROJECT_PATH, PRISMA_SCHEMA_CMD);

nuxt.hooks.hook("devtools:customTabs", (tab) => {
tab.push({
name: "nuxt-prisma",
title: "Prisma Studio",
icon: "simple-icons:prisma",
category: "server",
view: {
type: "iframe",
src: "http://localhost:5555/",
persistent: true,
},
});
});
};

await installAndStartPrismaStudio();
await startPrismaStudio(PROJECT_PATH, PRISMA_SCHEMA_CMD);

addCustomTab({
name: "nuxt-prisma",
title: "Prisma Studio",
icon: "simple-icons:prisma",
category: "server",
view: {
type: "iframe",
src: "http://localhost:5555/",
persistent: true,
},
});
};

// Execute workflows sequentially
Expand All @@ -247,11 +217,6 @@ export default defineNuxtModule<PrismaExtendedModule>({
await writeClientInLib(LAYER_PATH);

if (options.generateClient) {
if (options.installClient) {
await ensureDependencyInstalled("@prisma/client", {
cwd: PROJECT_PATH
});
}
await generatePrismaClient(
PROJECT_PATH,
PRISMA_SCHEMA_CMD,
Expand Down
65 changes: 18 additions & 47 deletions src/package-utils/log-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
import chalk from "chalk";

export function logSuccess(message: string) {
console.log(chalk.green(`✔ ${message}`));
}

export function logWarning(message: string) {
console.warn(chalk.yellow(`⚠️ ${message}`));
}

export function logError(message: string) {
console.error(chalk.red(`✘ ${message}`));
}

export function log(message: any) {
console.log(message);
}
import c from "tinyrainbow";

export const PREDEFINED_LOG_MESSAGES = {
isPrismaCLIinstalled: {
yes: `Prisma CLI is already installed.`,
no: `Prisma CLI is not installed.`,
},
installPrismaCLI: {
action: "Installing Prisma CLI...",
success: `Successfully installed Prisma CLI.`,
error: `Failed to install Prisma CLI.`,
},
checkIfPrismaSchemaExists: {
yes: "Prisma schema file exists.",
no: "Prisma schema file does not exist.",
},
initPrisma: {
action: "Initializing Prisma project...\n",
action: "Initializing Prisma project...",
success: "Initialized the Prisma project.",
error: "Failed to initialize Prisma project.",
},
checkIfMigrationsFolderExists: {
Expand All @@ -43,44 +19,39 @@ export const PREDEFINED_LOG_MESSAGES = {
failedToWrite: "Failed to write models to Prisma schema.",
},
runMigration: {
action: "Migrating database schema...\n",
action: "Migrating database schema...",
success: "Created User and Post tables in the database.",
error: "Failed to run Prisma migration.",
},
formatSchema: {
action: "Formatting Prisma schema...\n",
action: "Formatting Prisma schema...",
success: "Successfully formatted Prisma schema.",
error: "Failed to format Prisma schema file.",
},
generatePrismaClient: {
action: "Generating Prisma client...\n",
prismaClientInstallationError: "Failed to install Prisma Client.\n",
success: "Prisma Client successfully generated!",
error: "Failed to generate Prisma Client.\n",
action: "Generating Prisma client...",
success: "Successfully generated Prisma client.",
error: "Failed to generate Prisma Client.",
},
installStudio: {
action: "Starting Prisma Studio...\n",
success:
`Prisma Studio installed.` +
chalk.white(
`\nAfter clicking ${chalk.bold("Get Started")} in Nuxt DevTools, click on the ${chalk.bold("three dots (︙)")} in the lower left-hand side to reveal additional tabs.\nLocate the Prisma logo to open Prisma Studio.`,
),
error: "Failed to install Prisma Studio.",
startPrismaStudio: {
action: "Starting Prisma Studio...",
success: "Prisma Studio started.",
info: `After clicking ${c.bold("Get Started")} in Nuxt DevTools, click on the ${c.bold("three dots (︙)")} in the lower left-hand side to reveal additional tabs. Locate the Prisma logo to open Prisma Studio.\n`,
error: "Failed to start Prisma Studio.",
},
writeClientInLib: {
found:
"Skipping the creation of a lib/prisma.ts file that would hold a global instance of the Prisma Client because the prisma.ts file already exists in the lib folder.",
success: "Global instance of Prisma Client created in lib/prisma.ts.",
},
PRISMA_SETUP_SKIPPED_WARNING: chalk.yellow(
`${chalk.bold("Warning")}: Nuxt Prisma Module setup skipped.\nThis may cause unexpected behavior.`,
),
PRISMA_SETUP_SKIPPED_WARNING:
"Nuxt Prisma Module setup skipped.\nThis may cause unexpected behavior.",
skipMigrations: `\nNot migrating the database.`,
skipInstallingPrismaStudio: "Skipped installing Prisma Studio.",
suggestions: {
migrate:
chalk.yellow(chalk.bold("\nHint: ")) +
`You can manually run migrations by executing ${chalk.cyan.bold("npx prisma migrate dev")} or visit the ${chalk.blue.bold("Prisma Migrate")} docs for more info:\n${chalk.underline.blue("https://pris.ly/nuxt/migrate")}. ` +
`Or if you have pre-existing data on your database, you have to introspect it. Learn more in our docs:\n${chalk.underline.blue("https://pris.ly/nuxt/dbpull")}.\n`,
c.yellow(c.bold("\nHint: ")) +
`You can manually run migrations by executing ${c.cyan(c.bold("npx prisma migrate dev"))} or visit the ${c.blue(c.bold("Prisma Migrate"))} docs for more info:\n${c.underline(c.blue("https://pris.ly/nuxt/migrate"))}. ` +
`Or if you have pre-existing data on your database, you have to introspect it. Learn more in our docs:\n${c.underline(c.blue("https://pris.ly/nuxt/dbpull"))}.\n`,
},
};
Loading
Loading