-
Notifications
You must be signed in to change notification settings - Fork 0
feat: generate TypeScript types after sync and init #48
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { getHost, getToken } from "../auth"; | |
| import { getCustomTypes, getSlices } from "../clients/custom-types"; | ||
| import { safeGetRepositoryFromConfig } from "../config"; | ||
| import { type FrameworkAdapter, NoSupportedFrameworkError, requireFramework } from "../frameworks"; | ||
| import { generateAndWriteTypes } from "../lib/codegen"; | ||
| import { segmentSetRepository, segmentTrackEnd, segmentTrackStart } from "../lib/segment"; | ||
| import { sentrySetContext, sentrySetTag } from "../lib/sentry"; | ||
| import { dedent } from "../lib/string"; | ||
|
|
@@ -80,6 +81,7 @@ export async function sync(): Promise<void> { | |
| } else { | ||
| await syncSlices(repo, framework); | ||
| await syncCustomTypes(repo, framework); | ||
| await regenerateTypes(framework); | ||
| segmentTrackEnd("sync", true, undefined, { watch: false }); | ||
|
|
||
| console.info("Sync complete"); | ||
|
|
@@ -95,6 +97,7 @@ async function watchForChanges(repo: string, framework: FrameworkAdapter) { | |
|
|
||
| await syncSlices(repo, framework); | ||
| await syncCustomTypes(repo, framework); | ||
| await regenerateTypes(framework); | ||
|
|
||
| console.info(dedent` | ||
| Initial sync completed! | ||
|
|
@@ -143,6 +146,8 @@ async function watchForChanges(repo: string, framework: FrameworkAdapter) { | |
| changed.push("custom types"); | ||
| } | ||
|
|
||
| await regenerateTypes(framework); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hash updates before codegen prevents retry on failureMedium Severity In the watch loop, |
||
|
|
||
| const timestamp = new Date().toLocaleTimeString(); | ||
| console.info(`[${timestamp}] Changes detected in ${changed.join(" and ")}`); | ||
| } | ||
|
|
@@ -254,3 +259,14 @@ function exponentialMs(base: number): number { | |
| function hash(data: unknown): string { | ||
| return createHash("sha256").update(JSON.stringify(data)).digest("hex"); | ||
| } | ||
|
|
||
| async function regenerateTypes(framework: FrameworkAdapter): Promise<void> { | ||
| const slices = await framework.getSlices(); | ||
| const customTypes = await framework.getCustomTypes(); | ||
| const projectRoot = await framework.getProjectRoot(); | ||
| await generateAndWriteTypes({ | ||
| customTypes: customTypes.map((customType) => customType.model), | ||
| slices: slices.map((slice) => slice.model), | ||
| projectRoot, | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes"; | ||
|
|
||
| import { writeFile } from "node:fs/promises"; | ||
| import { generateTypes } from "prismic-ts-codegen"; | ||
|
|
||
| export async function generateAndWriteTypes(args: { | ||
| customTypes: CustomType[]; | ||
| slices: SharedSlice[]; | ||
| projectRoot: URL; | ||
| }): Promise<void> { | ||
| const types = generateTypes({ | ||
| customTypeModels: args.customTypes, | ||
| sharedSliceModels: args.slices, | ||
| clientIntegration: { | ||
| includeContentNamespace: true, | ||
| includeCreateClientInterface: true, | ||
| }, | ||
| cache: true, | ||
| typesProvider: "@prismicio/client", | ||
| }); | ||
| const outputPath = new URL("prismicio-types.d.ts", args.projectRoot); | ||
| await writeFile(outputPath, types); | ||
| } |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated type regeneration logic across two files
Low Severity
The inline code in
init.tsthat callsframework.getSlices(),framework.getCustomTypes(),framework.getProjectRoot(), and thengenerateAndWriteTypes()is an exact duplicate of theregenerateTypesfunction insync.ts. If the codegen behavior ever needs to change (e.g., different options, added error handling), both locations need updating, and forgetting one would introduce a bug. TheregenerateTypeshelper could live incodegen.tsand be shared.Additional Locations (1)
src/commands/sync.ts#L262-L272