@angeloashmore/prismic-cli-poc 0.0.0-canary.2ff9563
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 +1996 -0
- package/package.json +52 -0
- package/src/custom-type-add-field-boolean.ts +171 -0
- package/src/custom-type-add-field-color.ts +158 -0
- package/src/custom-type-add-field-date.ts +161 -0
- package/src/custom-type-add-field-embed.ts +158 -0
- package/src/custom-type-add-field-geo-point.ts +155 -0
- package/src/custom-type-add-field-image.ts +158 -0
- package/src/custom-type-add-field-key-text.ts +158 -0
- package/src/custom-type-add-field-link.ts +180 -0
- package/src/custom-type-add-field-number.ts +190 -0
- package/src/custom-type-add-field-rich-text.ts +181 -0
- package/src/custom-type-add-field-select.ts +164 -0
- package/src/custom-type-add-field-timestamp.ts +161 -0
- package/src/custom-type-add-field-uid.ts +158 -0
- package/src/custom-type-add-field.ts +111 -0
- package/src/custom-type-connect-slice.ts +221 -0
- package/src/custom-type-create.ts +92 -0
- package/src/custom-type-disconnect-slice.ts +179 -0
- package/src/custom-type-list.ts +110 -0
- package/src/custom-type-remove-field.ts +161 -0
- package/src/custom-type-remove.ts +126 -0
- package/src/custom-type-set-name.ts +128 -0
- package/src/custom-type-view.ts +118 -0
- package/src/custom-type.ts +85 -0
- package/src/index.ts +100 -0
- package/src/init.ts +62 -0
- package/src/lib/auth.ts +60 -0
- package/src/lib/config.ts +111 -0
- package/src/lib/file.ts +49 -0
- package/src/lib/json.ts +3 -0
- package/src/lib/request.ts +116 -0
- package/src/lib/slice.ts +112 -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 +143 -0
- package/src/logout.ts +36 -0
- package/src/page-type-add-field-boolean.ts +171 -0
- package/src/page-type-add-field-color.ts +158 -0
- package/src/page-type-add-field-date.ts +161 -0
- package/src/page-type-add-field-embed.ts +158 -0
- package/src/page-type-add-field-geo-point.ts +155 -0
- package/src/page-type-add-field-image.ts +158 -0
- package/src/page-type-add-field-key-text.ts +158 -0
- package/src/page-type-add-field-link.ts +180 -0
- package/src/page-type-add-field-number.ts +190 -0
- package/src/page-type-add-field-rich-text.ts +181 -0
- package/src/page-type-add-field-select.ts +164 -0
- package/src/page-type-add-field-timestamp.ts +161 -0
- package/src/page-type-add-field-uid.ts +158 -0
- package/src/page-type-add-field.ts +111 -0
- package/src/page-type-connect-slice.ts +221 -0
- package/src/page-type-create.ts +93 -0
- package/src/page-type-disconnect-slice.ts +179 -0
- package/src/page-type-list.ts +109 -0
- package/src/page-type-remove-field.ts +161 -0
- package/src/page-type-remove.ts +126 -0
- package/src/page-type-set-name.ts +128 -0
- package/src/page-type-set-repeatable.ts +137 -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-list.ts +106 -0
- package/src/preview-remove.ts +109 -0
- package/src/preview-set-name.ts +137 -0
- package/src/preview.ts +60 -0
- package/src/repo-create.ts +136 -0
- package/src/repo-list.ts +100 -0
- package/src/repo-set-name.ts +102 -0
- package/src/repo-view.ts +113 -0
- package/src/repo.ts +60 -0
- package/src/slice-add-field-boolean.ts +150 -0
- package/src/slice-add-field-color.ts +137 -0
- package/src/slice-add-field-date.ts +137 -0
- package/src/slice-add-field-embed.ts +137 -0
- package/src/slice-add-field-geo-point.ts +134 -0
- package/src/slice-add-field-image.ts +134 -0
- package/src/slice-add-field-key-text.ts +137 -0
- package/src/slice-add-field-link.ts +155 -0
- package/src/slice-add-field-number.ts +137 -0
- package/src/slice-add-field-rich-text.ts +160 -0
- package/src/slice-add-field-select.ts +143 -0
- package/src/slice-add-field-timestamp.ts +137 -0
- package/src/slice-add-field.ts +106 -0
- package/src/slice-add-variation.ts +137 -0
- package/src/slice-create.ts +129 -0
- package/src/slice-list-variations.ts +67 -0
- package/src/slice-list.ts +88 -0
- package/src/slice-remove-field.ts +117 -0
- package/src/slice-remove-variation.ts +108 -0
- package/src/slice-remove.ts +81 -0
- package/src/slice-rename.ts +112 -0
- package/src/slice-view.ts +77 -0
- package/src/slice.ts +90 -0
- package/src/sync.ts +309 -0
- package/src/token-create.ts +185 -0
- package/src/token-delete.ts +161 -0
- package/src/token-list.ts +212 -0
- package/src/token-set-name.ts +165 -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,158 @@
|
|
|
1
|
+
import type { CustomType, Embed } 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 { findUpward } from "./lib/file";
|
|
8
|
+
import { stringify } from "./lib/json";
|
|
9
|
+
|
|
10
|
+
const HELP = `
|
|
11
|
+
Add an embed field to an existing page type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic page-type add-field embed <type-id> <field-id> [flags]
|
|
15
|
+
|
|
16
|
+
ARGUMENTS
|
|
17
|
+
type-id Page type identifier (required)
|
|
18
|
+
field-id Field identifier (required)
|
|
19
|
+
|
|
20
|
+
FLAGS
|
|
21
|
+
-t, --tab string Target tab (default: first existing tab, or "Main")
|
|
22
|
+
-l, --label string Display label for the field
|
|
23
|
+
-p, --placeholder string Placeholder text
|
|
24
|
+
-h, --help Show help for command
|
|
25
|
+
|
|
26
|
+
EXAMPLES
|
|
27
|
+
prismic page-type add-field embed homepage video
|
|
28
|
+
prismic page-type add-field embed homepage youtube --tab "Media"
|
|
29
|
+
prismic page-type add-field embed homepage media --label "Media Embed"
|
|
30
|
+
`.trim();
|
|
31
|
+
|
|
32
|
+
const CustomTypeSchema = v.object({
|
|
33
|
+
id: v.string(),
|
|
34
|
+
label: v.string(),
|
|
35
|
+
repeatable: v.boolean(),
|
|
36
|
+
status: v.boolean(),
|
|
37
|
+
format: v.string(),
|
|
38
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export async function pageTypeAddFieldEmbed(): Promise<void> {
|
|
42
|
+
const {
|
|
43
|
+
values: { help, tab, label, placeholder },
|
|
44
|
+
positionals: [typeId, fieldId],
|
|
45
|
+
} = parseArgs({
|
|
46
|
+
args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "embed"
|
|
47
|
+
options: {
|
|
48
|
+
tab: { type: "string", short: "t" },
|
|
49
|
+
label: { type: "string", short: "l" },
|
|
50
|
+
placeholder: { type: "string", short: "p" },
|
|
51
|
+
help: { type: "boolean", short: "h" },
|
|
52
|
+
},
|
|
53
|
+
allowPositionals: true,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (help) {
|
|
57
|
+
console.info(HELP);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!typeId) {
|
|
62
|
+
console.error("Missing required argument: type-id\n");
|
|
63
|
+
console.error("Usage: prismic page-type add-field embed <type-id> <field-id>");
|
|
64
|
+
process.exitCode = 1;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!fieldId) {
|
|
69
|
+
console.error("Missing required argument: field-id\n");
|
|
70
|
+
console.error("Usage: prismic page-type add-field embed <type-id> <field-id>");
|
|
71
|
+
process.exitCode = 1;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Find the page type file
|
|
76
|
+
const projectRoot = await findUpward("package.json");
|
|
77
|
+
if (!projectRoot) {
|
|
78
|
+
console.error("Could not find project root (no package.json found)");
|
|
79
|
+
process.exitCode = 1;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
84
|
+
|
|
85
|
+
// Read and parse the model
|
|
86
|
+
let model: CustomType;
|
|
87
|
+
try {
|
|
88
|
+
const contents = await readFile(modelPath, "utf8");
|
|
89
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
90
|
+
if (!result.success) {
|
|
91
|
+
console.error(`Invalid page type model: ${modelPath.href}`);
|
|
92
|
+
process.exitCode = 1;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
model = result.output as CustomType;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
98
|
+
console.error(`Page type not found: ${typeId}\n`);
|
|
99
|
+
console.error(`Create it first with: prismic page-type create ${typeId}`);
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (error instanceof Error) {
|
|
104
|
+
console.error(`Failed to read page type: ${error.message}`);
|
|
105
|
+
} else {
|
|
106
|
+
console.error("Failed to read page type");
|
|
107
|
+
}
|
|
108
|
+
process.exitCode = 1;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Determine target tab
|
|
113
|
+
const existingTabs = Object.keys(model.json);
|
|
114
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
115
|
+
|
|
116
|
+
// Initialize tab if it doesn't exist
|
|
117
|
+
if (!model.json[targetTab]) {
|
|
118
|
+
model.json[targetTab] = {};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Check if field already exists in any tab
|
|
122
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
123
|
+
if (tabFields[fieldId]) {
|
|
124
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
125
|
+
process.exitCode = 1;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Build field definition
|
|
131
|
+
const fieldDefinition: Embed = {
|
|
132
|
+
type: "Embed",
|
|
133
|
+
config: {
|
|
134
|
+
...(label && { label }),
|
|
135
|
+
...(placeholder && { placeholder }),
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// Add field to model
|
|
140
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
141
|
+
|
|
142
|
+
// Write updated model
|
|
143
|
+
try {
|
|
144
|
+
await writeFile(modelPath, stringify(model));
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error instanceof Error) {
|
|
147
|
+
console.error(`Failed to update page type: ${error.message}`);
|
|
148
|
+
} else {
|
|
149
|
+
console.error("Failed to update page type");
|
|
150
|
+
}
|
|
151
|
+
process.exitCode = 1;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
console.info(
|
|
156
|
+
`Added field "${fieldId}" (Embed) to "${targetTab}" tab in ${typeId}`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { CustomType, GeoPoint } 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 { findUpward } from "./lib/file";
|
|
8
|
+
import { stringify } from "./lib/json";
|
|
9
|
+
|
|
10
|
+
const HELP = `
|
|
11
|
+
Add a geo-point (location) field to an existing page type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic page-type add-field geo-point <type-id> <field-id> [flags]
|
|
15
|
+
|
|
16
|
+
ARGUMENTS
|
|
17
|
+
type-id Page type identifier (required)
|
|
18
|
+
field-id Field identifier (required)
|
|
19
|
+
|
|
20
|
+
FLAGS
|
|
21
|
+
-t, --tab string Target tab (default: first existing tab, or "Main")
|
|
22
|
+
-l, --label string Display label for the field
|
|
23
|
+
-h, --help Show help for command
|
|
24
|
+
|
|
25
|
+
EXAMPLES
|
|
26
|
+
prismic page-type add-field geo-point homepage location
|
|
27
|
+
prismic page-type add-field geo-point store address --tab "Details"
|
|
28
|
+
prismic page-type add-field geo-point event venue --label "Event Venue"
|
|
29
|
+
`.trim();
|
|
30
|
+
|
|
31
|
+
const CustomTypeSchema = v.object({
|
|
32
|
+
id: v.string(),
|
|
33
|
+
label: v.string(),
|
|
34
|
+
repeatable: v.boolean(),
|
|
35
|
+
status: v.boolean(),
|
|
36
|
+
format: v.string(),
|
|
37
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export async function pageTypeAddFieldGeoPoint(): Promise<void> {
|
|
41
|
+
const {
|
|
42
|
+
values: { help, tab, label },
|
|
43
|
+
positionals: [typeId, fieldId],
|
|
44
|
+
} = parseArgs({
|
|
45
|
+
args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "geo-point"
|
|
46
|
+
options: {
|
|
47
|
+
tab: { type: "string", short: "t" },
|
|
48
|
+
label: { type: "string", short: "l" },
|
|
49
|
+
help: { type: "boolean", short: "h" },
|
|
50
|
+
},
|
|
51
|
+
allowPositionals: true,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
if (help) {
|
|
55
|
+
console.info(HELP);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!typeId) {
|
|
60
|
+
console.error("Missing required argument: type-id\n");
|
|
61
|
+
console.error("Usage: prismic page-type add-field geo-point <type-id> <field-id>");
|
|
62
|
+
process.exitCode = 1;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!fieldId) {
|
|
67
|
+
console.error("Missing required argument: field-id\n");
|
|
68
|
+
console.error("Usage: prismic page-type add-field geo-point <type-id> <field-id>");
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Find the page type file
|
|
74
|
+
const projectRoot = await findUpward("package.json");
|
|
75
|
+
if (!projectRoot) {
|
|
76
|
+
console.error("Could not find project root (no package.json found)");
|
|
77
|
+
process.exitCode = 1;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
82
|
+
|
|
83
|
+
// Read and parse the model
|
|
84
|
+
let model: CustomType;
|
|
85
|
+
try {
|
|
86
|
+
const contents = await readFile(modelPath, "utf8");
|
|
87
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
88
|
+
if (!result.success) {
|
|
89
|
+
console.error(`Invalid page type model: ${modelPath.href}`);
|
|
90
|
+
process.exitCode = 1;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
model = result.output as CustomType;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
96
|
+
console.error(`Page type not found: ${typeId}\n`);
|
|
97
|
+
console.error(`Create it first with: prismic page-type create ${typeId}`);
|
|
98
|
+
process.exitCode = 1;
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (error instanceof Error) {
|
|
102
|
+
console.error(`Failed to read page type: ${error.message}`);
|
|
103
|
+
} else {
|
|
104
|
+
console.error("Failed to read page type");
|
|
105
|
+
}
|
|
106
|
+
process.exitCode = 1;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Determine target tab
|
|
111
|
+
const existingTabs = Object.keys(model.json);
|
|
112
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
113
|
+
|
|
114
|
+
// Initialize tab if it doesn't exist
|
|
115
|
+
if (!model.json[targetTab]) {
|
|
116
|
+
model.json[targetTab] = {};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Check if field already exists in any tab
|
|
120
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
121
|
+
if (tabFields[fieldId]) {
|
|
122
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
123
|
+
process.exitCode = 1;
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Build field definition
|
|
129
|
+
const fieldDefinition: GeoPoint = {
|
|
130
|
+
type: "GeoPoint",
|
|
131
|
+
config: {
|
|
132
|
+
...(label && { label }),
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Add field to model
|
|
137
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
138
|
+
|
|
139
|
+
// Write updated model
|
|
140
|
+
try {
|
|
141
|
+
await writeFile(modelPath, stringify(model));
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (error instanceof Error) {
|
|
144
|
+
console.error(`Failed to update page type: ${error.message}`);
|
|
145
|
+
} else {
|
|
146
|
+
console.error("Failed to update page type");
|
|
147
|
+
}
|
|
148
|
+
process.exitCode = 1;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
console.info(
|
|
153
|
+
`Added field "${fieldId}" (GeoPoint) to "${targetTab}" tab in ${typeId}`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { CustomType, Image } 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 { findUpward } from "./lib/file";
|
|
8
|
+
import { stringify } from "./lib/json";
|
|
9
|
+
|
|
10
|
+
const HELP = `
|
|
11
|
+
Add an image field to an existing page type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic page-type add-field image <type-id> <field-id> [flags]
|
|
15
|
+
|
|
16
|
+
ARGUMENTS
|
|
17
|
+
type-id Page type identifier (required)
|
|
18
|
+
field-id Field identifier (required)
|
|
19
|
+
|
|
20
|
+
FLAGS
|
|
21
|
+
-t, --tab string Target tab (default: first existing tab, or "Main")
|
|
22
|
+
-l, --label string Display label for the field
|
|
23
|
+
-p, --placeholder string Placeholder text
|
|
24
|
+
-h, --help Show help for command
|
|
25
|
+
|
|
26
|
+
EXAMPLES
|
|
27
|
+
prismic page-type add-field image homepage hero
|
|
28
|
+
prismic page-type add-field image article thumbnail --tab "Media"
|
|
29
|
+
prismic page-type add-field image product photo --label "Product Photo"
|
|
30
|
+
`.trim();
|
|
31
|
+
|
|
32
|
+
const CustomTypeSchema = v.object({
|
|
33
|
+
id: v.string(),
|
|
34
|
+
label: v.string(),
|
|
35
|
+
repeatable: v.boolean(),
|
|
36
|
+
status: v.boolean(),
|
|
37
|
+
format: v.string(),
|
|
38
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export async function pageTypeAddFieldImage(): Promise<void> {
|
|
42
|
+
const {
|
|
43
|
+
values: { help, tab, label, placeholder },
|
|
44
|
+
positionals: [typeId, fieldId],
|
|
45
|
+
} = parseArgs({
|
|
46
|
+
args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "image"
|
|
47
|
+
options: {
|
|
48
|
+
tab: { type: "string", short: "t" },
|
|
49
|
+
label: { type: "string", short: "l" },
|
|
50
|
+
placeholder: { type: "string", short: "p" },
|
|
51
|
+
help: { type: "boolean", short: "h" },
|
|
52
|
+
},
|
|
53
|
+
allowPositionals: true,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (help) {
|
|
57
|
+
console.info(HELP);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!typeId) {
|
|
62
|
+
console.error("Missing required argument: type-id\n");
|
|
63
|
+
console.error("Usage: prismic page-type add-field image <type-id> <field-id>");
|
|
64
|
+
process.exitCode = 1;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!fieldId) {
|
|
69
|
+
console.error("Missing required argument: field-id\n");
|
|
70
|
+
console.error("Usage: prismic page-type add-field image <type-id> <field-id>");
|
|
71
|
+
process.exitCode = 1;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Find the page type file
|
|
76
|
+
const projectRoot = await findUpward("package.json");
|
|
77
|
+
if (!projectRoot) {
|
|
78
|
+
console.error("Could not find project root (no package.json found)");
|
|
79
|
+
process.exitCode = 1;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
84
|
+
|
|
85
|
+
// Read and parse the model
|
|
86
|
+
let model: CustomType;
|
|
87
|
+
try {
|
|
88
|
+
const contents = await readFile(modelPath, "utf8");
|
|
89
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
90
|
+
if (!result.success) {
|
|
91
|
+
console.error(`Invalid page type model: ${modelPath.href}`);
|
|
92
|
+
process.exitCode = 1;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
model = result.output as CustomType;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
98
|
+
console.error(`Page type not found: ${typeId}\n`);
|
|
99
|
+
console.error(`Create it first with: prismic page-type create ${typeId}`);
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (error instanceof Error) {
|
|
104
|
+
console.error(`Failed to read page type: ${error.message}`);
|
|
105
|
+
} else {
|
|
106
|
+
console.error("Failed to read page type");
|
|
107
|
+
}
|
|
108
|
+
process.exitCode = 1;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Determine target tab
|
|
113
|
+
const existingTabs = Object.keys(model.json);
|
|
114
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
115
|
+
|
|
116
|
+
// Initialize tab if it doesn't exist
|
|
117
|
+
if (!model.json[targetTab]) {
|
|
118
|
+
model.json[targetTab] = {};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Check if field already exists in any tab
|
|
122
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
123
|
+
if (tabFields[fieldId]) {
|
|
124
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
125
|
+
process.exitCode = 1;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Build field definition
|
|
131
|
+
const fieldDefinition: Image = {
|
|
132
|
+
type: "Image",
|
|
133
|
+
config: {
|
|
134
|
+
...(label && { label }),
|
|
135
|
+
...(placeholder && { placeholder }),
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// Add field to model
|
|
140
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
141
|
+
|
|
142
|
+
// Write updated model
|
|
143
|
+
try {
|
|
144
|
+
await writeFile(modelPath, stringify(model));
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error instanceof Error) {
|
|
147
|
+
console.error(`Failed to update page type: ${error.message}`);
|
|
148
|
+
} else {
|
|
149
|
+
console.error("Failed to update page type");
|
|
150
|
+
}
|
|
151
|
+
process.exitCode = 1;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
console.info(
|
|
156
|
+
`Added field "${fieldId}" (Image) to "${targetTab}" tab in ${typeId}`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
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 { findUpward } from "./lib/file";
|
|
8
|
+
import { stringify } from "./lib/json";
|
|
9
|
+
|
|
10
|
+
const HELP = `
|
|
11
|
+
Add a key-text (single-line text) field to an existing page type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic page-type add-field key-text <type-id> <field-id> [flags]
|
|
15
|
+
|
|
16
|
+
ARGUMENTS
|
|
17
|
+
type-id Page type identifier (required)
|
|
18
|
+
field-id Field identifier (required)
|
|
19
|
+
|
|
20
|
+
FLAGS
|
|
21
|
+
-t, --tab string Target tab (default: first existing tab, or "Main")
|
|
22
|
+
-l, --label string Display label for the field
|
|
23
|
+
-p, --placeholder string Placeholder text
|
|
24
|
+
-h, --help Show help for command
|
|
25
|
+
|
|
26
|
+
EXAMPLES
|
|
27
|
+
prismic page-type add-field key-text homepage title
|
|
28
|
+
prismic page-type add-field key-text homepage meta_title --tab "SEO"
|
|
29
|
+
prismic page-type add-field key-text homepage subtitle --label "Subtitle" --placeholder "Enter subtitle"
|
|
30
|
+
`.trim();
|
|
31
|
+
|
|
32
|
+
const CustomTypeSchema = v.object({
|
|
33
|
+
id: v.string(),
|
|
34
|
+
label: v.string(),
|
|
35
|
+
repeatable: v.boolean(),
|
|
36
|
+
status: v.boolean(),
|
|
37
|
+
format: v.string(),
|
|
38
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export async function pageTypeAddFieldKeyText(): Promise<void> {
|
|
42
|
+
const {
|
|
43
|
+
values: { help, tab, label, placeholder },
|
|
44
|
+
positionals: [typeId, fieldId],
|
|
45
|
+
} = parseArgs({
|
|
46
|
+
args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "key-text"
|
|
47
|
+
options: {
|
|
48
|
+
tab: { type: "string", short: "t" },
|
|
49
|
+
label: { type: "string", short: "l" },
|
|
50
|
+
placeholder: { type: "string", short: "p" },
|
|
51
|
+
help: { type: "boolean", short: "h" },
|
|
52
|
+
},
|
|
53
|
+
allowPositionals: true,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (help) {
|
|
57
|
+
console.info(HELP);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!typeId) {
|
|
62
|
+
console.error("Missing required argument: type-id\n");
|
|
63
|
+
console.error("Usage: prismic page-type add-field key-text <type-id> <field-id>");
|
|
64
|
+
process.exitCode = 1;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!fieldId) {
|
|
69
|
+
console.error("Missing required argument: field-id\n");
|
|
70
|
+
console.error("Usage: prismic page-type add-field key-text <type-id> <field-id>");
|
|
71
|
+
process.exitCode = 1;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Find the page type file
|
|
76
|
+
const projectRoot = await findUpward("package.json");
|
|
77
|
+
if (!projectRoot) {
|
|
78
|
+
console.error("Could not find project root (no package.json found)");
|
|
79
|
+
process.exitCode = 1;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
84
|
+
|
|
85
|
+
// Read and parse the model
|
|
86
|
+
let model: CustomType;
|
|
87
|
+
try {
|
|
88
|
+
const contents = await readFile(modelPath, "utf8");
|
|
89
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
90
|
+
if (!result.success) {
|
|
91
|
+
console.error(`Invalid page type model: ${modelPath.href}`);
|
|
92
|
+
process.exitCode = 1;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
model = result.output as CustomType;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
98
|
+
console.error(`Page type not found: ${typeId}\n`);
|
|
99
|
+
console.error(`Create it first with: prismic page-type create ${typeId}`);
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (error instanceof Error) {
|
|
104
|
+
console.error(`Failed to read page type: ${error.message}`);
|
|
105
|
+
} else {
|
|
106
|
+
console.error("Failed to read page type");
|
|
107
|
+
}
|
|
108
|
+
process.exitCode = 1;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Determine target tab
|
|
113
|
+
const existingTabs = Object.keys(model.json);
|
|
114
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
115
|
+
|
|
116
|
+
// Initialize tab if it doesn't exist
|
|
117
|
+
if (!model.json[targetTab]) {
|
|
118
|
+
model.json[targetTab] = {};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Check if field already exists in any tab
|
|
122
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
123
|
+
if (tabFields[fieldId]) {
|
|
124
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
125
|
+
process.exitCode = 1;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Build field definition
|
|
131
|
+
const fieldDefinition: Text = {
|
|
132
|
+
type: "Text",
|
|
133
|
+
config: {
|
|
134
|
+
...(label && { label }),
|
|
135
|
+
...(placeholder && { placeholder }),
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// Add field to model
|
|
140
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
141
|
+
|
|
142
|
+
// Write updated model
|
|
143
|
+
try {
|
|
144
|
+
await writeFile(modelPath, stringify(model));
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error instanceof Error) {
|
|
147
|
+
console.error(`Failed to update page type: ${error.message}`);
|
|
148
|
+
} else {
|
|
149
|
+
console.error("Failed to update page type");
|
|
150
|
+
}
|
|
151
|
+
process.exitCode = 1;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
console.info(
|
|
156
|
+
`Added field "${fieldId}" (Text) to "${targetTab}" tab in ${typeId}`,
|
|
157
|
+
);
|
|
158
|
+
}
|