@angeloashmore/prismic-cli-poc 0.0.0-canary.1143872

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 (139) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +2508 -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 +222 -0
  8. package/src/custom-type-add-field-color.ts +205 -0
  9. package/src/custom-type-add-field-date.ts +208 -0
  10. package/src/custom-type-add-field-embed.ts +205 -0
  11. package/src/custom-type-add-field-geo-point.ts +202 -0
  12. package/src/custom-type-add-field-group.ts +179 -0
  13. package/src/custom-type-add-field-image.ts +205 -0
  14. package/src/custom-type-add-field-key-text.ts +205 -0
  15. package/src/custom-type-add-field-link.ts +228 -0
  16. package/src/custom-type-add-field-number.ts +237 -0
  17. package/src/custom-type-add-field-rich-text.ts +229 -0
  18. package/src/custom-type-add-field-select.ts +211 -0
  19. package/src/custom-type-add-field-timestamp.ts +208 -0
  20. package/src/custom-type-add-field-uid.ts +188 -0
  21. package/src/custom-type-add-field.ts +116 -0
  22. package/src/custom-type-connect-slice.ts +214 -0
  23. package/src/custom-type-create.ts +112 -0
  24. package/src/custom-type-disconnect-slice.ts +171 -0
  25. package/src/custom-type-list.ts +110 -0
  26. package/src/custom-type-remove-field.ts +171 -0
  27. package/src/custom-type-remove.ts +138 -0
  28. package/src/custom-type-set-name.ts +138 -0
  29. package/src/custom-type-view.ts +118 -0
  30. package/src/custom-type.ts +85 -0
  31. package/src/docs-fetch.ts +146 -0
  32. package/src/docs-list.ts +131 -0
  33. package/src/docs.ts +54 -0
  34. package/src/index.ts +132 -0
  35. package/src/init.ts +64 -0
  36. package/src/lib/auth.ts +83 -0
  37. package/src/lib/config.ts +111 -0
  38. package/src/lib/custom-types-api.ts +438 -0
  39. package/src/lib/field-path.ts +81 -0
  40. package/src/lib/file.ts +49 -0
  41. package/src/lib/framework.ts +143 -0
  42. package/src/lib/json.ts +3 -0
  43. package/src/lib/request.ts +116 -0
  44. package/src/lib/slice.ts +115 -0
  45. package/src/lib/string.ts +6 -0
  46. package/src/lib/url.ts +25 -0
  47. package/src/locale-add.ts +116 -0
  48. package/src/locale-list.ts +107 -0
  49. package/src/locale-remove.ts +88 -0
  50. package/src/locale-set-default.ts +131 -0
  51. package/src/locale.ts +60 -0
  52. package/src/login.ts +152 -0
  53. package/src/logout.ts +36 -0
  54. package/src/page-type-add-field-boolean.ts +238 -0
  55. package/src/page-type-add-field-color.ts +224 -0
  56. package/src/page-type-add-field-date.ts +227 -0
  57. package/src/page-type-add-field-embed.ts +224 -0
  58. package/src/page-type-add-field-geo-point.ts +221 -0
  59. package/src/page-type-add-field-group.ts +198 -0
  60. package/src/page-type-add-field-image.ts +224 -0
  61. package/src/page-type-add-field-key-text.ts +224 -0
  62. package/src/page-type-add-field-link.ts +247 -0
  63. package/src/page-type-add-field-number.ts +256 -0
  64. package/src/page-type-add-field-rich-text.ts +248 -0
  65. package/src/page-type-add-field-select.ts +230 -0
  66. package/src/page-type-add-field-timestamp.ts +227 -0
  67. package/src/page-type-add-field-uid.ts +207 -0
  68. package/src/page-type-add-field.ts +116 -0
  69. package/src/page-type-connect-slice.ts +214 -0
  70. package/src/page-type-create.ts +161 -0
  71. package/src/page-type-disconnect-slice.ts +171 -0
  72. package/src/page-type-list.ts +109 -0
  73. package/src/page-type-remove-field.ts +171 -0
  74. package/src/page-type-remove.ts +138 -0
  75. package/src/page-type-set-name.ts +138 -0
  76. package/src/page-type-set-repeatable.ts +147 -0
  77. package/src/page-type-view.ts +118 -0
  78. package/src/page-type.ts +90 -0
  79. package/src/preview-add.ts +126 -0
  80. package/src/preview-get-simulator.ts +104 -0
  81. package/src/preview-list.ts +106 -0
  82. package/src/preview-remove-simulator.ts +80 -0
  83. package/src/preview-remove.ts +109 -0
  84. package/src/preview-set-name.ts +137 -0
  85. package/src/preview-set-simulator.ts +116 -0
  86. package/src/preview.ts +75 -0
  87. package/src/pull.ts +242 -0
  88. package/src/push.ts +405 -0
  89. package/src/repo-create.ts +195 -0
  90. package/src/repo-get-access.ts +86 -0
  91. package/src/repo-list.ts +100 -0
  92. package/src/repo-set-access.ts +100 -0
  93. package/src/repo-set-name.ts +102 -0
  94. package/src/repo-view.ts +113 -0
  95. package/src/repo.ts +70 -0
  96. package/src/slice-add-field-boolean.ts +240 -0
  97. package/src/slice-add-field-color.ts +226 -0
  98. package/src/slice-add-field-date.ts +226 -0
  99. package/src/slice-add-field-embed.ts +226 -0
  100. package/src/slice-add-field-geo-point.ts +223 -0
  101. package/src/slice-add-field-group.ts +191 -0
  102. package/src/slice-add-field-image.ts +223 -0
  103. package/src/slice-add-field-key-text.ts +226 -0
  104. package/src/slice-add-field-link.ts +245 -0
  105. package/src/slice-add-field-number.ts +226 -0
  106. package/src/slice-add-field-rich-text.ts +250 -0
  107. package/src/slice-add-field-select.ts +232 -0
  108. package/src/slice-add-field-timestamp.ts +226 -0
  109. package/src/slice-add-field.ts +111 -0
  110. package/src/slice-add-variation.ts +139 -0
  111. package/src/slice-create.ts +203 -0
  112. package/src/slice-list-variations.ts +67 -0
  113. package/src/slice-list.ts +88 -0
  114. package/src/slice-remove-field.ts +122 -0
  115. package/src/slice-remove-variation.ts +112 -0
  116. package/src/slice-remove.ts +91 -0
  117. package/src/slice-rename.ts +122 -0
  118. package/src/slice-set-screenshot.ts +235 -0
  119. package/src/slice-view.ts +80 -0
  120. package/src/slice.ts +95 -0
  121. package/src/status.ts +873 -0
  122. package/src/token-create.ts +203 -0
  123. package/src/token-delete.ts +182 -0
  124. package/src/token-list.ts +223 -0
  125. package/src/token-set-name.ts +193 -0
  126. package/src/token.ts +60 -0
  127. package/src/webhook-add-header.ts +118 -0
  128. package/src/webhook-create.ts +152 -0
  129. package/src/webhook-disable.ts +109 -0
  130. package/src/webhook-enable.ts +132 -0
  131. package/src/webhook-list.ts +93 -0
  132. package/src/webhook-remove-header.ts +117 -0
  133. package/src/webhook-remove.ts +106 -0
  134. package/src/webhook-set-triggers.ts +148 -0
  135. package/src/webhook-status.ts +90 -0
  136. package/src/webhook-test.ts +106 -0
  137. package/src/webhook-view.ts +147 -0
  138. package/src/webhook.ts +95 -0
  139. package/src/whoami.ts +62 -0
