fix(cli): only show the overwrite message when the schema file exists and the code has changed (#3826)

This commit is contained in:
Fraol Lemecha
2025-08-06 21:01:19 +03:00
committed by Bereket Engida
parent 772e6c78cc
commit 7c43ff0bf2
2 changed files with 8 additions and 5 deletions

View File

@@ -9,7 +9,7 @@ import prompts from "prompts";
import fs from "fs/promises";
import chalk from "chalk";
import { getAdapter } from "better-auth/db";
import { getGenerator } from "../generators";
import { generateSchema } from "../generators";
export async function generateAction(opts: any) {
const options = z
@@ -45,7 +45,7 @@ export async function generateAction(opts: any) {
const spinner = yoctoSpinner({ text: "preparing schema..." }).start();
const schema = await getGenerator({
const schema = await generateSchema({
adapter,
file: options.output,
options: config,
@@ -56,7 +56,7 @@ export async function generateAction(opts: any) {
logger.info("Your schema is already up to date.");
process.exit(0);
}
if (schema.append || schema.overwrite) {
if (schema.overwrite) {
let confirm = options.y || options.yes;
if (!confirm) {
const response = await prompts({

View File

@@ -15,6 +15,7 @@ export const generatePrismaSchema: SchemaGenerator = async ({
const tables = getAuthTables(options);
const filePath = file || "./prisma/schema.prisma";
const schemaPrismaExist = existsSync(path.join(process.cwd(), filePath));
let schemaPrisma = "";
if (schemaPrismaExist) {
schemaPrisma = await fs.readFile(
@@ -222,10 +223,12 @@ export const generatePrismaSchema: SchemaGenerator = async ({
}
});
const schemaChanged = schema.trim() !== schemaPrisma.trim();
return {
code: schema.trim() === schemaPrisma.trim() ? "" : schema,
code: schemaChanged ? schema : "",
fileName: filePath,
overwrite: true,
overwrite: schemaPrismaExist && schemaChanged,
};
};