@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.
Files changed (119) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +1996 -0
  4. package/package.json +52 -0
  5. package/src/custom-type-add-field-boolean.ts +171 -0
  6. package/src/custom-type-add-field-color.ts +158 -0
  7. package/src/custom-type-add-field-date.ts +161 -0
  8. package/src/custom-type-add-field-embed.ts +158 -0
  9. package/src/custom-type-add-field-geo-point.ts +155 -0
  10. package/src/custom-type-add-field-image.ts +158 -0
  11. package/src/custom-type-add-field-key-text.ts +158 -0
  12. package/src/custom-type-add-field-link.ts +180 -0
  13. package/src/custom-type-add-field-number.ts +190 -0
  14. package/src/custom-type-add-field-rich-text.ts +181 -0
  15. package/src/custom-type-add-field-select.ts +164 -0
  16. package/src/custom-type-add-field-timestamp.ts +161 -0
  17. package/src/custom-type-add-field-uid.ts +158 -0
  18. package/src/custom-type-add-field.ts +111 -0
  19. package/src/custom-type-connect-slice.ts +221 -0
  20. package/src/custom-type-create.ts +92 -0
  21. package/src/custom-type-disconnect-slice.ts +179 -0
  22. package/src/custom-type-list.ts +110 -0
  23. package/src/custom-type-remove-field.ts +161 -0
  24. package/src/custom-type-remove.ts +126 -0
  25. package/src/custom-type-set-name.ts +128 -0
  26. package/src/custom-type-view.ts +118 -0
  27. package/src/custom-type.ts +85 -0
  28. package/src/index.ts +100 -0
  29. package/src/init.ts +62 -0
  30. package/src/lib/auth.ts +60 -0
  31. package/src/lib/config.ts +111 -0
  32. package/src/lib/file.ts +49 -0
  33. package/src/lib/json.ts +3 -0
  34. package/src/lib/request.ts +116 -0
  35. package/src/lib/slice.ts +112 -0
  36. package/src/lib/url.ts +25 -0
  37. package/src/locale-add.ts +116 -0
  38. package/src/locale-list.ts +107 -0
  39. package/src/locale-remove.ts +88 -0
  40. package/src/locale-set-default.ts +131 -0
  41. package/src/locale.ts +60 -0
  42. package/src/login.ts +143 -0
  43. package/src/logout.ts +36 -0
  44. package/src/page-type-add-field-boolean.ts +171 -0
  45. package/src/page-type-add-field-color.ts +158 -0
  46. package/src/page-type-add-field-date.ts +161 -0
  47. package/src/page-type-add-field-embed.ts +158 -0
  48. package/src/page-type-add-field-geo-point.ts +155 -0
  49. package/src/page-type-add-field-image.ts +158 -0
  50. package/src/page-type-add-field-key-text.ts +158 -0
  51. package/src/page-type-add-field-link.ts +180 -0
  52. package/src/page-type-add-field-number.ts +190 -0
  53. package/src/page-type-add-field-rich-text.ts +181 -0
  54. package/src/page-type-add-field-select.ts +164 -0
  55. package/src/page-type-add-field-timestamp.ts +161 -0
  56. package/src/page-type-add-field-uid.ts +158 -0
  57. package/src/page-type-add-field.ts +111 -0
  58. package/src/page-type-connect-slice.ts +221 -0
  59. package/src/page-type-create.ts +93 -0
  60. package/src/page-type-disconnect-slice.ts +179 -0
  61. package/src/page-type-list.ts +109 -0
  62. package/src/page-type-remove-field.ts +161 -0
  63. package/src/page-type-remove.ts +126 -0
  64. package/src/page-type-set-name.ts +128 -0
  65. package/src/page-type-set-repeatable.ts +137 -0
  66. package/src/page-type-view.ts +118 -0
  67. package/src/page-type.ts +90 -0
  68. package/src/preview-add.ts +126 -0
  69. package/src/preview-list.ts +106 -0
  70. package/src/preview-remove.ts +109 -0
  71. package/src/preview-set-name.ts +137 -0
  72. package/src/preview.ts +60 -0
  73. package/src/repo-create.ts +136 -0
  74. package/src/repo-list.ts +100 -0
  75. package/src/repo-set-name.ts +102 -0
  76. package/src/repo-view.ts +113 -0
  77. package/src/repo.ts +60 -0
  78. package/src/slice-add-field-boolean.ts +150 -0
  79. package/src/slice-add-field-color.ts +137 -0
  80. package/src/slice-add-field-date.ts +137 -0
  81. package/src/slice-add-field-embed.ts +137 -0
  82. package/src/slice-add-field-geo-point.ts +134 -0
  83. package/src/slice-add-field-image.ts +134 -0
  84. package/src/slice-add-field-key-text.ts +137 -0
  85. package/src/slice-add-field-link.ts +155 -0
  86. package/src/slice-add-field-number.ts +137 -0
  87. package/src/slice-add-field-rich-text.ts +160 -0
  88. package/src/slice-add-field-select.ts +143 -0
  89. package/src/slice-add-field-timestamp.ts +137 -0
  90. package/src/slice-add-field.ts +106 -0
  91. package/src/slice-add-variation.ts +137 -0
  92. package/src/slice-create.ts +129 -0
  93. package/src/slice-list-variations.ts +67 -0
  94. package/src/slice-list.ts +88 -0
  95. package/src/slice-remove-field.ts +117 -0
  96. package/src/slice-remove-variation.ts +108 -0
  97. package/src/slice-remove.ts +81 -0
  98. package/src/slice-rename.ts +112 -0
  99. package/src/slice-view.ts +77 -0
  100. package/src/slice.ts +90 -0
  101. package/src/sync.ts +309 -0
  102. package/src/token-create.ts +185 -0
  103. package/src/token-delete.ts +161 -0
  104. package/src/token-list.ts +212 -0
  105. package/src/token-set-name.ts +165 -0
  106. package/src/token.ts +60 -0
  107. package/src/webhook-add-header.ts +118 -0
  108. package/src/webhook-create.ts +152 -0
  109. package/src/webhook-disable.ts +109 -0
  110. package/src/webhook-enable.ts +132 -0
  111. package/src/webhook-list.ts +93 -0
  112. package/src/webhook-remove-header.ts +117 -0
  113. package/src/webhook-remove.ts +106 -0
  114. package/src/webhook-set-triggers.ts +148 -0
  115. package/src/webhook-status.ts +90 -0
  116. package/src/webhook-test.ts +106 -0
  117. package/src/webhook-view.ts +147 -0
  118. package/src/webhook.ts +95 -0
  119. package/src/whoami.ts +62 -0