@@ -0,0 +1,67 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { findSliceModel } from "./lib/slice";
4
+
5
+ const HELP = `
6
+ List all variations for a slice.
7
+
8
+ USAGE
9
+ prismic slice list-variations <slice-id> [flags]
10
+
11
+ ARGUMENTS
12
+ slice-id Slice identifier (required)
13
+
14
+ FLAGS
15
+ --json Output as JSON
16
+ -h, --help Show help for command
17
+
18
+ EXAMPLES
19
+ prismic slice list-variations MySlice
20
+ prismic slice list-variations MySlice --json
21
+ `.trim();
22
+
23
+ export async function sliceListVariations(): Promise<void> {
24
+ const {
25
+ values: { help, json },
26
+ positionals: [sliceId],
27
+ } = parseArgs({
28
+ args: process.argv.slice(4), // skip: node, script, "slice", "list-variations"
29
+ options: {
30
+ json: { type: "boolean" },
31
+ help: { type: "boolean", short: "h" },
32
+ },
33
+ allowPositionals: true,
34
+ });
35
+
36
+ if (help) {
37
+ console.info(HELP);
38
+ return;
39
+ }
40
+
41
+ if (!sliceId) {
42
+ console.error("Missing required argument: slice-id\n");
43
+ console.error("Usage: prismic slice list-variations <slice-id>");
44
+ process.exitCode = 1;
45
+ return;
46
+ }
47
+
48
+ const result = await findSliceModel(sliceId);
49
+ if (!result.ok) {
50
+ console.error(result.error);
51
+ process.exitCode = 1;
52
+ return;
53
+ }
54
+
55
+ const { model } = result;
56
+ const variations = model.variations.map((v) => ({ id: v.id, name: v.name }));
57
+
58
+ if (json) {
59
+ console.info(JSON.stringify(variations, null, 2));
60
+ return;
61
+ }
62
+
63
+ console.info("ID\tNAME");
64
+ for (const variation of variations) {
65
+ console.info(`${variation.id}\t${variation.name}`);
66
+ }
67
+ }
@@ -0,0 +1,88 @@
1
+ import { readdir, readFile } from "node:fs/promises";
2
+ import { parseArgs } from "node:util";
3
+ import * as v from "valibot";
4
+
5
+ import { getSlicesDirectory, SharedSliceSchema } from "./lib/slice";
6
+
7
+ const HELP = `
8
+ List all slices in a Prismic project.
9
+
10
+ USAGE
11
+ prismic slice list [flags]
12
+
13
+ FLAGS
14
+ --json Output as JSON
15
+ -h, --help Show help for command
16
+
17
+ EXAMPLES
18
+ prismic slice list
19
+ prismic slice list --json
20
+ `.trim();
21
+
22
+ export async function sliceList(): Promise<void> {
23
+ const {
24
+ values: { help, json },
25
+ } = parseArgs({
26
+ args: process.argv.slice(4), // skip: node, script, "slice", "list"
27
+ options: {
28
+ json: { type: "boolean" },
29
+ help: { type: "boolean", short: "h" },
30
+ },
31
+ allowPositionals: true,
32
+ });
33
+
34
+ if (help) {
35
+ console.info(HELP);
36
+ return;
37
+ }
38
+
39
+ const slicesDirectory = await getSlicesDirectory();
40
+
41
+ let entries: string[];
42
+ try {
43
+ entries = (await readdir(slicesDirectory, {
44
+ withFileTypes: false,
45
+ })) as unknown as string[];
46
+ } catch {
47
+ if (json) {
48
+ console.info(JSON.stringify([]));
49
+ } else {
50
+ console.info("No slices found.");
51
+ }
52
+ return;
53
+ }
54
+
55
+ const slices: { id: string; name: string }[] = [];
56
+
57
+ for (const entry of entries) {
58
+ const modelPath = new URL(`${entry}/model.json`, slicesDirectory);
59
+ try {
60
+ const contents = await readFile(modelPath, "utf8");
61
+ const parsed = JSON.parse(contents);
62
+ const result = v.safeParse(SharedSliceSchema, parsed);
63
+ if (result.success) {
64
+ slices.push({ id: result.output.id, name: result.output.name });
65
+ }
66
+ } catch {
67
+ // Skip directories without valid model.json
68
+ }
69
+ }
70
+
71
+ if (slices.length === 0) {
72
+ if (json) {
73
+ console.info(JSON.stringify([]));
74
+ } else {
75
+ console.info("No slices found.");
76
+ }
77
+ return;
78
+ }
79
+
80
+ if (json) {
81
+ console.info(JSON.stringify(slices, null, 2));
82
+ } else {
83
+ console.info("ID\tNAME");
84
+ for (const slice of slices) {
85
+ console.info(`${slice.id}\t${slice.name}`);
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,122 @@
1
+ import { writeFile } from "node:fs/promises";
2
+ import { parseArgs } from "node:util";
3
+
4
+ import { buildTypes } from "./codegen-types";
5
+ import { stringify } from "./lib/json";
6
+ import { findSliceModel } from "./lib/slice";
7
+
8
+ const HELP = `
9
+ Remove a field from a slice variation.
10
+
11
+ USAGE
12
+ prismic slice remove-field <slice-id> <field-id> [flags]
13
+
14
+ ARGUMENTS
15
+ slice-id Slice identifier (required)
16
+ field-id Field identifier (required)
17
+
18
+ FLAGS
19
+ --variation string Target variation (default: "default")
20
+ --zone string Field zone: "primary" or "items" (default: "primary")
21
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
22
+ -h, --help Show help for command
23
+
24
+ EXAMPLES
25
+ prismic slice remove-field MySlice title
26
+ prismic slice remove-field MySlice title --variation withImage
27
+ prismic slice remove-field MySlice item_title --zone items
28
+ `.trim();
29
+
30
+ export async function sliceRemoveField(): Promise<void> {
31
+ const {
32
+ values: { help, variation, zone, types },
33
+ positionals: [sliceId, fieldId],
34
+ } = parseArgs({
35
+ args: process.argv.slice(4), // skip: node, script, "slice", "remove-field"
36
+ options: {
37
+ variation: { type: "string", default: "default" },
38
+ zone: { type: "string", default: "primary" },
39
+ types: { type: "string" },
40
+ help: { type: "boolean", short: "h" },
41
+ },
42
+ allowPositionals: true,
43
+ });
44
+
45
+ if (help) {
46
+ console.info(HELP);
47
+ return;
48
+ }
49
+
50
+ if (!sliceId) {
51
+ console.error("Missing required argument: slice-id\n");
52
+ console.error("Usage: prismic slice remove-field <slice-id> <field-id>");
53
+ process.exitCode = 1;
54
+ return;
55
+ }
56
+
57
+ if (!fieldId) {
58
+ console.error("Missing required argument: field-id\n");
59
+ console.error("Usage: prismic slice remove-field <slice-id> <field-id>");
60
+ process.exitCode = 1;
61
+ return;
62
+ }
63
+
64
+ if (zone !== "primary" && zone !== "items") {
65
+ console.error(`Invalid zone: ${zone}. Must be "primary" or "items".`);
66
+ process.exitCode = 1;
67
+ return;
68
+ }
69
+
70
+ const result = await findSliceModel(sliceId);
71
+ if (!result.ok) {
72
+ console.error(result.error);
73
+ process.exitCode = 1;
74
+ return;
75
+ }
76
+
77
+ const { model, modelPath } = result;
78
+
79
+ // Find the variation
80
+ const targetVariation = model.variations.find((v) => v.id === variation);
81
+ if (!targetVariation) {
82
+ console.error(`Variation not found: ${variation}`);
83
+ console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
84
+ process.exitCode = 1;
85
+ return;
86
+ }
87
+
88
+ // Check if field exists
89
+ const zoneFields = zone === "primary" ? targetVariation.primary : targetVariation.items;
90
+ if (!zoneFields || !(fieldId in zoneFields)) {
91
+ console.error(`Field "${fieldId}" not found in ${zone} zone of variation "${variation}"`);
92
+ process.exitCode = 1;
93
+ return;
94
+ }
95
+
96
+ // Remove the field
97
+ delete zoneFields[fieldId];
98
+
99
+ // Write updated model
100
+ try {
101
+ await writeFile(modelPath, stringify(model));
102
+ } catch (error) {
103
+ if (error instanceof Error) {
104
+ console.error(`Failed to update slice: ${error.message}`);
105
+ } else {
106
+ console.error("Failed to update slice");
107
+ }
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+
112
+ console.info(
113
+ `Removed field "${fieldId}" from ${zone} zone in variation "${variation}" of slice "${sliceId}"`,
114
+ );
115
+
116
+ try {
117
+ await buildTypes({ output: types });
118
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
119
+ } catch (error) {
120
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
121
+ }
122
+ }
@@ -0,0 +1,112 @@
1
+ import type { 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
+
10
+ const HELP = `
11
+ Remove a variation from a slice.
12
+
13
+ USAGE
14
+ prismic slice remove-variation <slice-id> <variation-id> [flags]
15
+
16
+ ARGUMENTS
17
+ slice-id Slice identifier (required)
18
+ variation-id Variation to remove (required)
19
+
20
+ FLAGS
21
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
22
+ -h, --help Show help for command
23
+
24
+ EXAMPLES
25
+ prismic slice remove-variation MySlice withImage
26
+ `.trim();
27
+
28
+ export async function sliceRemoveVariation(): Promise<void> {
29
+ const {
30
+ values: { help, types },
31
+ positionals: [sliceId, variationId],
32
+ } = parseArgs({
33
+ args: process.argv.slice(4), // skip: node, script, "slice", "remove-variation"
34
+ options: {
35
+ types: { type: "string" },
36
+ help: { type: "boolean", short: "h" },
37
+ },
38
+ allowPositionals: true,
39
+ });
40
+
41
+ if (help) {
42
+ console.info(HELP);
43
+ return;
44
+ }
45
+
46
+ if (!sliceId) {
47
+ console.error("Missing required argument: slice-id\n");
48
+ console.error("Usage: prismic slice remove-variation <slice-id> <variation-id>");
49
+ process.exitCode = 1;
50
+ return;
51
+ }
52
+
53
+ if (!variationId) {
54
+ console.error("Missing required argument: variation-id\n");
55
+ console.error("Usage: prismic slice remove-variation <slice-id> <variation-id>");
56
+ process.exitCode = 1;
57
+ return;
58
+ }
59
+
60
+ const result = await findSliceModel(sliceId);
61
+ if (!result.ok) {
62
+ console.error(result.error);
63
+ process.exitCode = 1;
64
+ return;
65
+ }
66
+
67
+ const { model, modelPath } = result;
68
+
69
+ // Check if variation exists
70
+ const variationExists = model.variations.some((v) => v.id === variationId);
71
+ if (!variationExists) {
72
+ console.error(`Variation not found: ${variationId}`);
73
+ console.error(`Available variations: ${model.variations.map((v) => v.id).join(", ")}`);
74
+ process.exitCode = 1;
75
+ return;
76
+ }
77
+
78
+ // Prevent removing the last variation
79
+ if (model.variations.length === 1) {
80
+ console.error("Cannot remove the last variation from a slice.");
81
+ process.exitCode = 1;
82
+ return;
83
+ }
84
+
85
+ // Remove the variation
86
+ const updatedModel = {
87
+ ...model,
88
+ variations: model.variations.filter((v) => v.id !== variationId),
89
+ };
90
+
91
+ // Write updated model
92
+ try {
93
+ await writeFile(modelPath, stringify(updatedModel as SharedSlice));
94
+ } catch (error) {
95
+ if (error instanceof Error) {
96
+ console.error(`Failed to update slice: ${error.message}`);
97
+ } else {
98
+ console.error("Failed to update slice");
99
+ }
100
+ process.exitCode = 1;
101
+ return;
102
+ }
103
+
104
+ console.info(`Removed variation "${variationId}" from slice "${sliceId}"`);
105
+
106
+ try {
107
+ await buildTypes({ output: types });
108
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
109
+ } catch (error) {
110
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
111
+ }
112
+ }
@@ -0,0 +1,91 @@
1
+ import { rm } from "node:fs/promises";
2
+ import { parseArgs } from "node:util";
3
+
4
+ import { buildTypes } from "./codegen-types";
5
+ import { findSliceModel } from "./lib/slice";
6
+
7
+ const HELP = `
8
+ Remove a slice from the project.
9
+
10
+ USAGE
11
+ prismic slice remove <slice-id> [flags]
12
+
13
+ ARGUMENTS
14
+ slice-id Slice identifier (required)
15
+
16
+ FLAGS
17
+ -y Confirm removal
18
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
19
+ -h, --help Show help for command
20
+
21
+ EXAMPLES
22
+ prismic slice remove MySlice
23
+ prismic slice remove MySlice -y
24
+ `.trim();
25
+
26
+ export async function sliceRemove(): Promise<void> {
27
+ const {
28
+ values: { help, y, types },
29
+ positionals: [sliceId],
30
+ } = parseArgs({
31
+ args: process.argv.slice(4), // skip: node, script, "slice", "remove"
32
+ options: {
33
+ y: { type: "boolean", short: "y" },
34
+ types: { type: "string" },
35
+ help: { type: "boolean", short: "h" },
36
+ },
37
+ allowPositionals: true,
38
+ });
39
+
40
+ if (help) {
41
+ console.info(HELP);
42
+ return;
43
+ }
44
+
45
+ if (!sliceId) {
46
+ console.error("Missing required argument: slice-id\n");
47
+ console.error("Usage: prismic slice remove <slice-id>");
48
+ process.exitCode = 1;
49
+ return;
50
+ }
51
+
52
+ const result = await findSliceModel(sliceId);
53
+ if (!result.ok) {
54
+ console.error(result.error);
55
+ process.exitCode = 1;
56
+ return;
57
+ }
58
+
59
+ const { modelPath } = result;
60
+ const sliceDirectory = new URL(".", modelPath);
61
+
62
+ // Require -y flag to confirm deletion
63
+ if (!y) {
64
+ console.error(`Refusing to remove slice "${sliceId}" (this will delete the entire directory).`);
65
+ console.error("Re-run with -y to confirm.");
66
+ process.exitCode = 1;
67
+ return;
68
+ }
69
+
70
+ // Delete the slice directory
71
+ try {
72
+ await rm(sliceDirectory, { recursive: true });
73
+ } catch (error) {
74
+ if (error instanceof Error) {
75
+ console.error(`Failed to remove slice: ${error.message}`);
76
+ } else {
77
+ console.error("Failed to remove slice");
78
+ }
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ console.info(`Removed slice "${sliceId}"`);
84
+
85
+ try {
86
+ await buildTypes({ output: types });
87
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
88
+ } catch (error) {
89
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
90
+ }
91
+ }
@@ -0,0 +1,122 @@
1
+ import { rename, writeFile } from "node:fs/promises";
2
+ import { parseArgs } from "node:util";
3
+
4
+ import { buildTypes } from "./codegen-types";
5
+ import { stringify } from "./lib/json";
6
+ import { findSliceModel, getSlicesDirectory, pascalCase } from "./lib/slice";
7
+
8
+ const HELP = `
9
+ Rename a slice (updates name field, optionally id and directory).
10
+
11
+ USAGE
12
+ prismic slice rename <slice-id> <new-name> [flags]
13
+
14
+ ARGUMENTS
15
+ slice-id Current slice identifier (required)
16
+ new-name New display name (required)
17
+
18
+ FLAGS
19
+ --id string Also change the slice ID (renames directory)
20
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
21
+ -h, --help Show help for command
22
+
23
+ EXAMPLES
24
+ prismic slice rename MySlice "My New Name"
25
+ prismic slice rename MySlice "My New Name" --id NewSliceId
26
+ `.trim();
27
+
28
+ export async function sliceRename(): Promise<void> {
29
+ const {
30
+ values: { help, id: newId, types },
31
+ positionals: [sliceId, newName],
32
+ } = parseArgs({
33
+ args: process.argv.slice(4), // skip: node, script, "slice", "rename"
34
+ options: {
35
+ id: { type: "string" },
36
+ types: { type: "string" },
37
+ help: { type: "boolean", short: "h" },
38
+ },
39
+ allowPositionals: true,
40
+ });
41
+
42
+ if (help) {
43
+ console.info(HELP);
44
+ return;
45
+ }
46
+
47
+ if (!sliceId) {
48
+ console.error("Missing required argument: slice-id\n");
49
+ console.error("Usage: prismic slice rename <slice-id> <new-name>");
50
+ process.exitCode = 1;
51
+ return;
52
+ }
53
+
54
+ if (!newName) {
55
+ console.error("Missing required argument: new-name\n");
56
+ console.error("Usage: prismic slice rename <slice-id> <new-name>");
57
+ process.exitCode = 1;
58
+ return;
59
+ }
60
+
61
+ const result = await findSliceModel(sliceId);
62
+ if (!result.ok) {
63
+ console.error(result.error);
64
+ process.exitCode = 1;
65
+ return;
66
+ }
67
+
68
+ const { model, modelPath } = result;
69
+
70
+ // Update the model
71
+ model.name = newName;
72
+ if (newId) {
73
+ model.id = newId;
74
+ }
75
+
76
+ // Write updated model
77
+ try {
78
+ await writeFile(modelPath, stringify(model));
79
+ } catch (error) {
80
+ if (error instanceof Error) {
81
+ console.error(`Failed to update slice: ${error.message}`);
82
+ } else {
83
+ console.error("Failed to update slice");
84
+ }
85
+ process.exitCode = 1;
86
+ return;
87
+ }
88
+
89
+ // If changing ID, also rename the directory
90
+ if (newId) {
91
+ const slicesDirectory = await getSlicesDirectory();
92
+ const currentDir = new URL(".", modelPath);
93
+ const newDir = new URL(pascalCase(newName) + "/", slicesDirectory);
94
+
95
+ if (currentDir.href !== newDir.href) {
96
+ try {
97
+ await rename(currentDir, newDir);
98
+ console.info(`Renamed slice "${sliceId}" to "${newId}" (${newName})`);
99
+ console.info(`Moved directory to ${newDir.href}`);
100
+ } catch (error) {
101
+ if (error instanceof Error) {
102
+ console.error(`Failed to rename directory: ${error.message}`);
103
+ } else {
104
+ console.error("Failed to rename directory");
105
+ }
106
+ process.exitCode = 1;
107
+ return;
108
+ }
109
+ } else {
110
+ console.info(`Renamed slice "${sliceId}" to "${newId}" (${newName})`);
111
+ }
112
+ } else {
113
+ console.info(`Renamed slice "${sliceId}" to "${newName}"`);
114
+ }
115
+
116
+ try {
117
+ await buildTypes({ output: types });
118
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
119
+ } catch (error) {
120
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
121
+ }
122
+ }