-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add gen commands #68
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { getAdapter } from "../adapters"; | ||
| import { generateAndWriteTypes } from "../lib/codegen"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { relativePathname } from "../lib/url"; | ||
| import { findProjectRoot } from "../project"; | ||
|
|
||
| const FILENAME = "prismicio-types.d.ts"; | ||
|
|
||
| const config = { | ||
| name: "prismic gen types", | ||
| description: ` | ||
| Generate TypeScript types from local custom type and slice models. | ||
|
|
||
| Reads models from the customtypes/ and slices directories, then writes | ||
| a prismicio-types.d.ts file at the project root. | ||
| `, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async () => { | ||
| const adapter = await getAdapter(); | ||
| const slices = await adapter.getSlices(); | ||
| const customTypes = await adapter.getCustomTypes(); | ||
| const projectRoot = await findProjectRoot(); | ||
|
|
||
| const output = new URL(FILENAME, projectRoot); | ||
| const relativeOutput = relativePathname(projectRoot, output); | ||
|
|
||
| await generateAndWriteTypes({ | ||
| customTypes: customTypes.map((customType) => customType.model), | ||
| slices: slices.map((slice) => slice.model), | ||
| output, | ||
| }); | ||
|
|
||
| console.info(`Generated types at ${relativeOutput}`); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { createCommandRouter } from "../lib/command"; | ||
| import genTypes from "./gen-types"; | ||
|
|
||
| export default createCommandRouter({ | ||
| name: "prismic gen", | ||
| description: "Generate files from local Prismic models.", | ||
| commands: { | ||
| types: { | ||
| handler: genTypes, | ||
| description: "Generate TypeScript types from local models", | ||
| }, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| import { relative } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| export function appendTrailingSlash(url: string | URL): URL { | ||
| const newURL = new URL(url); | ||
| if (!newURL.pathname.endsWith("/")) newURL.pathname += "/"; | ||
| return newURL; | ||
| } | ||
|
|
||
| export function relativePathname(a: URL, b: URL): string { | ||
| return relative(fileURLToPath(a), fileURLToPath(b)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes"; | ||
|
|
||
| import { mkdir, writeFile } from "node:fs/promises"; | ||
|
|
||
| import { it } from "./it"; | ||
|
|
||
| it("supports --help", async ({ expect, prismic }) => { | ||
| const { stdout, exitCode } = await prismic("gen", ["types", "--help"]); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("prismic gen types [options]"); | ||
| }); | ||
|
|
||
| it("generates types from local models", async ({ expect, project, prismic }) => { | ||
| const customType = buildCustomType(); | ||
| const customTypePath = new URL(`customtypes/${customType.id}/index.json`, project); | ||
| await mkdir(new URL(".", customTypePath), { recursive: true }); | ||
| await writeFile(customTypePath, JSON.stringify(customType)); | ||
|
|
||
| const slice = buildSlice(); | ||
| const slicePath = new URL(`slices/${slice.name}/model.json`, project); | ||
| await mkdir(new URL(".", slicePath), { recursive: true }); | ||
| await writeFile(slicePath, JSON.stringify(slice)); | ||
|
|
||
| const { exitCode, stdout } = await prismic("gen", ["types"]); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("Generated types"); | ||
|
|
||
| await expect(project).toHaveFile("prismicio-types.d.ts", { | ||
| contains: customType.id, | ||
| }); | ||
| }); | ||
|
|
||
| it("generates types with no models", async ({ expect, project, prismic }) => { | ||
| const { exitCode, stdout } = await prismic("gen", ["types"]); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("Generated types"); | ||
|
|
||
| await expect(project).toHaveFile("prismicio-types.d.ts"); | ||
| }); | ||
|
|
||
| function buildCustomType(): CustomType { | ||
| const id = crypto.randomUUID().split("-")[0]; | ||
| return { | ||
| id: `type-T${id}`, | ||
| label: `TypeT${id}`, | ||
| repeatable: true, | ||
| status: true, | ||
| json: {}, | ||
| }; | ||
| } | ||
|
|
||
| function buildSlice(): SharedSlice { | ||
| const id = crypto.randomUUID().split("-")[0]; | ||
| return { | ||
| id: `slice-S${id}`, | ||
| type: "SharedSlice", | ||
| name: `SliceS${id}`, | ||
| variations: [ | ||
| { | ||
| id: "default", | ||
| name: "Default", | ||
| docURL: "", | ||
| version: "initial", | ||
| description: "Default", | ||
| imageUrl: "", | ||
| }, | ||
| ], | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { it } from "./it"; | ||
|
|
||
| it("supports --help", async ({ expect, prismic }) => { | ||
| const { stdout, exitCode } = await prismic("gen", ["--help"]); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("prismic gen <command> [options]"); | ||
| }); | ||
|
|
||
| it("prints help by default", async ({ expect, prismic }) => { | ||
| const { stdout, exitCode } = await prismic("gen"); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("prismic gen <command> [options]"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.