@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,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 custom type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic custom-type add-field geo-point <type-id> <field-id> [flags]
|
|
15
|
+
|
|
16
|
+
ARGUMENTS
|
|
17
|
+
type-id Custom 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 custom-type add-field geo-point homepage location
|
|
27
|
+
prismic custom-type add-field geo-point store address --tab "Details"
|
|
28
|
+
prismic custom-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 customTypeAddFieldGeoPoint(): Promise<void> {
|
|
41
|
+
const {
|
|
42
|
+
values: { help, tab, label },
|
|
43
|
+
positionals: [typeId, fieldId],
|
|
44
|
+
} = parseArgs({
|
|
45
|
+
args: process.argv.slice(5), // skip: node, script, "custom-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 custom-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 custom-type add-field geo-point <type-id> <field-id>");
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Find the custom 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 custom 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(`Custom type not found: ${typeId}\n`);
|
|
97
|
+
console.error(`Create it first with: prismic custom-type create ${typeId}`);
|
|
98
|
+
process.exitCode = 1;
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (error instanceof Error) {
|
|
102
|
+
console.error(`Failed to read custom type: ${error.message}`);
|
|
103
|
+
} else {
|
|
104
|
+
console.error("Failed to read custom 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 custom type: ${error.message}`);
|
|
145
|
+
} else {
|
|
146
|
+
console.error("Failed to update custom 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 custom type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic custom-type add-field image <type-id> <field-id> [flags]
|
|
15
|
+
|
|
16
|
+
ARGUMENTS
|
|
17
|
+
type-id Custom 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 custom-type add-field image homepage hero
|
|
28
|
+
prismic custom-type add-field image article thumbnail --tab "Media"
|
|
29
|
+
prismic custom-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 customTypeAddFieldImage(): 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, "custom-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 custom-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 custom-type add-field image <type-id> <field-id>");
|
|
71
|
+
process.exitCode = 1;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Find the custom 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 custom 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(`Custom type not found: ${typeId}\n`);
|
|
99
|
+
console.error(`Create it first with: prismic custom-type create ${typeId}`);
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (error instanceof Error) {
|
|
104
|
+
console.error(`Failed to read custom type: ${error.message}`);
|
|
105
|
+
} else {
|
|
106
|
+
console.error("Failed to read custom 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 custom type: ${error.message}`);
|
|
148
|
+
} else {
|
|
149
|
+
console.error("Failed to update custom 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 custom type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic custom-type add-field key-text <type-id> <field-id> [flags]
|
|
15
|
+
|
|
16
|
+
ARGUMENTS
|
|
17
|
+
type-id Custom 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 custom-type add-field key-text homepage title
|
|
28
|
+
prismic custom-type add-field key-text homepage meta_title --tab "SEO"
|
|
29
|
+
prismic custom-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 customTypeAddFieldKeyText(): 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, "custom-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 custom-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 custom-type add-field key-text <type-id> <field-id>");
|
|
71
|
+
process.exitCode = 1;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Find the custom 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 custom 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(`Custom type not found: ${typeId}\n`);
|
|
99
|
+
console.error(`Create it first with: prismic custom-type create ${typeId}`);
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (error instanceof Error) {
|
|
104
|
+
console.error(`Failed to read custom type: ${error.message}`);
|
|
105
|
+
} else {
|
|
106
|
+
console.error("Failed to read custom 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 custom type: ${error.message}`);
|
|
148
|
+
} else {
|
|
149
|
+
console.error("Failed to update custom 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
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
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 { findUpward } from "./lib/file";
|
|
8
|
+
import { stringify } from "./lib/json";
|
|
9
|
+
|
|
10
|
+
const HELP = `
|
|
11
|
+
Add a link field to an existing custom type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic custom-type add-field link <type-id> <field-id> [flags]
|
|
15
|
+
|
|
16
|
+
ARGUMENTS
|
|
17
|
+
type-id Custom 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
|
+
--variation string Slice variations (can be used multiple times)
|
|
25
|
+
--allow-text Allow text with link
|
|
26
|
+
--allow-target-blank Allow opening link in new tab
|
|
27
|
+
--repeatable Allow multiple links
|
|
28
|
+
-h, --help Show help for command
|
|
29
|
+
|
|
30
|
+
EXAMPLES
|
|
31
|
+
prismic custom-type add-field link homepage button
|
|
32
|
+
prismic custom-type add-field link homepage cta --allow-text
|
|
33
|
+
prismic custom-type add-field link homepage cta --variation Primary --variation Secondary
|
|
34
|
+
prismic custom-type add-field link homepage links --repeatable
|
|
35
|
+
`.trim();
|
|
36
|
+
|
|
37
|
+
const CustomTypeSchema = v.object({
|
|
38
|
+
id: v.string(),
|
|
39
|
+
label: v.string(),
|
|
40
|
+
repeatable: v.boolean(),
|
|
41
|
+
status: v.boolean(),
|
|
42
|
+
format: v.string(),
|
|
43
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
export async function customTypeAddFieldLink(): Promise<void> {
|
|
47
|
+
const {
|
|
48
|
+
values: {
|
|
49
|
+
help,
|
|
50
|
+
tab,
|
|
51
|
+
label,
|
|
52
|
+
placeholder,
|
|
53
|
+
variation,
|
|
54
|
+
"allow-text": allowText,
|
|
55
|
+
"allow-target-blank": allowTargetBlank,
|
|
56
|
+
repeatable,
|
|
57
|
+
},
|
|
58
|
+
positionals: [typeId, fieldId],
|
|
59
|
+
} = parseArgs({
|
|
60
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "link"
|
|
61
|
+
options: {
|
|
62
|
+
tab: { type: "string", short: "t" },
|
|
63
|
+
label: { type: "string", short: "l" },
|
|
64
|
+
placeholder: { type: "string", short: "p" },
|
|
65
|
+
variation: { type: "string", multiple: true },
|
|
66
|
+
"allow-text": { type: "boolean" },
|
|
67
|
+
"allow-target-blank": { type: "boolean" },
|
|
68
|
+
repeatable: { type: "boolean" },
|
|
69
|
+
help: { type: "boolean", short: "h" },
|
|
70
|
+
},
|
|
71
|
+
allowPositionals: true,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (help) {
|
|
75
|
+
console.info(HELP);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!typeId) {
|
|
80
|
+
console.error("Missing required argument: type-id\n");
|
|
81
|
+
console.error("Usage: prismic custom-type add-field link <type-id> <field-id>");
|
|
82
|
+
process.exitCode = 1;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!fieldId) {
|
|
87
|
+
console.error("Missing required argument: field-id\n");
|
|
88
|
+
console.error("Usage: prismic custom-type add-field link <type-id> <field-id>");
|
|
89
|
+
process.exitCode = 1;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Find the custom type file
|
|
94
|
+
const projectRoot = await findUpward("package.json");
|
|
95
|
+
if (!projectRoot) {
|
|
96
|
+
console.error("Could not find project root (no package.json found)");
|
|
97
|
+
process.exitCode = 1;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
102
|
+
|
|
103
|
+
// Read and parse the model
|
|
104
|
+
let model: CustomType;
|
|
105
|
+
try {
|
|
106
|
+
const contents = await readFile(modelPath, "utf8");
|
|
107
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
108
|
+
if (!result.success) {
|
|
109
|
+
console.error(`Invalid custom type model: ${modelPath.href}`);
|
|
110
|
+
process.exitCode = 1;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
model = result.output as CustomType;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
116
|
+
console.error(`Custom type not found: ${typeId}\n`);
|
|
117
|
+
console.error(`Create it first with: prismic custom-type create ${typeId}`);
|
|
118
|
+
process.exitCode = 1;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (error instanceof Error) {
|
|
122
|
+
console.error(`Failed to read custom type: ${error.message}`);
|
|
123
|
+
} else {
|
|
124
|
+
console.error("Failed to read custom type");
|
|
125
|
+
}
|
|
126
|
+
process.exitCode = 1;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Determine target tab
|
|
131
|
+
const existingTabs = Object.keys(model.json);
|
|
132
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
133
|
+
|
|
134
|
+
// Initialize tab if it doesn't exist
|
|
135
|
+
if (!model.json[targetTab]) {
|
|
136
|
+
model.json[targetTab] = {};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Check if field already exists in any tab
|
|
140
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
141
|
+
if (tabFields[fieldId]) {
|
|
142
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
143
|
+
process.exitCode = 1;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Build field definition
|
|
149
|
+
const fieldDefinition: Link = {
|
|
150
|
+
type: "Link",
|
|
151
|
+
config: {
|
|
152
|
+
...(label && { label }),
|
|
153
|
+
...(placeholder && { placeholder }),
|
|
154
|
+
...(variation && variation.length > 0 && { variants: variation }),
|
|
155
|
+
...(allowText && { allowText: true }),
|
|
156
|
+
...(allowTargetBlank && { allowTargetBlank: true }),
|
|
157
|
+
...(repeatable && { repeat: true }),
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// Add field to model
|
|
162
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
163
|
+
|
|
164
|
+
// Write updated model
|
|
165
|
+
try {
|
|
166
|
+
await writeFile(modelPath, stringify(model));
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (error instanceof Error) {
|
|
169
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
170
|
+
} else {
|
|
171
|
+
console.error("Failed to update custom type");
|
|
172
|
+
}
|
|
173
|
+
process.exitCode = 1;
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
console.info(
|
|
178
|
+
`Added field "${fieldId}" (Link) to "${targetTab}" tab in ${typeId}`,
|
|
179
|
+
);
|
|
180
|
+
}
|