@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.
Files changed (131) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +2548 -0
  4. package/package.json +53 -0
  5. package/src/codegen-types.ts +82 -0
  6. package/src/codegen.ts +45 -0
  7. package/src/custom-type-add-field-boolean.ts +192 -0
  8. package/src/custom-type-add-field-color.ts +177 -0
  9. package/src/custom-type-add-field-date.ts +180 -0
  10. package/src/custom-type-add-field-embed.ts +177 -0
  11. package/src/custom-type-add-field-geo-point.ts +174 -0
  12. package/src/custom-type-add-field-image.ts +177 -0
  13. package/src/custom-type-add-field-key-text.ts +177 -0
  14. package/src/custom-type-add-field-link.ts +201 -0
  15. package/src/custom-type-add-field-number.ts +209 -0
  16. package/src/custom-type-add-field-rich-text.ts +202 -0
  17. package/src/custom-type-add-field-select.ts +192 -0
  18. package/src/custom-type-add-field-timestamp.ts +180 -0
  19. package/src/custom-type-add-field-uid.ts +177 -0
  20. package/src/custom-type-add-field.ts +111 -0
  21. package/src/custom-type-connect-slice.ts +220 -0
  22. package/src/custom-type-create.ts +118 -0
  23. package/src/custom-type-disconnect-slice.ts +177 -0
  24. package/src/custom-type-list.ts +110 -0
  25. package/src/custom-type-remove-field.ts +177 -0
  26. package/src/custom-type-remove.ts +144 -0
  27. package/src/custom-type-set-name.ts +144 -0
  28. package/src/custom-type-view.ts +118 -0
  29. package/src/custom-type.ts +85 -0
  30. package/src/index.ts +127 -0
  31. package/src/init.ts +64 -0
  32. package/src/lib/auth.ts +83 -0
  33. package/src/lib/config.ts +111 -0
  34. package/src/lib/custom-types-api.ts +438 -0
  35. package/src/lib/file.ts +49 -0
  36. package/src/lib/framework.ts +143 -0
  37. package/src/lib/json.ts +3 -0
  38. package/src/lib/request.ts +116 -0
  39. package/src/lib/slice.ts +115 -0
  40. package/src/lib/string.ts +6 -0
  41. package/src/lib/url.ts +25 -0
  42. package/src/locale-add.ts +116 -0
  43. package/src/locale-list.ts +107 -0
  44. package/src/locale-remove.ts +88 -0
  45. package/src/locale-set-default.ts +131 -0
  46. package/src/locale.ts +60 -0
  47. package/src/login.ts +152 -0
  48. package/src/logout.ts +36 -0
  49. package/src/page-type-add-field-boolean.ts +192 -0
  50. package/src/page-type-add-field-color.ts +177 -0
  51. package/src/page-type-add-field-date.ts +180 -0
  52. package/src/page-type-add-field-embed.ts +177 -0
  53. package/src/page-type-add-field-geo-point.ts +174 -0
  54. package/src/page-type-add-field-image.ts +177 -0
  55. package/src/page-type-add-field-key-text.ts +177 -0
  56. package/src/page-type-add-field-link.ts +201 -0
  57. package/src/page-type-add-field-number.ts +209 -0
  58. package/src/page-type-add-field-rich-text.ts +202 -0
  59. package/src/page-type-add-field-select.ts +192 -0
  60. package/src/page-type-add-field-timestamp.ts +180 -0
  61. package/src/page-type-add-field-uid.ts +177 -0
  62. package/src/page-type-add-field.ts +111 -0
  63. package/src/page-type-connect-slice.ts +220 -0
  64. package/src/page-type-create.ts +142 -0
  65. package/src/page-type-disconnect-slice.ts +177 -0
  66. package/src/page-type-list.ts +109 -0
  67. package/src/page-type-remove-field.ts +177 -0
  68. package/src/page-type-remove.ts +144 -0
  69. package/src/page-type-set-name.ts +144 -0
  70. package/src/page-type-set-repeatable.ts +153 -0
  71. package/src/page-type-view.ts +118 -0
  72. package/src/page-type.ts +90 -0
  73. package/src/preview-add.ts +126 -0
  74. package/src/preview-get-simulator.ts +104 -0
  75. package/src/preview-list.ts +106 -0
  76. package/src/preview-remove-simulator.ts +80 -0
  77. package/src/preview-remove.ts +109 -0
  78. package/src/preview-set-name.ts +137 -0
  79. package/src/preview-set-simulator.ts +116 -0
  80. package/src/preview.ts +75 -0
  81. package/src/pull.ts +247 -0
  82. package/src/push.ts +405 -0
  83. package/src/repo-create.ts +136 -0
  84. package/src/repo-get-access.ts +86 -0
  85. package/src/repo-list.ts +100 -0
  86. package/src/repo-set-access.ts +100 -0
  87. package/src/repo-set-name.ts +102 -0
  88. package/src/repo-view.ts +113 -0
  89. package/src/repo.ts +70 -0
  90. package/src/slice-add-field-boolean.ts +173 -0
  91. package/src/slice-add-field-color.ts +158 -0
  92. package/src/slice-add-field-date.ts +158 -0
  93. package/src/slice-add-field-embed.ts +158 -0
  94. package/src/slice-add-field-geo-point.ts +155 -0
  95. package/src/slice-add-field-image.ts +155 -0
  96. package/src/slice-add-field-key-text.ts +158 -0
  97. package/src/slice-add-field-link.ts +178 -0
  98. package/src/slice-add-field-number.ts +158 -0
  99. package/src/slice-add-field-rich-text.ts +183 -0
  100. package/src/slice-add-field-select.ts +173 -0
  101. package/src/slice-add-field-timestamp.ts +158 -0
  102. package/src/slice-add-field.ts +106 -0
  103. package/src/slice-add-variation.ts +145 -0
  104. package/src/slice-create.ts +148 -0
  105. package/src/slice-list-variations.ts +67 -0
  106. package/src/slice-list.ts +88 -0
  107. package/src/slice-remove-field.ts +128 -0
  108. package/src/slice-remove-variation.ts +118 -0
  109. package/src/slice-remove.ts +97 -0
  110. package/src/slice-rename.ts +128 -0
  111. package/src/slice-view.ts +77 -0
  112. package/src/slice.ts +90 -0
  113. package/src/status.ts +733 -0
  114. package/src/token-create.ts +203 -0
  115. package/src/token-delete.ts +182 -0
  116. package/src/token-list.ts +223 -0
  117. package/src/token-set-name.ts +193 -0
  118. package/src/token.ts +60 -0
  119. package/src/webhook-add-header.ts +118 -0
  120. package/src/webhook-create.ts +152 -0
  121. package/src/webhook-disable.ts +109 -0
  122. package/src/webhook-enable.ts +132 -0
  123. package/src/webhook-list.ts +93 -0
  124. package/src/webhook-remove-header.ts +117 -0
  125. package/src/webhook-remove.ts +106 -0
  126. package/src/webhook-set-triggers.ts +148 -0
  127. package/src/webhook-status.ts +90 -0
  128. package/src/webhook-test.ts +106 -0
  129. package/src/webhook-view.ts +147 -0
  130. package/src/webhook.ts +95 -0
  131. package/src/whoami.ts +62 -0
