@angeloashmore/prismic-cli-poc 0.0.0-canary.1d36cd8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +98 -0
- package/dist/index.mjs +2548 -0
- package/package.json +53 -0
- package/src/codegen-types.ts +82 -0
- package/src/codegen.ts +45 -0
- package/src/custom-type-add-field-boolean.ts +192 -0
- package/src/custom-type-add-field-color.ts +177 -0
- package/src/custom-type-add-field-date.ts +180 -0
- package/src/custom-type-add-field-embed.ts +177 -0
- package/src/custom-type-add-field-geo-point.ts +174 -0
- package/src/custom-type-add-field-image.ts +177 -0
- package/src/custom-type-add-field-key-text.ts +177 -0
- package/src/custom-type-add-field-link.ts +201 -0
- package/src/custom-type-add-field-number.ts +209 -0
- package/src/custom-type-add-field-rich-text.ts +202 -0
- package/src/custom-type-add-field-select.ts +192 -0
- package/src/custom-type-add-field-timestamp.ts +180 -0
- package/src/custom-type-add-field-uid.ts +177 -0
- package/src/custom-type-add-field.ts +111 -0
- package/src/custom-type-connect-slice.ts +220 -0
- package/src/custom-type-create.ts +118 -0
- package/src/custom-type-disconnect-slice.ts +177 -0
- package/src/custom-type-list.ts +110 -0
- package/src/custom-type-remove-field.ts +177 -0
- package/src/custom-type-remove.ts +144 -0
- package/src/custom-type-set-name.ts +144 -0
- package/src/custom-type-view.ts +118 -0
- package/src/custom-type.ts +85 -0
- package/src/index.ts +127 -0
- package/src/init.ts +64 -0
- package/src/lib/auth.ts +83 -0
- package/src/lib/config.ts +111 -0
- package/src/lib/custom-types-api.ts +438 -0
- package/src/lib/file.ts +49 -0
- package/src/lib/framework.ts +143 -0
- package/src/lib/json.ts +3 -0
- package/src/lib/request.ts +116 -0
- package/src/lib/slice.ts +115 -0
- package/src/lib/string.ts +6 -0
- package/src/lib/url.ts +25 -0
- package/src/locale-add.ts +116 -0
- package/src/locale-list.ts +107 -0
- package/src/locale-remove.ts +88 -0
- package/src/locale-set-default.ts +131 -0
- package/src/locale.ts +60 -0
- package/src/login.ts +152 -0
- package/src/logout.ts +36 -0
- package/src/page-type-add-field-boolean.ts +192 -0
- package/src/page-type-add-field-color.ts +177 -0
- package/src/page-type-add-field-date.ts +180 -0
- package/src/page-type-add-field-embed.ts +177 -0
- package/src/page-type-add-field-geo-point.ts +174 -0
- package/src/page-type-add-field-image.ts +177 -0
- package/src/page-type-add-field-key-text.ts +177 -0
- package/src/page-type-add-field-link.ts +201 -0
- package/src/page-type-add-field-number.ts +209 -0
- package/src/page-type-add-field-rich-text.ts +202 -0
- package/src/page-type-add-field-select.ts +192 -0
- package/src/page-type-add-field-timestamp.ts +180 -0
- package/src/page-type-add-field-uid.ts +177 -0
- package/src/page-type-add-field.ts +111 -0
- package/src/page-type-connect-slice.ts +220 -0
- package/src/page-type-create.ts +142 -0
- package/src/page-type-disconnect-slice.ts +177 -0
- package/src/page-type-list.ts +109 -0
- package/src/page-type-remove-field.ts +177 -0
- package/src/page-type-remove.ts +144 -0
- package/src/page-type-set-name.ts +144 -0
- package/src/page-type-set-repeatable.ts +153 -0
- package/src/page-type-view.ts +118 -0
- package/src/page-type.ts +90 -0
- package/src/preview-add.ts +126 -0
- package/src/preview-get-simulator.ts +104 -0
- package/src/preview-list.ts +106 -0
- package/src/preview-remove-simulator.ts +80 -0
- package/src/preview-remove.ts +109 -0
- package/src/preview-set-name.ts +137 -0
- package/src/preview-set-simulator.ts +116 -0
- package/src/preview.ts +75 -0
- package/src/pull.ts +247 -0
- package/src/push.ts +405 -0
- package/src/repo-create.ts +136 -0
- package/src/repo-get-access.ts +86 -0
- package/src/repo-list.ts +100 -0
- package/src/repo-set-access.ts +100 -0
- package/src/repo-set-name.ts +102 -0
- package/src/repo-view.ts +113 -0
- package/src/repo.ts +70 -0
- package/src/slice-add-field-boolean.ts +173 -0
- package/src/slice-add-field-color.ts +158 -0
- package/src/slice-add-field-date.ts +158 -0
- package/src/slice-add-field-embed.ts +158 -0
- package/src/slice-add-field-geo-point.ts +155 -0
- package/src/slice-add-field-image.ts +155 -0
- package/src/slice-add-field-key-text.ts +158 -0
- package/src/slice-add-field-link.ts +178 -0
- package/src/slice-add-field-number.ts +158 -0
- package/src/slice-add-field-rich-text.ts +183 -0
- package/src/slice-add-field-select.ts +173 -0
- package/src/slice-add-field-timestamp.ts +158 -0
- package/src/slice-add-field.ts +106 -0
- package/src/slice-add-variation.ts +145 -0
- package/src/slice-create.ts +148 -0
- package/src/slice-list-variations.ts +67 -0
- package/src/slice-list.ts +88 -0
- package/src/slice-remove-field.ts +128 -0
- package/src/slice-remove-variation.ts +118 -0
- package/src/slice-remove.ts +97 -0
- package/src/slice-rename.ts +128 -0
- package/src/slice-view.ts +77 -0
- package/src/slice.ts +90 -0
- package/src/status.ts +733 -0
- package/src/token-create.ts +203 -0
- package/src/token-delete.ts +182 -0
- package/src/token-list.ts +223 -0
- package/src/token-set-name.ts +193 -0
- package/src/token.ts +60 -0
- package/src/webhook-add-header.ts +118 -0
- package/src/webhook-create.ts +152 -0
- package/src/webhook-disable.ts +109 -0
- package/src/webhook-enable.ts +132 -0
- package/src/webhook-list.ts +93 -0
- package/src/webhook-remove-header.ts +117 -0
- package/src/webhook-remove.ts +106 -0
- package/src/webhook-set-triggers.ts +148 -0
- package/src/webhook-status.ts +90 -0
- package/src/webhook-test.ts +106 -0
- package/src/webhook-view.ts +147 -0
- package/src/webhook.ts +95 -0
- package/src/whoami.ts +62 -0
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@angeloashmore/prismic-cli-poc",
|
|
3
|
+
"version": "0.0.0-canary.1d36cd8",
|
|
4
|
+
"description": "A proof-of-concept developer CLI for Prismic.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"prismic",
|
|
7
|
+
"typescript"
|
|
8
|
+
],
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"author": "Prismic <contact@prismic.io> (https://prismic.io)",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "ssh://git@github.com/prismicio-community/prismic-cli-poc.git"
|
|
14
|
+
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"prismic": "./dist/index.mjs"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"./dist",
|
|
20
|
+
"./src"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsdown",
|
|
28
|
+
"dev": "tsdown --watch",
|
|
29
|
+
"format": "oxfmt",
|
|
30
|
+
"prepare": "npm run build",
|
|
31
|
+
"lint": "oxlint --deny-warnings",
|
|
32
|
+
"types": "tsc --noEmit",
|
|
33
|
+
"unit": "vitest run --coverage",
|
|
34
|
+
"unit:watch": "vitest watch",
|
|
35
|
+
"test": "npm run lint && npm run types && npm run unit && npm run build"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@prismicio/e2e-tests-utils": "1.15.0",
|
|
39
|
+
"@prismicio/types-internal": "3.16.1",
|
|
40
|
+
"@types/node": "25.0.9",
|
|
41
|
+
"@vitest/coverage-v8": "4.0.17",
|
|
42
|
+
"oxfmt": "^0.24.0",
|
|
43
|
+
"oxlint": "1.39.0",
|
|
44
|
+
"prismic-ts-codegen": "^0.1.28",
|
|
45
|
+
"tsdown": "0.19.0",
|
|
46
|
+
"typescript": "5.9.3",
|
|
47
|
+
"valibot": "^1.2.0",
|
|
48
|
+
"vitest": "4.0.17"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=24"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
|
|
2
|
+
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { generateTypes, NON_EDITABLE_FILE_HEADER } from "prismic-ts-codegen";
|
|
6
|
+
|
|
7
|
+
import { readLocalCustomTypes, readLocalSlices } from "./lib/custom-types-api";
|
|
8
|
+
|
|
9
|
+
const HELP = `
|
|
10
|
+
Generate TypeScript types from local custom type and slice models.
|
|
11
|
+
|
|
12
|
+
USAGE
|
|
13
|
+
prismic codegen types [flags]
|
|
14
|
+
|
|
15
|
+
FLAGS
|
|
16
|
+
-o, --output string Output file path (default: "prismicio-types.d.ts")
|
|
17
|
+
-h, --help Show help for command
|
|
18
|
+
|
|
19
|
+
EXAMPLES
|
|
20
|
+
prismic codegen types
|
|
21
|
+
prismic codegen types --output custom.d.ts
|
|
22
|
+
`.trim();
|
|
23
|
+
|
|
24
|
+
export async function codegenTypes(): Promise<void> {
|
|
25
|
+
const {
|
|
26
|
+
values: { help, output },
|
|
27
|
+
} = parseArgs({
|
|
28
|
+
args: process.argv.slice(4), // skip: node, script, "codegen", "types"
|
|
29
|
+
options: {
|
|
30
|
+
output: { type: "string", short: "o" },
|
|
31
|
+
help: { type: "boolean", short: "h" },
|
|
32
|
+
},
|
|
33
|
+
allowPositionals: false,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (help) {
|
|
37
|
+
console.info(HELP);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
await buildTypes({ output });
|
|
43
|
+
console.info(`Generated types written to ${output ?? "prismicio-types.d.ts"}`);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
46
|
+
process.exitCode = 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function buildTypes(args?: { output?: string }): Promise<void> {
|
|
51
|
+
const output = args?.output ?? "prismicio-types.d.ts";
|
|
52
|
+
|
|
53
|
+
const [customTypesResult, slicesResult] = await Promise.all([
|
|
54
|
+
readLocalCustomTypes(),
|
|
55
|
+
readLocalSlices(),
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
if (!customTypesResult.ok) {
|
|
59
|
+
throw new Error(`failed to read local custom types: ${customTypesResult.error}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!slicesResult.ok) {
|
|
63
|
+
throw new Error(`failed to read local slices: ${slicesResult.error}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const customTypes = customTypesResult.value as unknown as CustomTypeModel[];
|
|
67
|
+
const slices = slicesResult.value as unknown as SharedSliceModel[];
|
|
68
|
+
|
|
69
|
+
const types = generateTypes({
|
|
70
|
+
customTypeModels: customTypes,
|
|
71
|
+
sharedSliceModels: slices,
|
|
72
|
+
typesProvider: "@prismicio/client",
|
|
73
|
+
clientIntegration: {
|
|
74
|
+
includeCreateClientInterface: customTypes.length > 0 || slices.length > 0,
|
|
75
|
+
includeContentNamespace: true,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const content = NON_EDITABLE_FILE_HEADER + "\n\n" + types;
|
|
80
|
+
|
|
81
|
+
await writeFile(output, content);
|
|
82
|
+
}
|
package/src/codegen.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
|
|
3
|
+
import { codegenTypes } from "./codegen-types";
|
|
4
|
+
|
|
5
|
+
const HELP = `
|
|
6
|
+
Generate code from Prismic models.
|
|
7
|
+
|
|
8
|
+
USAGE
|
|
9
|
+
prismic codegen <command> [flags]
|
|
10
|
+
|
|
11
|
+
COMMANDS
|
|
12
|
+
types Generate TypeScript types from models pushed to Prismic
|
|
13
|
+
|
|
14
|
+
FLAGS
|
|
15
|
+
-h, --help Show help for command
|
|
16
|
+
|
|
17
|
+
LEARN MORE
|
|
18
|
+
Use \`prismic codegen <command> --help\` for more information about a command.
|
|
19
|
+
`.trim();
|
|
20
|
+
|
|
21
|
+
export async function codegen(): Promise<void> {
|
|
22
|
+
const {
|
|
23
|
+
positionals: [subcommand],
|
|
24
|
+
} = parseArgs({
|
|
25
|
+
args: process.argv.slice(3), // skip: node, script, "codegen"
|
|
26
|
+
options: {
|
|
27
|
+
help: { type: "boolean", short: "h" },
|
|
28
|
+
},
|
|
29
|
+
allowPositionals: true,
|
|
30
|
+
strict: false,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
switch (subcommand) {
|
|
34
|
+
case "types":
|
|
35
|
+
await codegenTypes();
|
|
36
|
+
break;
|
|
37
|
+
default: {
|
|
38
|
+
if (subcommand) {
|
|
39
|
+
console.error(`Unknown codegen subcommand: ${subcommand}\n`);
|
|
40
|
+
process.exitCode = 1;
|
|
41
|
+
}
|
|
42
|
+
console.info(HELP);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { BooleanField, CustomType } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import * as v from "valibot";
|
|
6
|
+
|
|
7
|
+
import { buildTypes } from "./codegen-types";
|
|
8
|
+
import { findUpward } from "./lib/file";
|
|
9
|
+
import { stringify } from "./lib/json";
|
|
10
|
+
import { humanReadable } from "./lib/string";
|
|
11
|
+
|
|
12
|
+
const HELP = `
|
|
13
|
+
Add a boolean (toggle) field to an existing custom type.
|
|
14
|
+
|
|
15
|
+
USAGE
|
|
16
|
+
prismic custom-type add-field boolean <type-id> <field-id> [flags]
|
|
17
|
+
|
|
18
|
+
ARGUMENTS
|
|
19
|
+
type-id Custom type identifier (required)
|
|
20
|
+
field-id Field identifier (required)
|
|
21
|
+
|
|
22
|
+
Types are generated by default after changes. Use --no-types to skip.
|
|
23
|
+
|
|
24
|
+
FLAGS
|
|
25
|
+
-t, --tab string Target tab (default: first existing tab, or "Main")
|
|
26
|
+
-l, --label string Display label for the field (inferred from field-id if omitted)
|
|
27
|
+
--default Set default value to true
|
|
28
|
+
--true-label string Label shown when toggle is on
|
|
29
|
+
--false-label string Label shown when toggle is off
|
|
30
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
31
|
+
--no-types Skip type generation
|
|
32
|
+
-h, --help Show help for command
|
|
33
|
+
|
|
34
|
+
EXAMPLES
|
|
35
|
+
prismic custom-type add-field boolean homepage featured
|
|
36
|
+
prismic custom-type add-field boolean article published --default
|
|
37
|
+
prismic custom-type add-field boolean product available --true-label "In Stock" --false-label "Out of Stock"
|
|
38
|
+
`.trim();
|
|
39
|
+
|
|
40
|
+
const CustomTypeSchema = v.object({
|
|
41
|
+
id: v.string(),
|
|
42
|
+
label: v.string(),
|
|
43
|
+
repeatable: v.boolean(),
|
|
44
|
+
status: v.boolean(),
|
|
45
|
+
format: v.string(),
|
|
46
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export async function customTypeAddFieldBoolean(): Promise<void> {
|
|
50
|
+
const {
|
|
51
|
+
values: {
|
|
52
|
+
help,
|
|
53
|
+
tab,
|
|
54
|
+
label,
|
|
55
|
+
default: defaultValue,
|
|
56
|
+
"true-label": trueLabel,
|
|
57
|
+
"false-label": falseLabel,
|
|
58
|
+
types,
|
|
59
|
+
"no-types": noTypes,
|
|
60
|
+
},
|
|
61
|
+
positionals: [typeId, fieldId],
|
|
62
|
+
} = parseArgs({
|
|
63
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "boolean"
|
|
64
|
+
options: {
|
|
65
|
+
tab: { type: "string", short: "t" },
|
|
66
|
+
label: { type: "string", short: "l" },
|
|
67
|
+
default: { type: "boolean" },
|
|
68
|
+
"true-label": { type: "string" },
|
|
69
|
+
"false-label": { type: "string" },
|
|
70
|
+
types: { type: "string" },
|
|
71
|
+
"no-types": { type: "boolean" },
|
|
72
|
+
help: { type: "boolean", short: "h" },
|
|
73
|
+
},
|
|
74
|
+
allowPositionals: true,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
if (help) {
|
|
78
|
+
console.info(HELP);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!typeId) {
|
|
83
|
+
console.error("Missing required argument: type-id\n");
|
|
84
|
+
console.error("Usage: prismic custom-type add-field boolean <type-id> <field-id>");
|
|
85
|
+
process.exitCode = 1;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!fieldId) {
|
|
90
|
+
console.error("Missing required argument: field-id\n");
|
|
91
|
+
console.error("Usage: prismic custom-type add-field boolean <type-id> <field-id>");
|
|
92
|
+
process.exitCode = 1;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Find the custom type file
|
|
97
|
+
const projectRoot = await findUpward("package.json");
|
|
98
|
+
if (!projectRoot) {
|
|
99
|
+
console.error("Could not find project root (no package.json found)");
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
105
|
+
|
|
106
|
+
// Read and parse the model
|
|
107
|
+
let model: CustomType;
|
|
108
|
+
try {
|
|
109
|
+
const contents = await readFile(modelPath, "utf8");
|
|
110
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
111
|
+
if (!result.success) {
|
|
112
|
+
console.error(`Invalid custom type model: ${modelPath.href}`);
|
|
113
|
+
process.exitCode = 1;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
model = result.output as CustomType;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
119
|
+
console.error(`Custom type not found: ${typeId}\n`);
|
|
120
|
+
console.error(`Create it first with: prismic custom-type create ${typeId}`);
|
|
121
|
+
process.exitCode = 1;
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (error instanceof Error) {
|
|
125
|
+
console.error(`Failed to read custom type: ${error.message}`);
|
|
126
|
+
} else {
|
|
127
|
+
console.error("Failed to read custom type");
|
|
128
|
+
}
|
|
129
|
+
process.exitCode = 1;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Determine target tab
|
|
134
|
+
const existingTabs = Object.keys(model.json);
|
|
135
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
136
|
+
|
|
137
|
+
// Initialize tab if it doesn't exist
|
|
138
|
+
if (!model.json[targetTab]) {
|
|
139
|
+
model.json[targetTab] = {};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Check if field already exists in any tab
|
|
143
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
144
|
+
if (tabFields[fieldId]) {
|
|
145
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
146
|
+
process.exitCode = 1;
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Build field definition
|
|
152
|
+
const fieldDefinition: BooleanField = {
|
|
153
|
+
type: "Boolean",
|
|
154
|
+
config: {
|
|
155
|
+
label: label ?? humanReadable(fieldId),
|
|
156
|
+
...(defaultValue && { default_value: true }),
|
|
157
|
+
...(trueLabel && { placeholder_true: trueLabel }),
|
|
158
|
+
...(falseLabel && { placeholder_false: falseLabel }),
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// Add field to model
|
|
163
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
164
|
+
|
|
165
|
+
// Write updated model
|
|
166
|
+
try {
|
|
167
|
+
await writeFile(modelPath, stringify(model));
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (error instanceof Error) {
|
|
170
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
171
|
+
} else {
|
|
172
|
+
console.error("Failed to update custom type");
|
|
173
|
+
}
|
|
174
|
+
process.exitCode = 1;
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
console.info(`Added field "${fieldId}" (Boolean) to "${targetTab}" tab in ${typeId}`);
|
|
179
|
+
|
|
180
|
+
if (!noTypes) {
|
|
181
|
+
try {
|
|
182
|
+
await buildTypes({ output: types });
|
|
183
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
console.info();
|
|
190
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
191
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
192
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type { Color, CustomType } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import * as v from "valibot";
|
|
6
|
+
|
|
7
|
+
import { buildTypes } from "./codegen-types";
|
|
8
|
+
import { findUpward } from "./lib/file";
|
|
9
|
+
import { stringify } from "./lib/json";
|
|
10
|
+
import { humanReadable } from "./lib/string";
|
|
11
|
+
|
|
12
|
+
const HELP = `
|
|
13
|
+
Add a color picker field to an existing custom type.
|
|
14
|
+
|
|
15
|
+
USAGE
|
|
16
|
+
prismic custom-type add-field color <type-id> <field-id> [flags]
|
|
17
|
+
|
|
18
|
+
ARGUMENTS
|
|
19
|
+
type-id Custom type identifier (required)
|
|
20
|
+
field-id Field identifier (required)
|
|
21
|
+
|
|
22
|
+
Types are generated by default after changes. Use --no-types to skip.
|
|
23
|
+
|
|
24
|
+
FLAGS
|
|
25
|
+
-t, --tab string Target tab (default: first existing tab, or "Main")
|
|
26
|
+
-l, --label string Display label for the field (inferred from field-id if omitted)
|
|
27
|
+
-p, --placeholder string Placeholder text
|
|
28
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
29
|
+
--no-types Skip type generation
|
|
30
|
+
-h, --help Show help for command
|
|
31
|
+
|
|
32
|
+
EXAMPLES
|
|
33
|
+
prismic custom-type add-field color homepage bg_color
|
|
34
|
+
prismic custom-type add-field color homepage accent --tab "Design"
|
|
35
|
+
prismic custom-type add-field color homepage text_color --label "Text Color"
|
|
36
|
+
`.trim();
|
|
37
|
+
|
|
38
|
+
const CustomTypeSchema = v.object({
|
|
39
|
+
id: v.string(),
|
|
40
|
+
label: v.string(),
|
|
41
|
+
repeatable: v.boolean(),
|
|
42
|
+
status: v.boolean(),
|
|
43
|
+
format: v.string(),
|
|
44
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export async function customTypeAddFieldColor(): Promise<void> {
|
|
48
|
+
const {
|
|
49
|
+
values: { help, tab, label, placeholder, types, "no-types": noTypes },
|
|
50
|
+
positionals: [typeId, fieldId],
|
|
51
|
+
} = parseArgs({
|
|
52
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "color"
|
|
53
|
+
options: {
|
|
54
|
+
tab: { type: "string", short: "t" },
|
|
55
|
+
label: { type: "string", short: "l" },
|
|
56
|
+
placeholder: { type: "string", short: "p" },
|
|
57
|
+
types: { type: "string" },
|
|
58
|
+
"no-types": { type: "boolean" },
|
|
59
|
+
help: { type: "boolean", short: "h" },
|
|
60
|
+
},
|
|
61
|
+
allowPositionals: true,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (help) {
|
|
65
|
+
console.info(HELP);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!typeId) {
|
|
70
|
+
console.error("Missing required argument: type-id\n");
|
|
71
|
+
console.error("Usage: prismic custom-type add-field color <type-id> <field-id>");
|
|
72
|
+
process.exitCode = 1;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!fieldId) {
|
|
77
|
+
console.error("Missing required argument: field-id\n");
|
|
78
|
+
console.error("Usage: prismic custom-type add-field color <type-id> <field-id>");
|
|
79
|
+
process.exitCode = 1;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Find the custom type file
|
|
84
|
+
const projectRoot = await findUpward("package.json");
|
|
85
|
+
if (!projectRoot) {
|
|
86
|
+
console.error("Could not find project root (no package.json found)");
|
|
87
|
+
process.exitCode = 1;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
92
|
+
|
|
93
|
+
// Read and parse the model
|
|
94
|
+
let model: CustomType;
|
|
95
|
+
try {
|
|
96
|
+
const contents = await readFile(modelPath, "utf8");
|
|
97
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
98
|
+
if (!result.success) {
|
|
99
|
+
console.error(`Invalid custom type model: ${modelPath.href}`);
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
model = result.output as CustomType;
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
106
|
+
console.error(`Custom type not found: ${typeId}\n`);
|
|
107
|
+
console.error(`Create it first with: prismic custom-type create ${typeId}`);
|
|
108
|
+
process.exitCode = 1;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (error instanceof Error) {
|
|
112
|
+
console.error(`Failed to read custom type: ${error.message}`);
|
|
113
|
+
} else {
|
|
114
|
+
console.error("Failed to read custom type");
|
|
115
|
+
}
|
|
116
|
+
process.exitCode = 1;
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Determine target tab
|
|
121
|
+
const existingTabs = Object.keys(model.json);
|
|
122
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
123
|
+
|
|
124
|
+
// Initialize tab if it doesn't exist
|
|
125
|
+
if (!model.json[targetTab]) {
|
|
126
|
+
model.json[targetTab] = {};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Check if field already exists in any tab
|
|
130
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
131
|
+
if (tabFields[fieldId]) {
|
|
132
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
133
|
+
process.exitCode = 1;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Build field definition
|
|
139
|
+
const fieldDefinition: Color = {
|
|
140
|
+
type: "Color",
|
|
141
|
+
config: {
|
|
142
|
+
label: label ?? humanReadable(fieldId),
|
|
143
|
+
...(placeholder && { placeholder }),
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// Add field to model
|
|
148
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
149
|
+
|
|
150
|
+
// Write updated model
|
|
151
|
+
try {
|
|
152
|
+
await writeFile(modelPath, stringify(model));
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (error instanceof Error) {
|
|
155
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
156
|
+
} else {
|
|
157
|
+
console.error("Failed to update custom type");
|
|
158
|
+
}
|
|
159
|
+
process.exitCode = 1;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
console.info(`Added field "${fieldId}" (Color) to "${targetTab}" tab in ${typeId}`);
|
|
164
|
+
|
|
165
|
+
if (!noTypes) {
|
|
166
|
+
try {
|
|
167
|
+
await buildTypes({ output: types });
|
|
168
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
console.info();
|
|
175
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
176
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
177
|
+
}
|