@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
@@ -0,0 +1,137 @@
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 { stringify } from "./lib/json";
7
+ import { findSliceModel } from "./lib/slice";
8
+
9
+ const HELP = `
10
+ Add an embed (oEmbed) field to an existing slice.
11
+
12
+ USAGE
13
+ prismic slice add-field embed <slice-id> <field-id> [flags]
14
+
15
+ ARGUMENTS
16
+ slice-id Slice identifier (required)
17
+ field-id Field identifier (required)
18
+
19
+ FLAGS
20
+ -v, --variation string Target variation (default: first variation)
21
+ -l, --label string Display label for the field
22
+ -p, --placeholder string Placeholder text
23
+ -h, --help Show help for command
24
+
25
+ EXAMPLES
26
+ prismic slice add-field embed my_slice video
27
+ prismic slice add-field embed gallery media --label "Media Embed"
28
+ prismic slice add-field embed social tweet --variation "twitter"
29
+ `.trim();
30
+
31
+ export async function sliceAddFieldEmbed(): Promise<void> {
32
+ const {
33
+ values: { help, variation, label, placeholder },
34
+ positionals: [sliceId, fieldId],
35
+ } = parseArgs({
36
+ args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "embed"
37
+ options: {
38
+ variation: { type: "string", short: "v" },
39
+ label: { type: "string", short: "l" },
40
+ placeholder: { type: "string", short: "p" },
41
+ help: { type: "boolean", short: "h" },
42
+ },
43
+ allowPositionals: true,
44
+ });
45
+
46
+ if (help) {
47
+ console.info(HELP);
48
+ return;
49
+ }
50
+
51
+ if (!sliceId) {
52
+ console.error("Missing required argument: slice-id\n");
53
+ console.error("Usage: prismic slice add-field embed <slice-id> <field-id>");
54
+ process.exitCode = 1;
55
+ return;
56
+ }
57
+
58
+ if (!fieldId) {
59
+ console.error("Missing required argument: field-id\n");
60
+ console.error("Usage: prismic slice add-field embed <slice-id> <field-id>");
61
+ process.exitCode = 1;
62
+ return;
63
+ }
64
+
65
+ // Find the slice model
66
+ const result = await findSliceModel(sliceId);
67
+ if (!result.ok) {
68
+ console.error(result.error);
69
+ process.exitCode = 1;
70
+ return;
71
+ }
72
+
73
+ const { model, modelPath } = result;
74
+
75
+ // Check for variations
76
+ if (model.variations.length === 0) {
77
+ console.error(`Slice "${sliceId}" has no variations.\n`);
78
+ console.error("Add a variation first before adding fields.");
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ // Find target variation
84
+ const targetVariation = variation
85
+ ? model.variations.find((v) => v.id === variation)
86
+ : model.variations[0];
87
+
88
+ if (!targetVariation) {
89
+ console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
90
+ console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
91
+ process.exitCode = 1;
92
+ return;
93
+ }
94
+
95
+ // Initialize primary if it doesn't exist
96
+ if (!targetVariation.primary) {
97
+ targetVariation.primary = {};
98
+ }
99
+
100
+ // Check if field already exists in any variation
101
+ for (const v of model.variations) {
102
+ if (v.primary?.[fieldId]) {
103
+ console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
104
+ process.exitCode = 1;
105
+ return;
106
+ }
107
+ }
108
+
109
+ // Build field definition
110
+ const fieldDefinition: Embed = {
111
+ type: "Embed",
112
+ config: {
113
+ ...(label && { label }),
114
+ ...(placeholder && { placeholder }),
115
+ },
116
+ };
117
+
118
+ // Add field to variation
119
+ targetVariation.primary[fieldId] = fieldDefinition;
120
+
121
+ // Write updated model
122
+ try {
123
+ await writeFile(modelPath, stringify(model as SharedSlice));
124
+ } catch (error) {
125
+ if (error instanceof Error) {
126
+ console.error(`Failed to update slice: ${error.message}`);
127
+ } else {
128
+ console.error("Failed to update slice");
129
+ }
130
+ process.exitCode = 1;
131
+ return;
132
+ }
133
+
134
+ console.info(
135
+ `Added field "${fieldId}" (Embed) to "${targetVariation.id}" variation in ${sliceId}`,
136
+ );
137
+ }
@@ -0,0 +1,134 @@
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 { stringify } from "./lib/json";
7
+ import { findSliceModel } from "./lib/slice";
8
+
9
+ const HELP = `
10
+ Add a geographic coordinates field to an existing slice.
11
+
12
+ USAGE
13
+ prismic slice add-field geo-point <slice-id> <field-id> [flags]
14
+
15
+ ARGUMENTS
16
+ slice-id Slice identifier (required)
17
+ field-id Field identifier (required)
18
+
19
+ FLAGS
20
+ -v, --variation string Target variation (default: first variation)
21
+ -l, --label string Display label for the field
22
+ -h, --help Show help for command
23
+
24
+ EXAMPLES
25
+ prismic slice add-field geo-point my_slice location
26
+ prismic slice add-field geo-point store coordinates --label "Store Location"
27
+ prismic slice add-field geo-point map marker --variation "interactive"
28
+ `.trim();
29
+
30
+ export async function sliceAddFieldGeoPoint(): Promise<void> {
31
+ const {
32
+ values: { help, variation, label },
33
+ positionals: [sliceId, fieldId],
34
+ } = parseArgs({
35
+ args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "geo-point"
36
+ options: {
37
+ variation: { type: "string", short: "v" },
38
+ label: { type: "string", short: "l" },
39
+ help: { type: "boolean", short: "h" },
40
+ },
41
+ allowPositionals: true,
42
+ });
43
+
44
+ if (help) {
45
+ console.info(HELP);
46
+ return;
47
+ }
48
+
49
+ if (!sliceId) {
50
+ console.error("Missing required argument: slice-id\n");
51
+ console.error("Usage: prismic slice add-field geo-point <slice-id> <field-id>");
52
+ process.exitCode = 1;
53
+ return;
54
+ }
55
+
56
+ if (!fieldId) {
57
+ console.error("Missing required argument: field-id\n");
58
+ console.error("Usage: prismic slice add-field geo-point <slice-id> <field-id>");
59
+ process.exitCode = 1;
60
+ return;
61
+ }
62
+
63
+ // Find the slice model
64
+ const result = await findSliceModel(sliceId);
65
+ if (!result.ok) {
66
+ console.error(result.error);
67
+ process.exitCode = 1;
68
+ return;
69
+ }
70
+
71
+ const { model, modelPath } = result;
72
+
73
+ // Check for variations
74
+ if (model.variations.length === 0) {
75
+ console.error(`Slice "${sliceId}" has no variations.\n`);
76
+ console.error("Add a variation first before adding fields.");
77
+ process.exitCode = 1;
78
+ return;
79
+ }
80
+
81
+ // Find target variation
82
+ const targetVariation = variation
83
+ ? model.variations.find((v) => v.id === variation)
84
+ : model.variations[0];
85
+
86
+ if (!targetVariation) {
87
+ console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
88
+ console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
89
+ process.exitCode = 1;
90
+ return;
91
+ }
92
+
93
+ // Initialize primary if it doesn't exist
94
+ if (!targetVariation.primary) {
95
+ targetVariation.primary = {};
96
+ }
97
+
98
+ // Check if field already exists in any variation
99
+ for (const v of model.variations) {
100
+ if (v.primary?.[fieldId]) {
101
+ console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
102
+ process.exitCode = 1;
103
+ return;
104
+ }
105
+ }
106
+
107
+ // Build field definition
108
+ const fieldDefinition: GeoPoint = {
109
+ type: "GeoPoint",
110
+ config: {
111
+ ...(label && { label }),
112
+ },
113
+ };
114
+
115
+ // Add field to variation
116
+ targetVariation.primary[fieldId] = fieldDefinition;
117
+
118
+ // Write updated model
119
+ try {
120
+ await writeFile(modelPath, stringify(model as SharedSlice));
121
+ } catch (error) {
122
+ if (error instanceof Error) {
123
+ console.error(`Failed to update slice: ${error.message}`);
124
+ } else {
125
+ console.error("Failed to update slice");
126
+ }
127
+ process.exitCode = 1;
128
+ return;
129
+ }
130
+
131
+ console.info(
132
+ `Added field "${fieldId}" (GeoPoint) to "${targetVariation.id}" variation in ${sliceId}`,
133
+ );
134
+ }
@@ -0,0 +1,134 @@
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 { stringify } from "./lib/json";
7
+ import { findSliceModel } from "./lib/slice";
8
+
9
+ const HELP = `
10
+ Add an image field to an existing slice.
11
+
12
+ USAGE
13
+ prismic slice add-field image <slice-id> <field-id> [flags]
14
+
15
+ ARGUMENTS
16
+ slice-id Slice identifier (required)
17
+ field-id Field identifier (required)
18
+
19
+ FLAGS
20
+ -v, --variation string Target variation (default: first variation)
21
+ -l, --label string Display label for the field
22
+ -h, --help Show help for command
23
+
24
+ EXAMPLES
25
+ prismic slice add-field image my_slice background
26
+ prismic slice add-field image hero banner --label "Hero Banner"
27
+ prismic slice add-field image gallery thumbnail --variation "grid"
28
+ `.trim();
29
+
30
+ export async function sliceAddFieldImage(): Promise<void> {
31
+ const {
32
+ values: { help, variation, label },
33
+ positionals: [sliceId, fieldId],
34
+ } = parseArgs({
35
+ args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "image"
36
+ options: {
37
+ variation: { type: "string", short: "v" },
38
+ label: { type: "string", short: "l" },
39
+ help: { type: "boolean", short: "h" },
40
+ },
41
+ allowPositionals: true,
42
+ });
43
+
44
+ if (help) {
45
+ console.info(HELP);
46
+ return;
47
+ }
48
+
49
+ if (!sliceId) {
50
+ console.error("Missing required argument: slice-id\n");
51
+ console.error("Usage: prismic slice add-field image <slice-id> <field-id>");
52
+ process.exitCode = 1;
53
+ return;
54
+ }
55
+
56
+ if (!fieldId) {
57
+ console.error("Missing required argument: field-id\n");
58
+ console.error("Usage: prismic slice add-field image <slice-id> <field-id>");
59
+ process.exitCode = 1;
60
+ return;
61
+ }
62
+
63
+ // Find the slice model
64
+ const result = await findSliceModel(sliceId);
65
+ if (!result.ok) {
66
+ console.error(result.error);
67
+ process.exitCode = 1;
68
+ return;
69
+ }
70
+
71
+ const { model, modelPath } = result;
72
+
73
+ // Check for variations
74
+ if (model.variations.length === 0) {
75
+ console.error(`Slice "${sliceId}" has no variations.\n`);
76
+ console.error("Add a variation first before adding fields.");
77
+ process.exitCode = 1;
78
+ return;
79
+ }
80
+
81
+ // Find target variation
82
+ const targetVariation = variation
83
+ ? model.variations.find((v) => v.id === variation)
84
+ : model.variations[0];
85
+
86
+ if (!targetVariation) {
87
+ console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
88
+ console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
89
+ process.exitCode = 1;
90
+ return;
91
+ }
92
+
93
+ // Initialize primary if it doesn't exist
94
+ if (!targetVariation.primary) {
95
+ targetVariation.primary = {};
96
+ }
97
+
98
+ // Check if field already exists in any variation
99
+ for (const v of model.variations) {
100
+ if (v.primary?.[fieldId]) {
101
+ console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
102
+ process.exitCode = 1;
103
+ return;
104
+ }
105
+ }
106
+
107
+ // Build field definition
108
+ const fieldDefinition: Image = {
109
+ type: "Image",
110
+ config: {
111
+ ...(label && { label }),
112
+ },
113
+ };
114
+
115
+ // Add field to variation
116
+ targetVariation.primary[fieldId] = fieldDefinition;
117
+
118
+ // Write updated model
119
+ try {
120
+ await writeFile(modelPath, stringify(model as SharedSlice));
121
+ } catch (error) {
122
+ if (error instanceof Error) {
123
+ console.error(`Failed to update slice: ${error.message}`);
124
+ } else {
125
+ console.error("Failed to update slice");
126
+ }
127
+ process.exitCode = 1;
128
+ return;
129
+ }
130
+
131
+ console.info(
132
+ `Added field "${fieldId}" (Image) to "${targetVariation.id}" variation in ${sliceId}`,
133
+ );
134
+ }
@@ -0,0 +1,137 @@
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 { stringify } from "./lib/json";
7
+ import { findSliceModel } from "./lib/slice";
8
+
9
+ const HELP = `
10
+ Add a key-text (single-line text) field to an existing slice.
11
+
12
+ USAGE
13
+ prismic slice add-field key-text <slice-id> <field-id> [flags]
14
+
15
+ ARGUMENTS
16
+ slice-id Slice identifier (required)
17
+ field-id Field identifier (required)
18
+
19
+ FLAGS
20
+ -v, --variation string Target variation (default: first variation)
21
+ -l, --label string Display label for the field
22
+ -p, --placeholder string Placeholder text
23
+ -h, --help Show help for command
24
+
25
+ EXAMPLES
26
+ prismic slice add-field key-text my_slice title
27
+ prismic slice add-field key-text hero heading --label "Heading"
28
+ prismic slice add-field key-text cta button_text --placeholder "Enter button text"
29
+ `.trim();
30
+
31
+ export async function sliceAddFieldKeyText(): Promise<void> {
32
+ const {
33
+ values: { help, variation, label, placeholder },
34
+ positionals: [sliceId, fieldId],
35
+ } = parseArgs({
36
+ args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "key-text"
37
+ options: {
38
+ variation: { type: "string", short: "v" },
39
+ label: { type: "string", short: "l" },
40
+ placeholder: { type: "string", short: "p" },
41
+ help: { type: "boolean", short: "h" },
42
+ },
43
+ allowPositionals: true,
44
+ });
45
+
46
+ if (help) {
47
+ console.info(HELP);
48
+ return;
49
+ }
50
+
51
+ if (!sliceId) {
52
+ console.error("Missing required argument: slice-id\n");
53
+ console.error("Usage: prismic slice add-field key-text <slice-id> <field-id>");
54
+ process.exitCode = 1;
55
+ return;
56
+ }
57
+
58
+ if (!fieldId) {
59
+ console.error("Missing required argument: field-id\n");
60
+ console.error("Usage: prismic slice add-field key-text <slice-id> <field-id>");
61
+ process.exitCode = 1;
62
+ return;
63
+ }
64
+
65
+ // Find the slice model
66
+ const result = await findSliceModel(sliceId);
67
+ if (!result.ok) {
68
+ console.error(result.error);
69
+ process.exitCode = 1;
70
+ return;
71
+ }
72
+
73
+ const { model, modelPath } = result;
74
+
75
+ // Check for variations
76
+ if (model.variations.length === 0) {
77
+ console.error(`Slice "${sliceId}" has no variations.\n`);
78
+ console.error("Add a variation first before adding fields.");
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ // Find target variation
84
+ const targetVariation = variation
85
+ ? model.variations.find((v) => v.id === variation)
86
+ : model.variations[0];
87
+
88
+ if (!targetVariation) {
89
+ console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
90
+ console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
91
+ process.exitCode = 1;
92
+ return;
93
+ }
94
+
95
+ // Initialize primary if it doesn't exist
96
+ if (!targetVariation.primary) {
97
+ targetVariation.primary = {};
98
+ }
99
+
100
+ // Check if field already exists in any variation
101
+ for (const v of model.variations) {
102
+ if (v.primary?.[fieldId]) {
103
+ console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
104
+ process.exitCode = 1;
105
+ return;
106
+ }
107
+ }
108
+
109
+ // Build field definition
110
+ const fieldDefinition: Text = {
111
+ type: "Text",
112
+ config: {
113
+ ...(label && { label }),
114
+ ...(placeholder && { placeholder }),
115
+ },
116
+ };
117
+
118
+ // Add field to variation
119
+ targetVariation.primary[fieldId] = fieldDefinition;
120
+
121
+ // Write updated model
122
+ try {
123
+ await writeFile(modelPath, stringify(model as SharedSlice));
124
+ } catch (error) {
125
+ if (error instanceof Error) {
126
+ console.error(`Failed to update slice: ${error.message}`);
127
+ } else {
128
+ console.error("Failed to update slice");
129
+ }
130
+ process.exitCode = 1;
131
+ return;
132
+ }
133
+
134
+ console.info(
135
+ `Added field "${fieldId}" (Text) to "${targetVariation.id}" variation in ${sliceId}`,
136
+ );
137
+ }
@@ -0,0 +1,155 @@
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 { stringify } from "./lib/json";
7
+ import { findSliceModel } from "./lib/slice";
8
+
9
+ const HELP = `
10
+ Add a link field to an existing slice.
11
+
12
+ USAGE
13
+ prismic slice add-field link <slice-id> <field-id> [flags]
14
+
15
+ ARGUMENTS
16
+ slice-id Slice identifier (required)
17
+ field-id Field identifier (required)
18
+
19
+ FLAGS
20
+ -v, --variation string Target variation (default: first variation)
21
+ -l, --label string Display label for the field
22
+ -p, --placeholder string Placeholder text
23
+ --allow-text Allow text with link
24
+ --allow-target-blank Allow opening link in new tab
25
+ --repeatable Allow multiple links
26
+ -h, --help Show help for command
27
+
28
+ EXAMPLES
29
+ prismic slice add-field link my_slice button
30
+ prismic slice add-field link cta primary_link --allow-text
31
+ prismic slice add-field link navigation links --repeatable
32
+ prismic slice add-field link hero cta --allow-text --allow-target-blank
33
+ `.trim();
34
+
35
+ export async function sliceAddFieldLink(): Promise<void> {
36
+ const {
37
+ values: {
38
+ help,
39
+ variation,
40
+ label,
41
+ placeholder,
42
+ "allow-text": allowText,
43
+ "allow-target-blank": allowTargetBlank,
44
+ repeatable,
45
+ },
46
+ positionals: [sliceId, fieldId],
47
+ } = parseArgs({
48
+ args: process.argv.slice(5), // skip: node, script, "slice", "add-field", "link"
49
+ options: {
50
+ variation: { type: "string", short: "v" },
51
+ label: { type: "string", short: "l" },
52
+ placeholder: { type: "string", short: "p" },
53
+ "allow-text": { type: "boolean" },
54
+ "allow-target-blank": { type: "boolean" },
55
+ repeatable: { type: "boolean" },
56
+ help: { type: "boolean", short: "h" },
57
+ },
58
+ allowPositionals: true,
59
+ });
60
+
61
+ if (help) {
62
+ console.info(HELP);
63
+ return;
64
+ }
65
+
66
+ if (!sliceId) {
67
+ console.error("Missing required argument: slice-id\n");
68
+ console.error("Usage: prismic slice add-field link <slice-id> <field-id>");
69
+ process.exitCode = 1;
70
+ return;
71
+ }
72
+
73
+ if (!fieldId) {
74
+ console.error("Missing required argument: field-id\n");
75
+ console.error("Usage: prismic slice add-field link <slice-id> <field-id>");
76
+ process.exitCode = 1;
77
+ return;
78
+ }
79
+
80
+ // Find the slice model
81
+ const result = await findSliceModel(sliceId);
82
+ if (!result.ok) {
83
+ console.error(result.error);
84
+ process.exitCode = 1;
85
+ return;
86
+ }
87
+
88
+ const { model, modelPath } = result;
89
+
90
+ // Check for variations
91
+ if (model.variations.length === 0) {
92
+ console.error(`Slice "${sliceId}" has no variations.\n`);
93
+ console.error("Add a variation first before adding fields.");
94
+ process.exitCode = 1;
95
+ return;
96
+ }
97
+
98
+ // Find target variation
99
+ const targetVariation = variation
100
+ ? model.variations.find((v) => v.id === variation)
101
+ : model.variations[0];
102
+
103
+ if (!targetVariation) {
104
+ console.error(`Variation "${variation}" not found in slice "${sliceId}"\n`);
105
+ console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
106
+ process.exitCode = 1;
107
+ return;
108
+ }
109
+
110
+ // Initialize primary if it doesn't exist
111
+ if (!targetVariation.primary) {
112
+ targetVariation.primary = {};
113
+ }
114
+
115
+ // Check if field already exists in any variation
116
+ for (const v of model.variations) {
117
+ if (v.primary?.[fieldId]) {
118
+ console.error(`Field "${fieldId}" already exists in variation "${v.id}"`);
119
+ process.exitCode = 1;
120
+ return;
121
+ }
122
+ }
123
+
124
+ // Build field definition
125
+ const fieldDefinition: Link = {
126
+ type: "Link",
127
+ config: {
128
+ ...(label && { label }),
129
+ ...(placeholder && { placeholder }),
130
+ ...(allowText && { allowText: true }),
131
+ ...(allowTargetBlank && { allowTargetBlank: true }),
132
+ ...(repeatable && { repeat: true }),
133
+ },
134
+ };
135
+
136
+ // Add field to variation
137
+ targetVariation.primary[fieldId] = fieldDefinition;
138
+
139
+ // Write updated model
140
+ try {
141
+ await writeFile(modelPath, stringify(model as SharedSlice));
142
+ } catch (error) {
143
+ if (error instanceof Error) {
144
+ console.error(`Failed to update slice: ${error.message}`);
145
+ } else {
146
+ console.error("Failed to update slice");
147
+ }
148
+ process.exitCode = 1;
149
+ return;
150
+ }
151
+
152
+ console.info(
153
+ `Added field "${fieldId}" (Link) to "${targetVariation.id}" variation in ${sliceId}`,
154
+ );
155
+ }