@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
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type { CustomType, Text } 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 key-text (single-line text) field to an existing custom type.
|
|
14
|
+
|
|
15
|
+
USAGE
|
|
16
|
+
prismic custom-type add-field key-text <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 key-text homepage title
|
|
34
|
+
prismic custom-type add-field key-text homepage meta_title --tab "SEO"
|
|
35
|
+
prismic custom-type add-field key-text homepage subtitle --label "Subtitle" --placeholder "Enter subtitle"
|
|
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 customTypeAddFieldKeyText(): 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", "key-text"
|
|
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 key-text <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 key-text <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: Text = {
|
|
140
|
+
type: "Text",
|
|
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}" (Text) 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
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { CustomType, Link } 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 link field to an existing custom type.
|
|
14
|
+
|
|
15
|
+
USAGE
|
|
16
|
+
prismic custom-type add-field link <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
|
+
--variation string Slice variations (can be used multiple times)
|
|
29
|
+
--allow-text Allow text with link
|
|
30
|
+
--allow-target-blank Allow opening link in new tab
|
|
31
|
+
--repeatable Allow multiple links
|
|
32
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
33
|
+
--no-types Skip type generation
|
|
34
|
+
-h, --help Show help for command
|
|
35
|
+
|
|
36
|
+
EXAMPLES
|
|
37
|
+
prismic custom-type add-field link homepage button
|
|
38
|
+
prismic custom-type add-field link homepage cta --allow-text
|
|
39
|
+
prismic custom-type add-field link homepage cta --variation Primary --variation Secondary
|
|
40
|
+
prismic custom-type add-field link homepage links --repeatable
|
|
41
|
+
`.trim();
|
|
42
|
+
|
|
43
|
+
const CustomTypeSchema = v.object({
|
|
44
|
+
id: v.string(),
|
|
45
|
+
label: v.string(),
|
|
46
|
+
repeatable: v.boolean(),
|
|
47
|
+
status: v.boolean(),
|
|
48
|
+
format: v.string(),
|
|
49
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export async function customTypeAddFieldLink(): Promise<void> {
|
|
53
|
+
const {
|
|
54
|
+
values: {
|
|
55
|
+
help,
|
|
56
|
+
tab,
|
|
57
|
+
label,
|
|
58
|
+
placeholder,
|
|
59
|
+
variation,
|
|
60
|
+
"allow-text": allowText,
|
|
61
|
+
"allow-target-blank": allowTargetBlank,
|
|
62
|
+
repeatable,
|
|
63
|
+
types,
|
|
64
|
+
"no-types": noTypes,
|
|
65
|
+
},
|
|
66
|
+
positionals: [typeId, fieldId],
|
|
67
|
+
} = parseArgs({
|
|
68
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "link"
|
|
69
|
+
options: {
|
|
70
|
+
tab: { type: "string", short: "t" },
|
|
71
|
+
label: { type: "string", short: "l" },
|
|
72
|
+
placeholder: { type: "string", short: "p" },
|
|
73
|
+
variation: { type: "string", multiple: true },
|
|
74
|
+
"allow-text": { type: "boolean" },
|
|
75
|
+
"allow-target-blank": { type: "boolean" },
|
|
76
|
+
repeatable: { type: "boolean" },
|
|
77
|
+
types: { type: "string" },
|
|
78
|
+
"no-types": { type: "boolean" },
|
|
79
|
+
help: { type: "boolean", short: "h" },
|
|
80
|
+
},
|
|
81
|
+
allowPositionals: true,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
if (help) {
|
|
85
|
+
console.info(HELP);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!typeId) {
|
|
90
|
+
console.error("Missing required argument: type-id\n");
|
|
91
|
+
console.error("Usage: prismic custom-type add-field link <type-id> <field-id>");
|
|
92
|
+
process.exitCode = 1;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!fieldId) {
|
|
97
|
+
console.error("Missing required argument: field-id\n");
|
|
98
|
+
console.error("Usage: prismic custom-type add-field link <type-id> <field-id>");
|
|
99
|
+
process.exitCode = 1;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Find the custom type file
|
|
104
|
+
const projectRoot = await findUpward("package.json");
|
|
105
|
+
if (!projectRoot) {
|
|
106
|
+
console.error("Could not find project root (no package.json found)");
|
|
107
|
+
process.exitCode = 1;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
112
|
+
|
|
113
|
+
// Read and parse the model
|
|
114
|
+
let model: CustomType;
|
|
115
|
+
try {
|
|
116
|
+
const contents = await readFile(modelPath, "utf8");
|
|
117
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
118
|
+
if (!result.success) {
|
|
119
|
+
console.error(`Invalid custom type model: ${modelPath.href}`);
|
|
120
|
+
process.exitCode = 1;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
model = result.output as CustomType;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
126
|
+
console.error(`Custom type not found: ${typeId}\n`);
|
|
127
|
+
console.error(`Create it first with: prismic custom-type create ${typeId}`);
|
|
128
|
+
process.exitCode = 1;
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (error instanceof Error) {
|
|
132
|
+
console.error(`Failed to read custom type: ${error.message}`);
|
|
133
|
+
} else {
|
|
134
|
+
console.error("Failed to read custom type");
|
|
135
|
+
}
|
|
136
|
+
process.exitCode = 1;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Determine target tab
|
|
141
|
+
const existingTabs = Object.keys(model.json);
|
|
142
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
143
|
+
|
|
144
|
+
// Initialize tab if it doesn't exist
|
|
145
|
+
if (!model.json[targetTab]) {
|
|
146
|
+
model.json[targetTab] = {};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Check if field already exists in any tab
|
|
150
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
151
|
+
if (tabFields[fieldId]) {
|
|
152
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
153
|
+
process.exitCode = 1;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Build field definition
|
|
159
|
+
const fieldDefinition: Link = {
|
|
160
|
+
type: "Link",
|
|
161
|
+
config: {
|
|
162
|
+
label: label ?? humanReadable(fieldId),
|
|
163
|
+
...(placeholder && { placeholder }),
|
|
164
|
+
...(variation && variation.length > 0 && { variants: variation }),
|
|
165
|
+
...(allowText && { allowText: true }),
|
|
166
|
+
...(allowTargetBlank && { allowTargetBlank: true }),
|
|
167
|
+
...(repeatable && { repeat: true }),
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// Add field to model
|
|
172
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
173
|
+
|
|
174
|
+
// Write updated model
|
|
175
|
+
try {
|
|
176
|
+
await writeFile(modelPath, stringify(model));
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (error instanceof Error) {
|
|
179
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
180
|
+
} else {
|
|
181
|
+
console.error("Failed to update custom type");
|
|
182
|
+
}
|
|
183
|
+
process.exitCode = 1;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
console.info(`Added field "${fieldId}" (Link) to "${targetTab}" tab in ${typeId}`);
|
|
188
|
+
|
|
189
|
+
if (!noTypes) {
|
|
190
|
+
try {
|
|
191
|
+
await buildTypes({ output: types });
|
|
192
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
console.info();
|
|
199
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
200
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
201
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import type { CustomType, Number as NumberField } 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 number field to an existing custom type.
|
|
14
|
+
|
|
15
|
+
USAGE
|
|
16
|
+
prismic custom-type add-field number <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
|
+
--min number Minimum value
|
|
29
|
+
--max number Maximum value
|
|
30
|
+
--step number Step increment
|
|
31
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
32
|
+
--no-types Skip type generation
|
|
33
|
+
-h, --help Show help for command
|
|
34
|
+
|
|
35
|
+
EXAMPLES
|
|
36
|
+
prismic custom-type add-field number homepage price
|
|
37
|
+
prismic custom-type add-field number product quantity --min 0 --max 100
|
|
38
|
+
prismic custom-type add-field number settings rating --min 1 --max 5 --step 1
|
|
39
|
+
`.trim();
|
|
40
|
+
|
|
41
|
+
const CustomTypeSchema = v.object({
|
|
42
|
+
id: v.string(),
|
|
43
|
+
label: v.string(),
|
|
44
|
+
repeatable: v.boolean(),
|
|
45
|
+
status: v.boolean(),
|
|
46
|
+
format: v.string(),
|
|
47
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
export async function customTypeAddFieldNumber(): Promise<void> {
|
|
51
|
+
const {
|
|
52
|
+
values: { help, tab, label, placeholder, min, max, step, types, "no-types": noTypes },
|
|
53
|
+
positionals: [typeId, fieldId],
|
|
54
|
+
} = parseArgs({
|
|
55
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "number"
|
|
56
|
+
options: {
|
|
57
|
+
tab: { type: "string", short: "t" },
|
|
58
|
+
label: { type: "string", short: "l" },
|
|
59
|
+
placeholder: { type: "string", short: "p" },
|
|
60
|
+
min: { type: "string" },
|
|
61
|
+
max: { type: "string" },
|
|
62
|
+
step: { type: "string" },
|
|
63
|
+
types: { type: "string" },
|
|
64
|
+
"no-types": { type: "boolean" },
|
|
65
|
+
help: { type: "boolean", short: "h" },
|
|
66
|
+
},
|
|
67
|
+
allowPositionals: true,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (help) {
|
|
71
|
+
console.info(HELP);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!typeId) {
|
|
76
|
+
console.error("Missing required argument: type-id\n");
|
|
77
|
+
console.error("Usage: prismic custom-type add-field number <type-id> <field-id>");
|
|
78
|
+
process.exitCode = 1;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!fieldId) {
|
|
83
|
+
console.error("Missing required argument: field-id\n");
|
|
84
|
+
console.error("Usage: prismic custom-type add-field number <type-id> <field-id>");
|
|
85
|
+
process.exitCode = 1;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Parse numeric values
|
|
90
|
+
const minValue = min !== undefined ? Number(min) : undefined;
|
|
91
|
+
const maxValue = max !== undefined ? Number(max) : undefined;
|
|
92
|
+
const stepValue = step !== undefined ? Number(step) : undefined;
|
|
93
|
+
|
|
94
|
+
if (min !== undefined && Number.isNaN(minValue)) {
|
|
95
|
+
console.error("Invalid --min value: must be a number");
|
|
96
|
+
process.exitCode = 1;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (max !== undefined && Number.isNaN(maxValue)) {
|
|
101
|
+
console.error("Invalid --max value: must be a number");
|
|
102
|
+
process.exitCode = 1;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (step !== undefined && Number.isNaN(stepValue)) {
|
|
107
|
+
console.error("Invalid --step value: must be a number");
|
|
108
|
+
process.exitCode = 1;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Find the custom type file
|
|
113
|
+
const projectRoot = await findUpward("package.json");
|
|
114
|
+
if (!projectRoot) {
|
|
115
|
+
console.error("Could not find project root (no package.json found)");
|
|
116
|
+
process.exitCode = 1;
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
121
|
+
|
|
122
|
+
// Read and parse the model
|
|
123
|
+
let model: CustomType;
|
|
124
|
+
try {
|
|
125
|
+
const contents = await readFile(modelPath, "utf8");
|
|
126
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
127
|
+
if (!result.success) {
|
|
128
|
+
console.error(`Invalid custom type model: ${modelPath.href}`);
|
|
129
|
+
process.exitCode = 1;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
model = result.output as CustomType;
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
135
|
+
console.error(`Custom type not found: ${typeId}\n`);
|
|
136
|
+
console.error(`Create it first with: prismic custom-type create ${typeId}`);
|
|
137
|
+
process.exitCode = 1;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (error instanceof Error) {
|
|
141
|
+
console.error(`Failed to read custom type: ${error.message}`);
|
|
142
|
+
} else {
|
|
143
|
+
console.error("Failed to read custom type");
|
|
144
|
+
}
|
|
145
|
+
process.exitCode = 1;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Determine target tab
|
|
150
|
+
const existingTabs = Object.keys(model.json);
|
|
151
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
152
|
+
|
|
153
|
+
// Initialize tab if it doesn't exist
|
|
154
|
+
if (!model.json[targetTab]) {
|
|
155
|
+
model.json[targetTab] = {};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Check if field already exists in any tab
|
|
159
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
160
|
+
if (tabFields[fieldId]) {
|
|
161
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
162
|
+
process.exitCode = 1;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Build field definition
|
|
168
|
+
const fieldDefinition: NumberField = {
|
|
169
|
+
type: "Number",
|
|
170
|
+
config: {
|
|
171
|
+
label: label ?? humanReadable(fieldId),
|
|
172
|
+
...(placeholder && { placeholder }),
|
|
173
|
+
...(minValue !== undefined && { min: minValue }),
|
|
174
|
+
...(maxValue !== undefined && { max: maxValue }),
|
|
175
|
+
...(stepValue !== undefined && { step: stepValue }),
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// Add field to model
|
|
180
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
181
|
+
|
|
182
|
+
// Write updated model
|
|
183
|
+
try {
|
|
184
|
+
await writeFile(modelPath, stringify(model));
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (error instanceof Error) {
|
|
187
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
188
|
+
} else {
|
|
189
|
+
console.error("Failed to update custom type");
|
|
190
|
+
}
|
|
191
|
+
process.exitCode = 1;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
console.info(`Added field "${fieldId}" (Number) to "${targetTab}" tab in ${typeId}`);
|
|
196
|
+
|
|
197
|
+
if (!noTypes) {
|
|
198
|
+
try {
|
|
199
|
+
await buildTypes({ output: types });
|
|
200
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
console.info();
|
|
207
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
208
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
209
|
+
}
|