diff --git a/CHANGELOG.md b/CHANGELOG.md index e2cc9e5..6f5d087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Redwood uses prettier 3, and prettier 3 removes the sync API. This means we now have to operate entirely async. This is a breaking change from the sdl-codegen API, as you need to await the exposed public fns. - There is a verbose option which provides info on the timings of the codegen. +- Big watch mode improvements ### 1.1.2 diff --git a/package.json b/package.json index 0a62a84..7570127 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ ], "scripts": { "build": "tsc", + "build:watch": "tsc --watch", "format": "prettier \"**/*\" --ignore-unknown", "format:write": "pnpm format --write", "jest": "vitest", diff --git a/src/index.ts b/src/index.ts index 028377b..4d618f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,9 +14,9 @@ export * from "./types.js" import { basename, join } from "node:path" -interface SDLCodeGenReturn { +export interface SDLCodeGenReturn { // Optional way to start up a watcher mode for the codegen - createWatcher: () => { fileChanged: (path: string) => void } + createWatcher: () => { fileChanged: (path: string) => Promise } // Paths which were added/changed during the run paths: string[] } @@ -107,24 +107,27 @@ export async function runFullCodegen(preset: string, config: unknown): Promise { + const oldSDL = "" + return { fileChanged: async (path: string) => { + if (isTypesFile(path)) return if (path === appContext.pathSettings.graphQLSchemaPath) { + const newSDL = appContext.sys.readFile(path) + if (newSDL === oldSDL) return + if (verbose) console.log("[sdl-codegen] SDL Schema changed") - getGraphQLSDLFromFile(appContext.pathSettings) - await createSharedSchemaFiles(appContext) - await createDTSFilesForAllServices() + await step("GraphQL schema changed", () => getGraphQLSDLFromFile(appContext.pathSettings)) + await step("Create all shared schema files", () => createSharedSchemaFiles(appContext)) + await step("Create all service files", createDTSFilesForAllServices) } else if (path === appContext.pathSettings.prismaDSLPath) { - if (verbose) console.log("[sdl-codegen] Prisma schema changed") - getPrismaSchemaFromFile(appContext.pathSettings) - await createDTSFilesForAllServices() + await step("Prisma schema changed", () => getPrismaSchemaFromFile(appContext.pathSettings)) + await step("Create all shared schema files", createDTSFilesForAllServices) } else if (isRedwoodServiceFile(path)) { if (!knownServiceFiles.includes(path)) { - if (verbose) console.log("[sdl-codegen] New service file") - await createDTSFilesForAllServices() + await step("Create all shared schema files", createDTSFilesForAllServices) } else { - if (verbose) console.log("[sdl-codegen] Service file changed") - await lookAtServiceFile(path, appContext) + await step("Create known service files", () => lookAtServiceFile(path, appContext)) } } }, @@ -137,15 +140,20 @@ export async function runFullCodegen(preset: string, config: unknown): Promise file.endsWith(".d.ts") + const isRedwoodServiceFile = (file: string) => { + if (!file.includes("services")) return false + if (file.endsWith(".d.ts")) return false if (file.endsWith(".test.ts") || file.endsWith(".test.js")) return false if (file.endsWith("scenarios.ts") || file.endsWith("scenarios.js")) return false return file.endsWith(".ts") || file.endsWith(".tsx") || file.endsWith(".js") } -const makeStep = (verbose: boolean) => async (msg: string, fn: () => Promise | void) => { +const makeStep = (verbose: boolean) => async (msg: string, fn: () => Promise | Promise | void) => { if (!verbose) return fn() - console.time(msg) + console.log("[sdl-codegen] " + msg) + console.time("[sdl-codegen] " + msg) await fn() - console.timeEnd(msg) + console.timeEnd("[sdl-codegen] " + msg) }