@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,155 @@
|
|
|
1
|
+
import type { Image, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
|
|
6
|
+
import { buildTypes } from "./codegen-types";
|
|
7
|
+
import { stringify } from "./lib/json";
|
|
8
|
+
import { findSliceModel } from "./lib/slice";
|
|
9
|
+
import { humanReadable } from "./lib/string";
|
|
10
|
+
|
|
11
|
+
const HELP = `
|
|
12
|
+
Add an image field to an existing slice.
|
|
13
|
+
|
|
14
|
+
USAGE
|
|
15
|
+
prismic slice add-field image <slice-id> <field-id> [flags]
|
|
16
|
+
|
|
17
|
+
ARGUMENTS
|
|
18
|
+
slice-id Slice identifier (required)
|
|
19
|
+
field-id Field identifier (required)
|
|
20
|
+
|
|
21
|
+
Types are generated by default after changes. Use --no-types to skip.
|
|
22
|
+
|
|
23
|
+
FLAGS
|
|
24
|
+
-v, --variation string Target variation (default: first variation)
|
|
25
|
+
-l, --label string Display label for the field (inferred from field-id if omitted)
|
|
26
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
27
|
+
--no-types Skip type generation
|
|
28
|
+
-h, --help Show help for command
|
|
29
|
+
|
|
30
|
+
EXAMPLES
|
|
31
|
+
prismic slice add-field image my_slice background
|
|
32
|
+
prismic slice add-field image hero banner --label "Hero Banner"
|
|
33
|
+
prismic slice add-field image gallery thumbnail --variation "grid"
|
|
34
|
+
`.trim();
|
|
35
|
+
|
|
36
|
+
export async function sliceAddFieldImage(): Promise<void> {
|
|
37
|
+
const {
|
|
38
|
+
values: { help, variation, label, types, "no-types": noTypes },
|
|
39
|
+
positionals: [sliceId, fieldId],
|
|
40
|
+
} = parseArgs({
|
|
41
|
+
args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "image"
|
|
42
|
+
options: {
|
|
43
|
+
variation: { type: "string", short: "v" },
|
|
44
|
+
label: { type: "string", short: "l" },
|
|
45
|
+
types: { type: "string" },
|
|
46
|
+
"no-types": { type: "boolean" },
|
|
47
|
+
help: { type: "boolean", short: "h" },
|
|
48
|
+
},
|
|
49
|
+
allowPositionals: true,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
if (help) {
|
|
53
|
+
console.info(HELP);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!sliceId) {
|
|
58
|
+
console.error("Missing required argument: slice-id\n");
|
|
59
|
+
console.error("Usage: prismic slice add-field image <slice-id> <field-id>");
|
|
60
|
+
process.exitCode = 1;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!fieldId) {
|
|
65
|
+
console.error("Missing required argument: field-id\n");
|
|
66
|
+
console.error("Usage: prismic slice add-field image <slice-id> <field-id>");
|
|
67
|
+
process.exitCode = 1;
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Find the slice model
|
|
72
|
+
const result = await findSliceModel(sliceId);
|
|
73
|
+
if (!result.ok) {
|
|
74
|
+
console.error(result.error);
|
|
75
|
+
process.exitCode = 1;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const { model, modelPath } = result;
|
|
80
|
+
|
|
81
|
+
// Check for variations
|
|
82
|
+
if (model.variations.length === 0) {
|
|
83
|
+
console.error(`Slice "${sliceId}" has no variations.\n`);
|
|
84
|
+
console.error("Add a variation first before adding fields.");
|
|
85
|
+
process.exitCode = 1;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Find target variation
|
|
90
|
+
const targetVariation = variation
|
|
91
|
+
? model.variations.find((v) => v.id === variation)
|
|
92
|
+
: model.variations[0];
|
|
93
|
+
|
|
94
|
+
if (!targetVariation) {
|
|
95
|
+
console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
|
|
96
|
+
console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
|
|
97
|
+
process.exitCode = 1;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Initialize primary if it doesn't exist
|
|
102
|
+
if (!targetVariation.primary) {
|
|
103
|
+
targetVariation.primary = {};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Check if field already exists in any variation
|
|
107
|
+
for (const v of model.variations) {
|
|
108
|
+
if (v.primary?.[fieldId]) {
|
|
109
|
+
console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
|
|
110
|
+
process.exitCode = 1;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Build field definition
|
|
116
|
+
const fieldDefinition: Image = {
|
|
117
|
+
type: "Image",
|
|
118
|
+
config: {
|
|
119
|
+
label: label ?? humanReadable(fieldId),
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// Add field to variation
|
|
124
|
+
targetVariation.primary[fieldId] = fieldDefinition;
|
|
125
|
+
|
|
126
|
+
// Write updated model
|
|
127
|
+
try {
|
|
128
|
+
await writeFile(modelPath, stringify(model as SharedSlice));
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (error instanceof Error) {
|
|
131
|
+
console.error(`Failed to update slice: ${error.message}`);
|
|
132
|
+
} else {
|
|
133
|
+
console.error("Failed to update slice");
|
|
134
|
+
}
|
|
135
|
+
process.exitCode = 1;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
console.info(
|
|
140
|
+
`Added field "${fieldId}" (Image) to "${targetVariation.id}" variation in ${sliceId}`,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
if (!noTypes) {
|
|
144
|
+
try {
|
|
145
|
+
await buildTypes({ output: types });
|
|
146
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
console.info();
|
|
153
|
+
console.info("Next: Add more fields with `prismic slice add-field`");
|
|
154
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
155
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { SharedSlice, Text } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
|
|
6
|
+
import { buildTypes } from "./codegen-types";
|
|
7
|
+
import { stringify } from "./lib/json";
|
|
8
|
+
import { findSliceModel } from "./lib/slice";
|
|
9
|
+
import { humanReadable } from "./lib/string";
|
|
10
|
+
|
|
11
|
+
const HELP = `
|
|
12
|
+
Add a key-text (single-line text) field to an existing slice.
|
|
13
|
+
|
|
14
|
+
USAGE
|
|
15
|
+
prismic slice add-field key-text <slice-id> <field-id> [flags]
|
|
16
|
+
|
|
17
|
+
ARGUMENTS
|
|
18
|
+
slice-id Slice identifier (required)
|
|
19
|
+
field-id Field identifier (required)
|
|
20
|
+
|
|
21
|
+
Types are generated by default after changes. Use --no-types to skip.
|
|
22
|
+
|
|
23
|
+
FLAGS
|
|
24
|
+
-v, --variation string Target variation (default: first variation)
|
|
25
|
+
-l, --label string Display label for the field (inferred from field-id if omitted)
|
|
26
|
+
-p, --placeholder string Placeholder text
|
|
27
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
28
|
+
--no-types Skip type generation
|
|
29
|
+
-h, --help Show help for command
|
|
30
|
+
|
|
31
|
+
EXAMPLES
|
|
32
|
+
prismic slice add-field key-text my_slice title
|
|
33
|
+
prismic slice add-field key-text hero heading --label "Heading"
|
|
34
|
+
prismic slice add-field key-text cta button_text --placeholder "Enter button text"
|
|
35
|
+
`.trim();
|
|
36
|
+
|
|
37
|
+
export async function sliceAddFieldKeyText(): Promise<void> {
|
|
38
|
+
const {
|
|
39
|
+
values: { help, variation, label, placeholder, types, "no-types": noTypes },
|
|
40
|
+
positionals: [sliceId, fieldId],
|
|
41
|
+
} = parseArgs({
|
|
42
|
+
args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "key-text"
|
|
43
|
+
options: {
|
|
44
|
+
variation: { type: "string", short: "v" },
|
|
45
|
+
label: { type: "string", short: "l" },
|
|
46
|
+
placeholder: { type: "string", short: "p" },
|
|
47
|
+
types: { type: "string" },
|
|
48
|
+
"no-types": { type: "boolean" },
|
|
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 (!sliceId) {
|
|
60
|
+
console.error("Missing required argument: slice-id\n");
|
|
61
|
+
console.error("Usage: prismic slice add-field key-text <slice-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 slice add-field key-text <slice-id> <field-id>");
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Find the slice model
|
|
74
|
+
const result = await findSliceModel(sliceId);
|
|
75
|
+
if (!result.ok) {
|
|
76
|
+
console.error(result.error);
|
|
77
|
+
process.exitCode = 1;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const { model, modelPath } = result;
|
|
82
|
+
|
|
83
|
+
// Check for variations
|
|
84
|
+
if (model.variations.length === 0) {
|
|
85
|
+
console.error(`Slice "${sliceId}" has no variations.\n`);
|
|
86
|
+
console.error("Add a variation first before adding fields.");
|
|
87
|
+
process.exitCode = 1;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Find target variation
|
|
92
|
+
const targetVariation = variation
|
|
93
|
+
? model.variations.find((v) => v.id === variation)
|
|
94
|
+
: model.variations[0];
|
|
95
|
+
|
|
96
|
+
if (!targetVariation) {
|
|
97
|
+
console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
|
|
98
|
+
console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
|
|
99
|
+
process.exitCode = 1;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Initialize primary if it doesn't exist
|
|
104
|
+
if (!targetVariation.primary) {
|
|
105
|
+
targetVariation.primary = {};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Check if field already exists in any variation
|
|
109
|
+
for (const v of model.variations) {
|
|
110
|
+
if (v.primary?.[fieldId]) {
|
|
111
|
+
console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
|
|
112
|
+
process.exitCode = 1;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Build field definition
|
|
118
|
+
const fieldDefinition: Text = {
|
|
119
|
+
type: "Text",
|
|
120
|
+
config: {
|
|
121
|
+
label: label ?? humanReadable(fieldId),
|
|
122
|
+
...(placeholder && { placeholder }),
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// Add field to variation
|
|
127
|
+
targetVariation.primary[fieldId] = fieldDefinition;
|
|
128
|
+
|
|
129
|
+
// Write updated model
|
|
130
|
+
try {
|
|
131
|
+
await writeFile(modelPath, stringify(model as SharedSlice));
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (error instanceof Error) {
|
|
134
|
+
console.error(`Failed to update slice: ${error.message}`);
|
|
135
|
+
} else {
|
|
136
|
+
console.error("Failed to update slice");
|
|
137
|
+
}
|
|
138
|
+
process.exitCode = 1;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
console.info(
|
|
143
|
+
`Added field "${fieldId}" (Text) to "${targetVariation.id}" variation in ${sliceId}`,
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
if (!noTypes) {
|
|
147
|
+
try {
|
|
148
|
+
await buildTypes({ output: types });
|
|
149
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
console.info();
|
|
156
|
+
console.info("Next: Add more fields with `prismic slice add-field`");
|
|
157
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
158
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { Link, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
|
|
6
|
+
import { buildTypes } from "./codegen-types";
|
|
7
|
+
import { stringify } from "./lib/json";
|
|
8
|
+
import { findSliceModel } from "./lib/slice";
|
|
9
|
+
import { humanReadable } from "./lib/string";
|
|
10
|
+
|
|
11
|
+
const HELP = `
|
|
12
|
+
Add a link field to an existing slice.
|
|
13
|
+
|
|
14
|
+
USAGE
|
|
15
|
+
prismic slice add-field link <slice-id> <field-id> [flags]
|
|
16
|
+
|
|
17
|
+
ARGUMENTS
|
|
18
|
+
slice-id Slice identifier (required)
|
|
19
|
+
field-id Field identifier (required)
|
|
20
|
+
|
|
21
|
+
Types are generated by default after changes. Use --no-types to skip.
|
|
22
|
+
|
|
23
|
+
FLAGS
|
|
24
|
+
-v, --variation string Target variation (default: first variation)
|
|
25
|
+
-l, --label string Display label for the field (inferred from field-id if omitted)
|
|
26
|
+
-p, --placeholder string Placeholder text
|
|
27
|
+
--allow-text Allow text with link
|
|
28
|
+
--allow-target-blank Allow opening link in new tab
|
|
29
|
+
--repeatable Allow multiple links
|
|
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 slice add-field link my_slice button
|
|
36
|
+
prismic slice add-field link cta primary_link --allow-text
|
|
37
|
+
prismic slice add-field link navigation links --repeatable
|
|
38
|
+
prismic slice add-field link hero cta --allow-text --allow-target-blank
|
|
39
|
+
`.trim();
|
|
40
|
+
|
|
41
|
+
export async function sliceAddFieldLink(): Promise<void> {
|
|
42
|
+
const {
|
|
43
|
+
values: {
|
|
44
|
+
help,
|
|
45
|
+
variation,
|
|
46
|
+
label,
|
|
47
|
+
placeholder,
|
|
48
|
+
"allow-text": allowText,
|
|
49
|
+
"allow-target-blank": allowTargetBlank,
|
|
50
|
+
repeatable,
|
|
51
|
+
types,
|
|
52
|
+
"no-types": noTypes,
|
|
53
|
+
},
|
|
54
|
+
positionals: [sliceId, fieldId],
|
|
55
|
+
} = parseArgs({
|
|
56
|
+
args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "link"
|
|
57
|
+
options: {
|
|
58
|
+
variation: { type: "string", short: "v" },
|
|
59
|
+
label: { type: "string", short: "l" },
|
|
60
|
+
placeholder: { type: "string", short: "p" },
|
|
61
|
+
"allow-text": { type: "boolean" },
|
|
62
|
+
"allow-target-blank": { type: "boolean" },
|
|
63
|
+
repeatable: { type: "boolean" },
|
|
64
|
+
types: { type: "string" },
|
|
65
|
+
"no-types": { type: "boolean" },
|
|
66
|
+
help: { type: "boolean", short: "h" },
|
|
67
|
+
},
|
|
68
|
+
allowPositionals: true,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (help) {
|
|
72
|
+
console.info(HELP);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!sliceId) {
|
|
77
|
+
console.error("Missing required argument: slice-id\n");
|
|
78
|
+
console.error("Usage: prismic slice add-field link <slice-id> <field-id>");
|
|
79
|
+
process.exitCode = 1;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!fieldId) {
|
|
84
|
+
console.error("Missing required argument: field-id\n");
|
|
85
|
+
console.error("Usage: prismic slice add-field link <slice-id> <field-id>");
|
|
86
|
+
process.exitCode = 1;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Find the slice model
|
|
91
|
+
const result = await findSliceModel(sliceId);
|
|
92
|
+
if (!result.ok) {
|
|
93
|
+
console.error(result.error);
|
|
94
|
+
process.exitCode = 1;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const { model, modelPath } = result;
|
|
99
|
+
|
|
100
|
+
// Check for variations
|
|
101
|
+
if (model.variations.length === 0) {
|
|
102
|
+
console.error(`Slice "${sliceId}" has no variations.\n`);
|
|
103
|
+
console.error("Add a variation first before adding fields.");
|
|
104
|
+
process.exitCode = 1;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Find target variation
|
|
109
|
+
const targetVariation = variation
|
|
110
|
+
? model.variations.find((v) => v.id === variation)
|
|
111
|
+
: model.variations[0];
|
|
112
|
+
|
|
113
|
+
if (!targetVariation) {
|
|
114
|
+
console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
|
|
115
|
+
console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
|
|
116
|
+
process.exitCode = 1;
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Initialize primary if it doesn't exist
|
|
121
|
+
if (!targetVariation.primary) {
|
|
122
|
+
targetVariation.primary = {};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Check if field already exists in any variation
|
|
126
|
+
for (const v of model.variations) {
|
|
127
|
+
if (v.primary?.[fieldId]) {
|
|
128
|
+
console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
|
|
129
|
+
process.exitCode = 1;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Build field definition
|
|
135
|
+
const fieldDefinition: Link = {
|
|
136
|
+
type: "Link",
|
|
137
|
+
config: {
|
|
138
|
+
label: label ?? humanReadable(fieldId),
|
|
139
|
+
...(placeholder && { placeholder }),
|
|
140
|
+
...(allowText && { allowText: true }),
|
|
141
|
+
...(allowTargetBlank && { allowTargetBlank: true }),
|
|
142
|
+
...(repeatable && { repeat: true }),
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Add field to variation
|
|
147
|
+
targetVariation.primary[fieldId] = fieldDefinition;
|
|
148
|
+
|
|
149
|
+
// Write updated model
|
|
150
|
+
try {
|
|
151
|
+
await writeFile(modelPath, stringify(model as SharedSlice));
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (error instanceof Error) {
|
|
154
|
+
console.error(`Failed to update slice: ${error.message}`);
|
|
155
|
+
} else {
|
|
156
|
+
console.error("Failed to update slice");
|
|
157
|
+
}
|
|
158
|
+
process.exitCode = 1;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
console.info(
|
|
163
|
+
`Added field "${fieldId}" (Link) to "${targetVariation.id}" variation in ${sliceId}`,
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
if (!noTypes) {
|
|
167
|
+
try {
|
|
168
|
+
await buildTypes({ output: types });
|
|
169
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
console.info();
|
|
176
|
+
console.info("Next: Add more fields with `prismic slice add-field`");
|
|
177
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
178
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { Number, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
|
|
6
|
+
import { buildTypes } from "./codegen-types";
|
|
7
|
+
import { stringify } from "./lib/json";
|
|
8
|
+
import { findSliceModel } from "./lib/slice";
|
|
9
|
+
import { humanReadable } from "./lib/string";
|
|
10
|
+
|
|
11
|
+
const HELP = `
|
|
12
|
+
Add a number field to an existing slice.
|
|
13
|
+
|
|
14
|
+
USAGE
|
|
15
|
+
prismic slice add-field number <slice-id> <field-id> [flags]
|
|
16
|
+
|
|
17
|
+
ARGUMENTS
|
|
18
|
+
slice-id Slice identifier (required)
|
|
19
|
+
field-id Field identifier (required)
|
|
20
|
+
|
|
21
|
+
Types are generated by default after changes. Use --no-types to skip.
|
|
22
|
+
|
|
23
|
+
FLAGS
|
|
24
|
+
-v, --variation string Target variation (default: first variation)
|
|
25
|
+
-l, --label string Display label for the field (inferred from field-id if omitted)
|
|
26
|
+
-p, --placeholder string Placeholder text
|
|
27
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
28
|
+
--no-types Skip type generation
|
|
29
|
+
-h, --help Show help for command
|
|
30
|
+
|
|
31
|
+
EXAMPLES
|
|
32
|
+
prismic slice add-field number my_slice price
|
|
33
|
+
prismic slice add-field number product quantity --label "Quantity"
|
|
34
|
+
prismic slice add-field number stats count --variation "detailed"
|
|
35
|
+
`.trim();
|
|
36
|
+
|
|
37
|
+
export async function sliceAddFieldNumber(): Promise<void> {
|
|
38
|
+
const {
|
|
39
|
+
values: { help, variation, label, placeholder, types, "no-types": noTypes },
|
|
40
|
+
positionals: [sliceId, fieldId],
|
|
41
|
+
} = parseArgs({
|
|
42
|
+
args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "number"
|
|
43
|
+
options: {
|
|
44
|
+
variation: { type: "string", short: "v" },
|
|
45
|
+
label: { type: "string", short: "l" },
|
|
46
|
+
placeholder: { type: "string", short: "p" },
|
|
47
|
+
types: { type: "string" },
|
|
48
|
+
"no-types": { type: "boolean" },
|
|
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 (!sliceId) {
|
|
60
|
+
console.error("Missing required argument: slice-id\n");
|
|
61
|
+
console.error("Usage: prismic slice add-field number <slice-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 slice add-field number <slice-id> <field-id>");
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Find the slice model
|
|
74
|
+
const result = await findSliceModel(sliceId);
|
|
75
|
+
if (!result.ok) {
|
|
76
|
+
console.error(result.error);
|
|
77
|
+
process.exitCode = 1;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const { model, modelPath } = result;
|
|
82
|
+
|
|
83
|
+
// Check for variations
|
|
84
|
+
if (model.variations.length === 0) {
|
|
85
|
+
console.error(`Slice "${sliceId}" has no variations.\n`);
|
|
86
|
+
console.error("Add a variation first before adding fields.");
|
|
87
|
+
process.exitCode = 1;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Find target variation
|
|
92
|
+
const targetVariation = variation
|
|
93
|
+
? model.variations.find((v) => v.id === variation)
|
|
94
|
+
: model.variations[0];
|
|
95
|
+
|
|
96
|
+
if (!targetVariation) {
|
|
97
|
+
console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
|
|
98
|
+
console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
|
|
99
|
+
process.exitCode = 1;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Initialize primary if it doesn't exist
|
|
104
|
+
if (!targetVariation.primary) {
|
|
105
|
+
targetVariation.primary = {};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Check if field already exists in any variation
|
|
109
|
+
for (const v of model.variations) {
|
|
110
|
+
if (v.primary?.[fieldId]) {
|
|
111
|
+
console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
|
|
112
|
+
process.exitCode = 1;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Build field definition
|
|
118
|
+
const fieldDefinition: Number = {
|
|
119
|
+
type: "Number",
|
|
120
|
+
config: {
|
|
121
|
+
label: label ?? humanReadable(fieldId),
|
|
122
|
+
...(placeholder && { placeholder }),
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// Add field to variation
|
|
127
|
+
targetVariation.primary[fieldId] = fieldDefinition;
|
|
128
|
+
|
|
129
|
+
// Write updated model
|
|
130
|
+
try {
|
|
131
|
+
await writeFile(modelPath, stringify(model as SharedSlice));
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (error instanceof Error) {
|
|
134
|
+
console.error(`Failed to update slice: ${error.message}`);
|
|
135
|
+
} else {
|
|
136
|
+
console.error("Failed to update slice");
|
|
137
|
+
}
|
|
138
|
+
process.exitCode = 1;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
console.info(
|
|
143
|
+
`Added field "${fieldId}" (Number) to "${targetVariation.id}" variation in ${sliceId}`,
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
if (!noTypes) {
|
|
147
|
+
try {
|
|
148
|
+
await buildTypes({ output: types });
|
|
149
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
console.info();
|
|
156
|
+
console.info("Next: Add more fields with `prismic slice add-field`");
|
|
157
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
158
|
+
}
|