package/src/login.ts ADDED
@@ -0,0 +1,143 @@
1
+ import { exec } from "node:child_process";
2
+ import { createServer } from "node:http";
3
+ import { parseArgs } from "node:util";
4
+
5
+ import { saveToken } from "./lib/auth";
6
+
7
+ const LOGIN_TIMEOUT_MS = 3 * 60 * 1000; // 3 minutes
8
+
9
+ const HELP = `
10
+ Log in to Prismic via browser.
11
+
12
+ USAGE
13
+ prismic login [flags]
14
+
15
+ FLAGS
16
+ -h, --help Show help for command
17
+
18
+ LEARN MORE
19
+ Use \`prismic <command> --help\` for more information about a command.
20
+ `.trim();
21
+
22
+ export async function login(): Promise<void> {
23
+ const { values } = parseArgs({
24
+ args: process.argv.slice(3),
25
+ options: { help: { type: "boolean", short: "h" } },
26
+ });
27
+
28
+ if (values.help) {
29
+ console.info(HELP);
30
+ return;
31
+ }
32
+
33
+ return new Promise((resolve, reject) => {
34
+ const server = createServer((req, res) => {
35
+ // Handle CORS preflight
36
+ if (req.method === "OPTIONS") {
37
+ res.writeHead(204, {
38
+ "Access-Control-Allow-Origin": "*",
39
+ "Access-Control-Allow-Methods": "POST, OPTIONS",
40
+ "Access-Control-Allow-Headers": "Content-Type",
41
+ });
42
+ res.end();
43
+ return;
44
+ }
45
+
46
+ // Handle POST with credentials
47
+ if (req.method === "POST") {
48
+ let body = "";
49
+
50
+ req.on("data", (chunk) => {
51
+ body += chunk.toString();
52
+ });
53
+
54
+ req.on("end", async () => {
55
+ try {
56
+ const { cookies, email } = JSON.parse(body);
57
+
58
+ const cookie: string | undefined = cookies.find((c: string) =>
59
+ c.startsWith("prismic-auth="),
60
+ );
61
+ const token = cookie?.split(";")[0]?.replace(/^prismic-auth=/, "");
62
+
63
+ if (!token) {
64
+ res.writeHead(400, {
65
+ "Access-Control-Allow-Origin": "*",
66
+ "Content-Type": "application/json",
67
+ });
68
+ res.end(JSON.stringify({ error: "Invalid request" }));
69
+ return;
70
+ }
71
+
72
+ await saveToken(token);
73
+
74
+ console.info(`Logged in to Prismic as ${email}`);
75
+
76
+ res.writeHead(200, {
77
+ "Access-Control-Allow-Origin": "*",
78
+ "Content-Type": "application/json",
79
+ });
80
+ res.end(JSON.stringify({ success: true }));
81
+
82
+ clearTimeout(timeoutId);
83
+ server.close();
84
+ resolve();
85
+ } catch {
86
+ res.writeHead(400, {
87
+ "Access-Control-Allow-Origin": "*",
88
+ "Content-Type": "application/json",
89
+ });
90
+ res.end(JSON.stringify({ error: "Invalid request" }));
91
+ }
92
+ });
93
+
94
+ return;
95
+ }
96
+
97
+ // Handle other requests
98
+ res.writeHead(404);
99
+ res.end();
100
+ });
101
+
102
+ const timeoutId = setTimeout(() => {
103
+ server.close();
104
+ reject(new Error("Login timed out. Please try again."));
105
+ }, LOGIN_TIMEOUT_MS);
106
+
107
+ server.listen(0, "127.0.0.1", () => {
108
+ const address = server.address();
109
+ if (!address || typeof address === "string") {
110
+ clearTimeout(timeoutId);
111
+ server.close();
112
+ reject(new Error("Failed to start login server"));
113
+ return;
114
+ }
115
+
116
+ const port = address.port;
117
+ const loginUrl = buildLoginUrl(port);
118
+
119
+ console.info(`Opening browser to complete login...`);
120
+ console.info(`If the browser doesn't open, visit: ${loginUrl}`);
121
+
122
+ openBrowser(loginUrl);
123
+ });
124
+
125
+ server.on("error", (error) => {
126
+ clearTimeout(timeoutId);
127
+ reject(error);
128
+ });
129
+ });
130
+ }
131
+
132
+ function buildLoginUrl(port: number): URL {
133
+ const url = new URL(`https://prismic.io/dashboard/cli/login`);
134
+ url.searchParams.set("source", "slice-machine");
135
+ url.searchParams.set("port", port.toString());
136
+ return url;
137
+ }
138
+
139
+ function openBrowser(url: URL): void {
140
+ const cmd =
141
+ process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
142
+ exec(`${cmd} "${url.toString()}"`);
143
+ }
package/src/logout.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { removeToken } from "./lib/auth";
4
+
5
+ const HELP = `
6
+ Log out of Prismic.
7
+
8
+ USAGE
9
+ prismic logout [flags]
10
+
11
+ FLAGS
12
+ -h, --help Show help for command
13
+
14
+ LEARN MORE
15
+ Use \`prismic <command> --help\` for more information about a command.
16
+ `.trim();
17
+
18
+ export async function logout(): Promise<void> {
19
+ const { values } = parseArgs({
20
+ args: process.argv.slice(3),
21
+ options: { help: { type: "boolean", short: "h" } },
22
+ });
23
+
24
+ if (values.help) {
25
+ console.info(HELP);
26
+ return;
27
+ }
28
+
29
+ const ok = await removeToken();
30
+ if (ok) {
31
+ console.info("Logged out of Prismic");
32
+ } else {
33
+ console.error("Logout failed. You can log out manually by deleting the file.");
34
+ process.exitCode = 1;
35
+ }
36
+ }
@@ -0,0 +1,171 @@
1
+ import type { BooleanField, CustomType } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { readFile, writeFile } from "node:fs/promises";
4
+ import { parseArgs } from "node:util";
5
+ import * as v from "valibot";
6
+
7
+ import { findUpward } from "./lib/file";
8
+ import { stringify } from "./lib/json";
9
+
10
+ const HELP = `
11
+ Add a boolean (toggle) field to an existing page type.
12
+
13
+ USAGE
14
+ prismic page-type add-field boolean <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
+ --default Set default value to true
24
+ --true-label string Label shown when toggle is on
25
+ --false-label string Label shown when toggle is off
26
+ -h, --help Show help for command
27
+
28
+ EXAMPLES
29
+ prismic page-type add-field boolean homepage featured
30
+ prismic page-type add-field boolean article published --default
31
+ prismic page-type add-field boolean product available --true-label "In Stock" --false-label "Out of Stock"
32
+ `.trim();
33
+
34
+ const CustomTypeSchema = v.object({
35
+ id: v.string(),
36
+ label: v.string(),
37
+ repeatable: v.boolean(),
38
+ status: v.boolean(),
39
+ format: v.string(),
40
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
41
+ });
42
+
43
+ export async function pageTypeAddFieldBoolean(): Promise<void> {
44
+ const {
45
+ values: {
46
+ help,
47
+ tab,
48
+ label,
49
+ default: defaultValue,
50
+ "true-label": trueLabel,
51
+ "false-label": falseLabel,
52
+ },
53
+ positionals: [typeId, fieldId],
54
+ } = parseArgs({
55
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "boolean"
56
+ options: {
57
+ tab: { type: "string", short: "t" },
58
+ label: { type: "string", short: "l" },
59
+ default: { type: "boolean" },
60
+ "true-label": { type: "string" },
61
+ "false-label": { type: "string" },
62
+ help: { type: "boolean", short: "h" },
63
+ },
64
+ allowPositionals: true,
65
+ });
66
+
67
+ if (help) {
68
+ console.info(HELP);
69
+ return;
70
+ }
71
+
72
+ if (!typeId) {
73
+ console.error("Missing required argument: type-id\n");
74
+ console.error("Usage: prismic page-type add-field boolean <type-id> <field-id>");
75
+ process.exitCode = 1;
76
+ return;
77
+ }
78
+
79
+ if (!fieldId) {
80
+ console.error("Missing required argument: field-id\n");
81
+ console.error("Usage: prismic page-type add-field boolean <type-id> <field-id>");
82
+ process.exitCode = 1;
83
+ return;
84
+ }
85
+
86
+ // Find the page type file
87
+ const projectRoot = await findUpward("package.json");
88
+ if (!projectRoot) {
89
+ console.error("Could not find project root (no package.json found)");
90
+ process.exitCode = 1;
91
+ return;
92
+ }
93
+
94
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
95
+
96
+ // Read and parse the model
97
+ let model: CustomType;
98
+ try {
99
+ const contents = await readFile(modelPath, "utf8");
100
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
101
+ if (!result.success) {
102
+ console.error(`Invalid page type model: ${modelPath.href}`);
103
+ process.exitCode = 1;
104
+ return;
105
+ }
106
+ model = result.output as CustomType;
107
+ } catch (error) {
108
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
109
+ console.error(`Page type not found: ${typeId}\n`);
110
+ console.error(`Create it first with: prismic page-type create ${typeId}`);
111
+ process.exitCode = 1;
112
+ return;
113
+ }
114
+ if (error instanceof Error) {
115
+ console.error(`Failed to read page type: ${error.message}`);
116
+ } else {
117
+ console.error("Failed to read page type");
118
+ }
119
+ process.exitCode = 1;
120
+ return;
121
+ }
122
+
123
+ // Determine target tab
124
+ const existingTabs = Object.keys(model.json);
125
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
126
+
127
+ // Initialize tab if it doesn't exist
128
+ if (!model.json[targetTab]) {
129
+ model.json[targetTab] = {};
130
+ }
131
+
132
+ // Check if field already exists in any tab
133
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
134
+ if (tabFields[fieldId]) {
135
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
136
+ process.exitCode = 1;
137
+ return;
138
+ }
139
+ }
140
+
141
+ // Build field definition
142
+ const fieldDefinition: BooleanField = {
143
+ type: "Boolean",
144
+ config: {
145
+ ...(label && { label }),
146
+ ...(defaultValue && { default_value: true }),
147
+ ...(trueLabel && { placeholder_true: trueLabel }),
148
+ ...(falseLabel && { placeholder_false: falseLabel }),
149
+ },
150
+ };
151
+
152
+ // Add field to model
153
+ model.json[targetTab][fieldId] = fieldDefinition;
154
+
155
+ // Write updated model
156
+ try {
157
+ await writeFile(modelPath, stringify(model));
158
+ } catch (error) {
159
+ if (error instanceof Error) {
160
+ console.error(`Failed to update page type: ${error.message}`);
161
+ } else {
162
+ console.error("Failed to update page type");
163
+ }
164
+ process.exitCode = 1;
165
+ return;
166
+ }
167
+
168
+ console.info(
169
+ `Added field "${fieldId}" (Boolean) to "${targetTab}" tab in ${typeId}`,
170
+ );
171
+ }
@@ -0,0 +1,158 @@
1
+ import type { Color, CustomType } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { readFile, writeFile } from "node:fs/promises";
4
+ import { parseArgs } from "node:util";
5
+ import * as v from "valibot";
6
+
7
+ import { findUpward } from "./lib/file";
8
+ import { stringify } from "./lib/json";
9
+
10
+ const HELP = `
11
+ Add a color picker field to an existing page type.
12
+
13
+ USAGE
14
+ prismic page-type add-field color <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 color homepage bg_color
28
+ prismic page-type add-field color homepage accent --tab "Design"
29
+ prismic page-type add-field color homepage text_color --label "Text Color"
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 pageTypeAddFieldColor(): 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", "color"
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 color <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 color <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: Color = {
132
+ type: "Color",
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}" (Color) to "${targetTab}" tab in ${typeId}`,
157
+ );
158
+ }
@@ -0,0 +1,161 @@
1
+ import type { CustomType, Date as DateField } 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 date field to an existing page type.
12
+
13
+ USAGE
14
+ prismic page-type add-field date <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
+ --default string Default date value (YYYY-MM-DD format)
25
+ -h, --help Show help for command
26
+
27
+ EXAMPLES
28
+ prismic page-type add-field date homepage publish_date
29
+ prismic page-type add-field date event start_date --tab "Schedule"
30
+ prismic page-type add-field date article date --label "Publication Date" --default "2024-01-01"
31
+ `.trim();
32
+
33
+ const CustomTypeSchema = v.object({
34
+ id: v.string(),
35
+ label: v.string(),
36
+ repeatable: v.boolean(),
37
+ status: v.boolean(),
38
+ format: v.string(),
39
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
40
+ });
41
+
42
+ export async function pageTypeAddFieldDate(): Promise<void> {
43
+ const {
44
+ values: { help, tab, label, placeholder, default: defaultValue },
45
+ positionals: [typeId, fieldId],
46
+ } = parseArgs({
47
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "date"
48
+ options: {
49
+ tab: { type: "string", short: "t" },
50
+ label: { type: "string", short: "l" },
51
+ placeholder: { type: "string", short: "p" },
52
+ default: { type: "string" },
53
+ help: { type: "boolean", short: "h" },
54
+ },
55
+ allowPositionals: true,
56
+ });
57
+
58
+ if (help) {
59
+ console.info(HELP);
60
+ return;
61
+ }
62
+
63
+ if (!typeId) {
64
+ console.error("Missing required argument: type-id\n");
65
+ console.error("Usage: prismic page-type add-field date <type-id> <field-id>");
66
+ process.exitCode = 1;
67
+ return;
68
+ }
69
+
70
+ if (!fieldId) {
71
+ console.error("Missing required argument: field-id\n");
72
+ console.error("Usage: prismic page-type add-field date <type-id> <field-id>");
73
+ process.exitCode = 1;
74
+ return;
75
+ }
76
+
77
+ // Find the page type file
78
+ const projectRoot = await findUpward("package.json");
79
+ if (!projectRoot) {
80
+ console.error("Could not find project root (no package.json found)");
81
+ process.exitCode = 1;
82
+ return;
83
+ }
84
+
85
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
86
+
87
+ // Read and parse the model
88
+ let model: CustomType;
89
+ try {
90
+ const contents = await readFile(modelPath, "utf8");
91
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
92
+ if (!result.success) {
93
+ console.error(`Invalid page type model: ${modelPath.href}`);
94
+ process.exitCode = 1;
95
+ return;
96
+ }
97
+ model = result.output as CustomType;
98
+ } catch (error) {
99
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
100
+ console.error(`Page type not found: ${typeId}\n`);
101
+ console.error(`Create it first with: prismic page-type create ${typeId}`);
102
+ process.exitCode = 1;
103
+ return;
104
+ }
105
+ if (error instanceof Error) {
106
+ console.error(`Failed to read page type: ${error.message}`);
107
+ } else {
108
+ console.error("Failed to read page type");
109
+ }
110
+ process.exitCode = 1;
111
+ return;
112
+ }
113
+
114
+ // Determine target tab
115
+ const existingTabs = Object.keys(model.json);
116
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
117
+
118
+ // Initialize tab if it doesn't exist
119
+ if (!model.json[targetTab]) {
120
+ model.json[targetTab] = {};
121
+ }
122
+
123
+ // Check if field already exists in any tab
124
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
125
+ if (tabFields[fieldId]) {
126
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
127
+ process.exitCode = 1;
128
+ return;
129
+ }
130
+ }
131
+
132
+ // Build field definition
133
+ const fieldDefinition: DateField = {
134
+ type: "Date",
135
+ config: {
136
+ ...(label && { label }),
137
+ ...(placeholder && { placeholder }),
138
+ ...(defaultValue && { default: defaultValue }),
139
+ },
140
+ };
141
+
142
+ // Add field to model
143
+ model.json[targetTab][fieldId] = fieldDefinition;
144
+
145
+ // Write updated model
146
+ try {
147
+ await writeFile(modelPath, stringify(model));
148
+ } catch (error) {
149
+ if (error instanceof Error) {
150
+ console.error(`Failed to update page type: ${error.message}`);
151
+ } else {
152
+ console.error("Failed to update page type");
153
+ }
154
+ process.exitCode = 1;
155
+ return;
156
+ }
157
+
158
+ console.info(
159
+ `Added field "${fieldId}" (Date) to "${targetTab}" tab in ${typeId}`,
160
+ );
161
+ }