Skip to content

Commit

Permalink
#3775 Only generate prisma client if it has not been generated (#3780)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhsuresh authored Aug 23, 2022
1 parent f6dac09 commit 161270e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-walls-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"blitz": patch
---

Only run the prisma generate command when the schema file changes.
37 changes: 28 additions & 9 deletions packages/blitz/src/cli/utils/codegen-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {join} from "path"
import fs from "fs-extra"
import {getPackageJson} from "./get-package-json"
import {runPrisma} from "../../utils/run-prisma"
import os from "os"

export const codegenTasks = async () => {
try {
Expand Down Expand Up @@ -38,15 +39,33 @@ export const codegenTasks = async () => {
)

if (hasPrisma) {
let prismaSpinner = log.spinner(`Generating Prisma client`).start()
const result = await runPrisma(["generate"], true)
if (result.success) {
prismaSpinner.succeed(log.greenText("Generated Prisma client"))
} else {
prismaSpinner.fail()
console.log("\n" + result.stderr)
process.exit(1)
}
const tempDir = os.tmpdir()
const creationDate = await fs.stat(process.cwd())
if (!fs.existsSync(join(tempDir, `blitz-${creationDate.birthtimeMs}-prisma-client.json`))) {
const lastModified = await fs.stat(join(process.cwd(), "db/schema.prisma"))
await fs.writeFile(
join(tempDir, `blitz-${creationDate.birthtimeMs}-prisma-client.json`),
JSON.stringify({ lastModified: lastModified.mtimeMs }),
)
}
const prismaStatus = await fs.readJson(join(tempDir, `blitz-${creationDate.birthtimeMs}-prisma-client.json`))
const lastModified = await fs.stat(join(process.cwd(), "db/schema.prisma"))
if (prismaStatus.lastModified !== lastModified.mtimeMs) {
let prismaSpinner = log.spinner(`Generating Prisma client`).start()
const result = await runPrisma(["generate"], true)
if (result.success) {
prismaSpinner.succeed(log.greenText("Generated Prisma client"))
} else {
prismaSpinner.fail()
console.log("\n" + result.stderr)
process.exit(1)
}
await fs.writeJson(join(process.cwd(), `blitz-${creationDate.birthtimeMs}-prisma-client.json`), {
lastModified: lastModified.mtimeMs,
})
} else {
log.success("Prisma client was up to date")
}
}
} catch (err) {
log.error(JSON.stringify(err, null, 2))
Expand Down

0 comments on commit 161270e

Please sign in to comment.