Skip to content

Commit

Permalink
Also throw in the watcher improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Sep 5, 2024
1 parent dc8f925 commit 56c6a3c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"format": "prettier \"**/*\" --ignore-unknown",
"format:write": "pnpm format --write",
"jest": "vitest",
Expand Down
38 changes: 23 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> }
// Paths which were added/changed during the run
paths: string[]
}
Expand Down Expand Up @@ -107,24 +107,27 @@ export async function runFullCodegen(preset: string, config: unknown): Promise<S
if (verbose) console.log(`[sdl-codegen]: Full run took ${timeTaken}ms`)

const createWatcher = () => {
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))
}
}
},
Expand All @@ -137,15 +140,20 @@ export async function runFullCodegen(preset: string, config: unknown): Promise<S
}
}

const isTypesFile = (file: string) => 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> | void) => {
const makeStep = (verbose: boolean) => async (msg: string, fn: () => Promise<unknown> | Promise<void> | void) => {

Check warning on line 153 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

void is not valid as a constituent in a union type
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)
}

0 comments on commit 56c6a3c

Please sign in to comment.