@@ -0,0 +1,158 @@
1
+ import type { Color, 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 color picker field to an existing slice.
13
+
14
+ USAGE
15
+ prismic slice add-field color <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 color my_slice background_color
33
+ prismic slice add-field color hero accent --label "Accent Color"
34
+ prismic slice add-field color banner theme_color --variation "dark"
35
+ `.trim();
36
+
37
+ export async function sliceAddFieldColor(): 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", "color"
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 color <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 color <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: Color = {
119
+ type: "Color",
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}" (Color) 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,158 @@
1
+ import type { Date as DateField, 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 date picker field to an existing slice.
13
+
14
+ USAGE
15
+ prismic slice add-field date <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 date my_slice publish_date
33
+ prismic slice add-field date event start_date --label "Start Date"
34
+ prismic slice add-field date promo end_date --variation "countdown"
35
+ `.trim();
36
+
37
+ export async function sliceAddFieldDate(): 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", "date"
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 date <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 date <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: DateField = {
119
+ type: "Date",
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}" (Date) 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,158 @@
1
+ import type { Embed, 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 embed (oEmbed) field to an existing slice.
13
+
14
+ USAGE
15
+ prismic slice add-field embed <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 embed my_slice video
33
+ prismic slice add-field embed gallery media --label "Media Embed"
34
+ prismic slice add-field embed social tweet --variation "twitter"
35
+ `.trim();
36
+
37
+ export async function sliceAddFieldEmbed(): 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", "embed"
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 embed <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 embed <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: Embed = {
119
+ type: "Embed",
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}" (Embed) 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,155 @@
1
+ import type { GeoPoint, 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 geographic coordinates field to an existing slice.
13
+
14
+ USAGE
15
+ prismic slice add-field geo-point <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 geo-point my_slice location
32
+ prismic slice add-field geo-point store coordinates --label "Store Location"
33
+ prismic slice add-field geo-point map marker --variation "interactive"
34
+ `.trim();
35
+
36
+ export async function sliceAddFieldGeoPoint(): 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", "geo-point"
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 geo-point <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 geo-point <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: GeoPoint = {
117
+ type: "GeoPoint",
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}" (GeoPoint) 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
+ }