@slicemachine/adapter-next 0.0.1 → 0.0.3

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 (185) hide show
  1. package/dist/SliceSimulator.cjs +93 -0
  2. package/dist/SliceSimulator.cjs.map +1 -0
  3. package/dist/SliceSimulator.d.ts +29 -0
  4. package/dist/SliceSimulator.js +74 -0
  5. package/dist/SliceSimulator.js.map +1 -0
  6. package/dist/hooks/customType-asset-delete.cjs +28 -0
  7. package/dist/hooks/customType-asset-delete.cjs.map +1 -0
  8. package/dist/hooks/customType-asset-delete.d.ts +3 -0
  9. package/dist/hooks/customType-asset-delete.js +9 -0
  10. package/dist/hooks/customType-asset-delete.js.map +1 -0
  11. package/dist/hooks/customType-asset-read.cjs +31 -0
  12. package/dist/hooks/customType-asset-read.cjs.map +1 -0
  13. package/dist/hooks/customType-asset-read.d.ts +3 -0
  14. package/dist/hooks/customType-asset-read.js +12 -0
  15. package/dist/hooks/customType-asset-read.js.map +1 -0
  16. package/dist/hooks/customType-asset-update.cjs +31 -0
  17. package/dist/hooks/customType-asset-update.cjs.map +1 -0
  18. package/dist/hooks/customType-asset-update.d.ts +3 -0
  19. package/dist/hooks/customType-asset-update.js +11 -0
  20. package/dist/hooks/customType-asset-update.js.map +1 -0
  21. package/dist/hooks/customType-create.cjs +41 -0
  22. package/dist/hooks/customType-create.cjs.map +1 -0
  23. package/dist/hooks/customType-create.d.ts +3 -0
  24. package/dist/hooks/customType-create.js +21 -0
  25. package/dist/hooks/customType-create.js.map +1 -0
  26. package/dist/hooks/customType-delete.cjs +30 -0
  27. package/dist/hooks/customType-delete.cjs.map +1 -0
  28. package/dist/hooks/customType-delete.d.ts +3 -0
  29. package/dist/hooks/customType-delete.js +11 -0
  30. package/dist/hooks/customType-delete.js.map +1 -0
  31. package/dist/hooks/customType-read.cjs +12 -0
  32. package/dist/hooks/customType-read.cjs.map +1 -0
  33. package/dist/hooks/customType-read.d.ts +3 -0
  34. package/dist/hooks/customType-read.js +12 -0
  35. package/dist/hooks/customType-read.js.map +1 -0
  36. package/dist/hooks/customType-rename.cjs +6 -0
  37. package/dist/hooks/customType-rename.cjs.map +1 -0
  38. package/dist/hooks/customType-rename.d.ts +1 -0
  39. package/dist/hooks/customType-rename.js +6 -0
  40. package/dist/hooks/customType-rename.js.map +1 -0
  41. package/dist/hooks/customType-update.cjs +6 -0
  42. package/dist/hooks/customType-update.cjs.map +1 -0
  43. package/dist/hooks/customType-update.d.ts +1 -0
  44. package/dist/hooks/customType-update.js +6 -0
  45. package/dist/hooks/customType-update.js.map +1 -0
  46. package/dist/hooks/customTypeLibrary-read.cjs +54 -0
  47. package/dist/hooks/customTypeLibrary-read.cjs.map +1 -0
  48. package/dist/hooks/customTypeLibrary-read.d.ts +3 -0
  49. package/dist/hooks/customTypeLibrary-read.js +34 -0
  50. package/dist/hooks/customTypeLibrary-read.js.map +1 -0
  51. package/dist/hooks/slice-asset-delete.cjs +29 -0
  52. package/dist/hooks/slice-asset-delete.cjs.map +1 -0
  53. package/dist/hooks/slice-asset-delete.d.ts +3 -0
  54. package/dist/hooks/slice-asset-delete.js +10 -0
  55. package/dist/hooks/slice-asset-delete.js.map +1 -0
  56. package/dist/hooks/slice-asset-read.cjs +32 -0
  57. package/dist/hooks/slice-asset-read.cjs.map +1 -0
  58. package/dist/hooks/slice-asset-read.d.ts +3 -0
  59. package/dist/hooks/slice-asset-read.js +13 -0
  60. package/dist/hooks/slice-asset-read.js.map +1 -0
  61. package/dist/hooks/slice-asset-update.cjs +32 -0
  62. package/dist/hooks/slice-asset-update.cjs.map +1 -0
  63. package/dist/hooks/slice-asset-update.d.ts +3 -0
  64. package/dist/hooks/slice-asset-update.js +12 -0
  65. package/dist/hooks/slice-asset-update.js.map +1 -0
  66. package/dist/hooks/slice-create.cjs +109 -0
  67. package/dist/hooks/slice-create.cjs.map +1 -0
  68. package/dist/hooks/slice-create.d.ts +3 -0
  69. package/dist/hooks/slice-create.js +89 -0
  70. package/dist/hooks/slice-create.js.map +1 -0
  71. package/dist/hooks/slice-delete.cjs +39 -0
  72. package/dist/hooks/slice-delete.cjs.map +1 -0
  73. package/dist/hooks/slice-delete.d.ts +3 -0
  74. package/dist/hooks/slice-delete.js +20 -0
  75. package/dist/hooks/slice-delete.js.map +1 -0
  76. package/dist/hooks/slice-read.cjs +49 -0
  77. package/dist/hooks/slice-read.cjs.map +1 -0
  78. package/dist/hooks/slice-read.d.ts +3 -0
  79. package/dist/hooks/slice-read.js +29 -0
  80. package/dist/hooks/slice-read.js.map +1 -0
  81. package/dist/hooks/slice-rename.cjs +45 -0
  82. package/dist/hooks/slice-rename.cjs.map +1 -0
  83. package/dist/hooks/slice-rename.d.ts +3 -0
  84. package/dist/hooks/slice-rename.js +26 -0
  85. package/dist/hooks/slice-rename.js.map +1 -0
  86. package/dist/hooks/slice-update.cjs +14 -0
  87. package/dist/hooks/slice-update.cjs.map +1 -0
  88. package/dist/hooks/slice-update.d.ts +3 -0
  89. package/dist/hooks/slice-update.js +14 -0
  90. package/dist/hooks/slice-update.js.map +1 -0
  91. package/dist/hooks/sliceLibrary-read.cjs +50 -0
  92. package/dist/hooks/sliceLibrary-read.cjs.map +1 -0
  93. package/dist/hooks/sliceLibrary-read.d.ts +3 -0
  94. package/dist/hooks/sliceLibrary-read.js +30 -0
  95. package/dist/hooks/sliceLibrary-read.js.map +1 -0
  96. package/dist/hooks/sliceSimulator-setup-read.cjs +217 -0
  97. package/dist/hooks/sliceSimulator-setup-read.cjs.map +1 -0
  98. package/dist/hooks/sliceSimulator-setup-read.d.ts +3 -0
  99. package/dist/hooks/sliceSimulator-setup-read.js +198 -0
  100. package/dist/hooks/sliceSimulator-setup-read.js.map +1 -0
  101. package/dist/hooks/snippet-read.cjs +96 -0
  102. package/dist/hooks/snippet-read.cjs.map +1 -0
  103. package/dist/hooks/snippet-read.d.ts +3 -0
  104. package/dist/hooks/snippet-read.js +77 -0
  105. package/dist/hooks/snippet-read.js.map +1 -0
  106. package/dist/index.cjs +6 -698
  107. package/dist/index.cjs.map +1 -1
  108. package/dist/index.d.ts +5 -45
  109. package/dist/index.js +6 -669
  110. package/dist/index.js.map +1 -1
  111. package/dist/lib/getJSOrTSXFileExtension.cjs +13 -0
  112. package/dist/lib/getJSOrTSXFileExtension.cjs.map +1 -0
  113. package/dist/lib/getJSOrTSXFileExtension.d.ts +2 -0
  114. package/dist/lib/getJSOrTSXFileExtension.js +13 -0
  115. package/dist/lib/getJSOrTSXFileExtension.js.map +1 -0
  116. package/dist/lib/isSharedSliceModel.cjs +27 -0
  117. package/dist/lib/isSharedSliceModel.cjs.map +1 -0
  118. package/dist/lib/isSharedSliceModel.d.ts +2 -0
  119. package/dist/lib/isSharedSliceModel.js +8 -0
  120. package/dist/lib/isSharedSliceModel.js.map +1 -0
  121. package/dist/lib/pascalCase.cjs +8 -0
  122. package/dist/lib/pascalCase.cjs.map +1 -0
  123. package/dist/lib/pascalCase.d.ts +8 -0
  124. package/dist/lib/pascalCase.js +8 -0
  125. package/dist/lib/pascalCase.js.map +1 -0
  126. package/dist/lib/readJSONFile.cjs +28 -0
  127. package/dist/lib/readJSONFile.cjs.map +1 -0
  128. package/dist/lib/readJSONFile.d.ts +1 -0
  129. package/dist/lib/readJSONFile.js +9 -0
  130. package/dist/lib/readJSONFile.js.map +1 -0
  131. package/dist/lib/rejectIfNecessary.cjs +10 -0
  132. package/dist/lib/rejectIfNecessary.cjs.map +1 -0
  133. package/dist/lib/rejectIfNecessary.d.ts +1 -0
  134. package/dist/lib/rejectIfNecessary.js +10 -0
  135. package/dist/lib/rejectIfNecessary.js.map +1 -0
  136. package/dist/lib/updateSliceModelFile.cjs +33 -0
  137. package/dist/lib/updateSliceModelFile.cjs.map +1 -0
  138. package/dist/lib/updateSliceModelFile.d.ts +9 -0
  139. package/dist/lib/updateSliceModelFile.js +14 -0
  140. package/dist/lib/updateSliceModelFile.js.map +1 -0
  141. package/dist/lib/upsertGlobalContentTypes.cjs +53 -0
  142. package/dist/lib/upsertGlobalContentTypes.cjs.map +1 -0
  143. package/dist/lib/upsertGlobalContentTypes.d.ts +12 -0
  144. package/dist/lib/upsertGlobalContentTypes.js +33 -0
  145. package/dist/lib/upsertGlobalContentTypes.js.map +1 -0
  146. package/dist/lib/upsertSliceLibraryIndexFile.cjs +47 -0
  147. package/dist/lib/upsertSliceLibraryIndexFile.cjs.map +1 -0
  148. package/dist/lib/upsertSliceLibraryIndexFile.d.ts +7 -0
  149. package/dist/lib/upsertSliceLibraryIndexFile.js +28 -0
  150. package/dist/lib/upsertSliceLibraryIndexFile.js.map +1 -0
  151. package/dist/package.cjs +5 -0
  152. package/dist/package.cjs.map +1 -0
  153. package/dist/package.js +5 -0
  154. package/dist/package.js.map +1 -0
  155. package/dist/plugin.cjs +56 -0
  156. package/dist/plugin.cjs.map +1 -0
  157. package/dist/plugin.d.ts +2 -0
  158. package/dist/plugin.js +56 -0
  159. package/dist/plugin.js.map +1 -0
  160. package/dist/types.d.ts +9 -0
  161. package/package.json +28 -23
  162. package/src/hooks/customType-asset-delete.ts +16 -0
  163. package/src/hooks/customType-asset-read.ts +20 -0
  164. package/src/hooks/customType-asset-update.ts +18 -0
  165. package/src/hooks/customType-create.ts +5 -20
  166. package/src/hooks/customType-delete.ts +6 -2
  167. package/src/hooks/customType-read.ts +5 -1
  168. package/src/hooks/customType-rename.ts +3 -0
  169. package/src/hooks/{customType-library-read.ts → customTypeLibrary-read.ts} +16 -4
  170. package/src/hooks/slice-asset-delete.ts +19 -0
  171. package/src/hooks/slice-asset-read.ts +23 -0
  172. package/src/hooks/slice-asset-update.ts +21 -0
  173. package/src/hooks/slice-create.ts +27 -52
  174. package/src/hooks/slice-delete.ts +11 -21
  175. package/src/hooks/slice-read.ts +38 -8
  176. package/src/hooks/slice-rename.ts +44 -0
  177. package/src/hooks/slice-update.ts +9 -48
  178. package/src/hooks/sliceLibrary-read.ts +44 -0
  179. package/src/lib/isSharedSliceModel.ts +14 -0
  180. package/src/lib/rejectIfNecessary.ts +16 -0
  181. package/src/lib/updateSliceModelFile.ts +32 -0
  182. package/src/lib/upsertGlobalContentTypes.ts +59 -0
  183. package/src/lib/{buildSliceLibraryIndexFileContents.ts → upsertSliceLibraryIndexFile.ts} +14 -9
  184. package/src/plugin.ts +24 -8
  185. package/src/hooks/slice-library-read.ts +0 -51
@@ -3,14 +3,15 @@ import type {
3
3
  SliceCreateHookData,
4
4
  SliceMachineContext,
5
5
  } from "@slicemachine/plugin-kit";
6
- import { generateTypes } from "prismic-ts-codegen";
7
6
  import { stripIndent } from "common-tags";
8
7
  import * as fs from "node:fs/promises";
9
8
  import * as path from "node:path";
10
9
 
11
- import { buildSliceLibraryIndexFileContents } from "../lib/buildSliceLibraryIndexFileContents";
10
+ import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
12
11
  import { getJSOrTSXFileExtension } from "../lib/getJSOrTSXFileExtension";
13
12
  import { pascalCase } from "../lib/pascalCase";
13
+ import { rejectIfNecessary } from "../lib/rejectIfNecessary";
14
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
14
15
 
15
16
  import type { PluginOptions } from "../types";
16
17
 
@@ -34,24 +35,24 @@ const createModelFile = async ({ dir, data, helpers, options }: Args) => {
34
35
  const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
35
36
  const filePath = path.join(dir, `index.${getJSOrTSXFileExtension(options)}`);
36
37
  const model = data.model;
37
- const pascalID = pascalCase(model.id);
38
+ const pascalName = pascalCase(model.name);
38
39
 
39
40
  let contents: string;
40
41
 
41
42
  if (options.typescript) {
42
43
  contents = stripIndent`
43
44
  import { SliceComponentProps } from "@prismicio/react";
44
- import { ${pascalID}Slice } from "./types";
45
+ import { ${pascalName}Slice } from "./types";
45
46
 
46
47
  /**
47
- * Props for \`${pascalID}\`.
48
+ * Props for \`${pascalName}\`.
48
49
  */
49
- export type ${pascalID}Props = SliceComponentProps<${pascalID}Slice>;
50
+ export type ${pascalName}Props = SliceComponentProps<${pascalName}Slice>;
50
51
 
51
52
  /**
52
53
  * Component for "${model.name}" Slices.
53
54
  */
54
- const ${pascalID} = ({ slice }: ${pascalID}Props): React.Element => {
55
+ const ${pascalName} = ({ slice }: ${pascalName}Props): React.Element => {
55
56
  return (
56
57
  <section
57
58
  data-slice-type={slice.slice_type}
@@ -62,16 +63,16 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
62
63
  );
63
64
  };
64
65
 
65
- export default ${pascalID}
66
+ export default ${pascalName}
66
67
  `;
67
68
  } else {
68
69
  contents = stripIndent`
69
70
  /**
70
- * @typedef {import("./types").${pascalID}Slice} ${pascalID}Slice
71
- * @typedef {import("@prismicio/react").SliceComponentProps<${pascalID}Slice>} ${pascalID}Props
72
- * @param {${pascalID}Props}
71
+ * @typedef {import("./types").${pascalName}Slice} ${pascalName}Slice
72
+ * @typedef {import("@prismicio/react").SliceComponentProps<${pascalName}Slice>} ${pascalName}Props
73
+ * @param {${pascalName}Props}
73
74
  */
74
- const ${pascalID} = ({ slice }) => {
75
+ const ${pascalName} = ({ slice }) => {
75
76
  return (
76
77
  <section
77
78
  data-slice-type={slice.slice_type}
@@ -82,7 +83,7 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
82
83
  );
83
84
  };
84
85
 
85
- export default ${pascalID};
86
+ export default ${pascalName};
86
87
  `;
87
88
  }
88
89
 
@@ -93,54 +94,28 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
93
94
  await fs.writeFile(filePath, contents);
94
95
  };
95
96
 
96
- const createTypesFile = async ({ dir, data, helpers, options }: Args) => {
97
- const filePath = path.join(dir, "types.ts");
98
-
99
- let contents = generateTypes({
100
- sharedSliceModels: [data.model],
101
- });
102
-
103
- if (options.format) {
104
- contents = await helpers.format(contents, filePath);
105
- }
106
-
107
- await fs.writeFile(filePath, contents);
108
- };
109
-
110
- const upsertSliceLibraryIndexFile = async ({
111
- data,
112
- actions,
113
- helpers,
114
- project,
115
- options,
116
- }: Omit<Args, "dir">) => {
117
- const { filePath, contents } = await buildSliceLibraryIndexFileContents({
118
- libraryID: data.libraryID,
119
- actions,
120
- helpers,
121
- project,
122
- options,
123
- });
124
-
125
- await fs.writeFile(filePath, contents);
126
- };
127
-
128
97
  export const sliceCreate: SliceCreateHook<PluginOptions> = async (
129
98
  data,
130
99
  context,
131
100
  ) => {
132
101
  const dir = context.helpers.joinPathFromRoot(
133
102
  data.libraryID,
134
- pascalCase(data.model.id),
103
+ pascalCase(data.model.name),
135
104
  );
136
105
 
137
106
  await fs.mkdir(dir, { recursive: true });
138
107
 
139
- await Promise.allSettled([
140
- createModelFile({ dir, data, ...context }),
141
- createComponentFile({ dir, data, ...context }),
142
- createTypesFile({ dir, data, ...context }),
143
- ]);
108
+ rejectIfNecessary(
109
+ await Promise.allSettled([
110
+ createModelFile({ dir, data, ...context }),
111
+ createComponentFile({ dir, data, ...context }),
112
+ ]),
113
+ );
144
114
 
145
- await upsertSliceLibraryIndexFile({ data, ...context });
115
+ rejectIfNecessary(
116
+ await Promise.allSettled([
117
+ upsertGlobalContentTypes(context),
118
+ upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context }),
119
+ ]),
120
+ );
146
121
  };
@@ -5,8 +5,10 @@ import type {
5
5
  } from "@slicemachine/plugin-kit";
6
6
  import * as fs from "node:fs/promises";
7
7
 
8
- import { buildSliceLibraryIndexFileContents } from "../lib/buildSliceLibraryIndexFileContents";
9
8
  import { pascalCase } from "../lib/pascalCase";
9
+ import { rejectIfNecessary } from "../lib/rejectIfNecessary";
10
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
11
+ import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
10
12
 
11
13
  import type { PluginOptions } from "../types";
12
14
 
@@ -17,34 +19,22 @@ type Args = {
17
19
  const deleteSliceDir = async ({ data, helpers }: Args) => {
18
20
  const dir = helpers.joinPathFromRoot(
19
21
  data.libraryID,
20
- pascalCase(data.model.id),
22
+ pascalCase(data.model.name),
21
23
  );
22
24
 
23
25
  await fs.rm(dir, { recursive: true });
24
26
  };
25
27
 
26
- const updateSliceLibraryIndexFile = async ({
27
- data,
28
- actions,
29
- helpers,
30
- project,
31
- options,
32
- }: Args) => {
33
- const { filePath, contents } = await buildSliceLibraryIndexFileContents({
34
- libraryID: data.libraryID,
35
- actions,
36
- helpers,
37
- project,
38
- options,
39
- });
40
-
41
- await fs.writeFile(filePath, contents);
42
- };
43
-
44
28
  export const sliceDelete: SliceDeleteHook<PluginOptions> = async (
45
29
  data,
46
30
  context,
47
31
  ) => {
48
32
  await deleteSliceDir({ data, ...context });
49
- await updateSliceLibraryIndexFile({ data, ...context });
33
+
34
+ rejectIfNecessary(
35
+ await Promise.allSettled([
36
+ upsertGlobalContentTypes(context),
37
+ upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context }),
38
+ ]),
39
+ );
50
40
  };
@@ -1,8 +1,9 @@
1
1
  import type { SliceReadHook } from "@slicemachine/plugin-kit";
2
- import * as prismicT from "@prismicio/types";
2
+ import * as fs from "node:fs/promises";
3
+ import * as path from "node:path";
3
4
 
5
+ import { isSharedSliceModel } from "../lib/isSharedSliceModel";
4
6
  import { readJSONFile } from "../lib/readJSONFile";
5
- import { pascalCase } from "../lib/pascalCase";
6
7
 
7
8
  import type { PluginOptions } from "../types";
8
9
 
@@ -10,11 +11,40 @@ export const sliceRead: SliceReadHook<PluginOptions> = async (
10
11
  data,
11
12
  { helpers },
12
13
  ) => {
13
- const filePath = helpers.joinPathFromRoot(
14
- data.libraryID,
15
- pascalCase(data.sliceID),
16
- "model.json",
17
- );
14
+ const libraryDir = helpers.joinPathFromRoot(data.libraryID);
18
15
 
19
- return await readJSONFile<prismicT.SharedSliceModel>(filePath);
16
+ // Ensure the directory exists.
17
+ await fs.mkdir(libraryDir, { recursive: true });
18
+
19
+ const childDirs = await fs.readdir(libraryDir, { withFileTypes: true });
20
+
21
+ // Find the first matching model.
22
+ const [model] = (
23
+ await Promise.all(
24
+ childDirs.map(async (childDir) => {
25
+ if (childDir.isDirectory()) {
26
+ const modelPath = path.join(libraryDir, childDir.name, "model.json");
27
+
28
+ const modelContents = await readJSONFile(modelPath);
29
+
30
+ if (
31
+ isSharedSliceModel(modelContents) &&
32
+ modelContents.id === data.sliceID
33
+ ) {
34
+ return modelContents;
35
+ }
36
+ }
37
+ }),
38
+ )
39
+ ).filter((model): model is NonNullable<typeof model> => Boolean(model));
40
+
41
+ if (model) {
42
+ return {
43
+ model,
44
+ };
45
+ } else {
46
+ throw new Error(
47
+ `Did not find a Slice model with ID "${data.sliceID}" in the "${data.libraryID}" Slice Library.`,
48
+ );
49
+ }
20
50
  };
@@ -0,0 +1,44 @@
1
+ import type { SliceRenameHook } from "@slicemachine/plugin-kit";
2
+ import * as fse from "fs-extra";
3
+
4
+ import { pascalCase } from "../lib/pascalCase";
5
+ import { rejectIfNecessary } from "../lib/rejectIfNecessary";
6
+ import { updateSliceModelFile } from "../lib/updateSliceModelFile";
7
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
8
+ import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
9
+
10
+ import type { PluginOptions } from "../types";
11
+
12
+ export const sliceRename: SliceRenameHook<PluginOptions> = async (
13
+ data,
14
+ context,
15
+ ) => {
16
+ const { model: currentModel } = await context.actions.readSliceModel({
17
+ libraryID: data.libraryID,
18
+ sliceID: data.model.id,
19
+ });
20
+
21
+ await fse.move(
22
+ context.helpers.joinPathFromRoot(
23
+ data.libraryID,
24
+ pascalCase(currentModel.name),
25
+ ),
26
+ context.helpers.joinPathFromRoot(
27
+ data.libraryID,
28
+ pascalCase(data.model.name),
29
+ ),
30
+ );
31
+
32
+ await updateSliceModelFile({
33
+ libraryID: data.libraryID,
34
+ model: data.model,
35
+ ...context,
36
+ });
37
+
38
+ rejectIfNecessary(
39
+ await Promise.allSettled([
40
+ upsertGlobalContentTypes(context),
41
+ upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context }),
42
+ ]),
43
+ );
44
+ };
@@ -1,58 +1,19 @@
1
- import type {
2
- SliceMachineContext,
3
- SliceUpdateHook,
4
- SliceUpdateHookData,
5
- } from "@slicemachine/plugin-kit";
6
- import { generateTypes } from "prismic-ts-codegen";
7
- import * as fs from "node:fs/promises";
8
- import * as path from "node:path";
1
+ import type { SliceUpdateHook } from "@slicemachine/plugin-kit";
9
2
 
10
- import { pascalCase } from "../lib/pascalCase";
3
+ import { updateSliceModelFile } from "../lib/updateSliceModelFile";
4
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
11
5
 
12
6
  import type { PluginOptions } from "../types";
13
7
 
14
- type Args = {
15
- dir: string;
16
- data: SliceUpdateHookData;
17
- } & SliceMachineContext<PluginOptions>;
18
-
19
- const updateModelFile = async ({ dir, data, helpers, options }: Args) => {
20
- const filePath = path.join(dir, "model.json");
21
-
22
- let contents = JSON.stringify(data.model);
23
-
24
- if (options.format) {
25
- contents = await helpers.format(contents, filePath);
26
- }
27
-
28
- await fs.writeFile(filePath, contents);
29
- };
30
-
31
- const updateTypesFile = async ({ dir, data, helpers, options }: Args) => {
32
- const filePath = path.join(dir, "types.ts");
33
-
34
- let contents = generateTypes({
35
- sharedSliceModels: [data.model],
36
- });
37
-
38
- if (options.format) {
39
- contents = await helpers.format(contents, filePath);
40
- }
41
-
42
- await fs.writeFile(filePath, contents);
43
- };
44
-
45
8
  export const sliceUpdate: SliceUpdateHook<PluginOptions> = async (
46
9
  data,
47
10
  context,
48
11
  ) => {
49
- const dir = context.helpers.joinPathFromRoot(
50
- data.libraryID,
51
- pascalCase(data.model.id),
52
- );
12
+ await updateSliceModelFile({
13
+ libraryID: data.libraryID,
14
+ model: data.model,
15
+ ...context,
16
+ });
53
17
 
54
- await Promise.allSettled([
55
- updateModelFile({ dir, data, ...context }),
56
- updateTypesFile({ dir, data, ...context }),
57
- ]);
18
+ await upsertGlobalContentTypes(context);
58
19
  };
@@ -0,0 +1,44 @@
1
+ import type { SliceLibraryReadHook } from "@slicemachine/plugin-kit";
2
+ import * as fs from "node:fs/promises";
3
+ import * as path from "node:path";
4
+
5
+ import { isSharedSliceModel } from "../lib/isSharedSliceModel";
6
+ import { readJSONFile } from "../lib/readJSONFile";
7
+
8
+ import type { PluginOptions } from "../types";
9
+
10
+ export const sliceLibraryRead: SliceLibraryReadHook<PluginOptions> = async (
11
+ data,
12
+ { helpers },
13
+ ) => {
14
+ const dir = helpers.joinPathFromRoot(data.libraryID);
15
+
16
+ // Ensure the directory exists.
17
+ await fs.mkdir(dir, { recursive: true });
18
+
19
+ const childDirs = await fs.readdir(dir, { withFileTypes: true });
20
+
21
+ const sliceIDs: string[] = [];
22
+ await Promise.all(
23
+ childDirs.map(async (childDir) => {
24
+ if (childDir.isDirectory()) {
25
+ const modelPath = path.join(dir, childDir.name, "model.json");
26
+
27
+ try {
28
+ const modelContents = await readJSONFile(modelPath);
29
+
30
+ if (isSharedSliceModel(modelContents)) {
31
+ sliceIDs.push(modelContents.id);
32
+ }
33
+ } catch {
34
+ // noop
35
+ }
36
+ }
37
+ }),
38
+ );
39
+
40
+ return {
41
+ id: data.libraryID,
42
+ sliceIDs: sliceIDs.sort(),
43
+ };
44
+ };
@@ -0,0 +1,14 @@
1
+ import * as prismicT from "@prismicio/types";
2
+
3
+ export const isSharedSliceModel = (
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
5
+ input: any,
6
+ ): input is prismicT.SharedSliceModel => {
7
+ return (
8
+ typeof input === "object" &&
9
+ input !== null &&
10
+ "type" in input &&
11
+ input.type === prismicT.CustomTypeModelSliceType.SharedSlice &&
12
+ typeof input.id === "string"
13
+ );
14
+ };
@@ -0,0 +1,16 @@
1
+ export const rejectIfNecessary = <
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ TPromiseSettledResults extends readonly PromiseSettledResult<any>[],
4
+ >(
5
+ promiseSettledResults: TPromiseSettledResults,
6
+ ): void => {
7
+ const rejectedReasons = promiseSettledResults
8
+ .filter(
9
+ (result): result is PromiseRejectedResult => result.status === "rejected",
10
+ )
11
+ .map((result) => result.reason);
12
+
13
+ if (rejectedReasons.length > 0) {
14
+ throw rejectedReasons[0];
15
+ }
16
+ };
@@ -0,0 +1,32 @@
1
+ import { SharedSliceModel } from "@prismicio/types";
2
+ import { SliceMachineContext } from "@slicemachine/plugin-kit";
3
+ import * as fs from "node:fs/promises";
4
+
5
+ import type { PluginOptions } from "../types";
6
+ import { pascalCase } from "./pascalCase";
7
+
8
+ type UpdateSliceModelFileArgs = {
9
+ libraryID: string;
10
+ model: SharedSliceModel;
11
+ } & SliceMachineContext<PluginOptions>;
12
+
13
+ export const updateSliceModelFile = async ({
14
+ libraryID,
15
+ model,
16
+ helpers,
17
+ options,
18
+ }: UpdateSliceModelFileArgs): Promise<void> => {
19
+ const filePath = helpers.joinPathFromRoot(
20
+ libraryID,
21
+ pascalCase(model.name),
22
+ "model.json",
23
+ );
24
+
25
+ let contents = JSON.stringify(model);
26
+
27
+ if (options.format) {
28
+ contents = await helpers.format(contents, filePath);
29
+ }
30
+
31
+ await fs.writeFile(filePath, contents);
32
+ };
@@ -0,0 +1,59 @@
1
+ import type { SliceMachineContext } from "@slicemachine/plugin-kit";
2
+ import { generateTypes } from "prismic-ts-codegen";
3
+ import * as fs from "node:fs/promises";
4
+ import * as path from "node:path";
5
+
6
+ import type { PluginOptions } from "../types";
7
+
8
+ /**
9
+ * Arguments for `upsertGlobalContentTypes()`.
10
+ */
11
+ type UpsertGlobalTypesArgs = Pick<
12
+ SliceMachineContext<PluginOptions>,
13
+ "actions" | "helpers" | "options"
14
+ >;
15
+
16
+ /**
17
+ * Creates a globally accessible TypeScript file containing types representing
18
+ * the Prismic repository's content.
19
+ */
20
+ export const upsertGlobalContentTypes = async ({
21
+ actions,
22
+ helpers,
23
+ options,
24
+ }: UpsertGlobalTypesArgs): Promise<void> => {
25
+ const filePath = helpers.joinPathFromRoot(".slicemachine", "prismicio.d.ts");
26
+
27
+ const [customTypeModelDescriptors, sharedSliceModelDescriptors] =
28
+ await Promise.all([
29
+ actions.readAllCustomTypeModels(),
30
+ actions.readAllSliceModels(),
31
+ ]);
32
+
33
+ const customTypeModels = customTypeModelDescriptors.map(
34
+ (customTypeModelDescriptors) => {
35
+ return customTypeModelDescriptors.model;
36
+ },
37
+ );
38
+ const sharedSliceModels = sharedSliceModelDescriptors.map(
39
+ (sharedSliceModelDescriptors) => {
40
+ return sharedSliceModelDescriptors.model;
41
+ },
42
+ );
43
+
44
+ let contents = generateTypes({
45
+ customTypeModels,
46
+ sharedSliceModels,
47
+ clientIntegration: {
48
+ includeCreateClientInterface: true,
49
+ includeContentNamespace: true,
50
+ },
51
+ });
52
+
53
+ if (options.format) {
54
+ contents = await helpers.format(contents, filePath);
55
+ }
56
+
57
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
58
+ await fs.writeFile(filePath, contents);
59
+ };
@@ -1,22 +1,24 @@
1
1
  import { SliceMachineContext } from "@slicemachine/plugin-kit";
2
2
  import { stripIndent } from "common-tags";
3
+ import * as fs from "node:fs/promises";
3
4
 
4
5
  import { PluginOptions } from "../types";
5
6
 
6
7
  import { pascalCase } from "./pascalCase";
7
8
 
8
- type BuildSliceLibraryIndexFileContentsArgs = {
9
+ type UpsertSliceLibraryIndexFileArgs = {
9
10
  libraryID: string;
10
11
  } & SliceMachineContext<PluginOptions>;
11
12
 
12
- export const buildSliceLibraryIndexFileContents = async (
13
- args: BuildSliceLibraryIndexFileContentsArgs,
14
- ): Promise<{ filePath: string; contents: string }> => {
13
+ export const upsertSliceLibraryIndexFile = async (
14
+ args: UpsertSliceLibraryIndexFileArgs,
15
+ ): Promise<void> => {
15
16
  const filePath = args.helpers.joinPathFromRoot(
16
17
  args.libraryID,
17
18
  args.options.typescript ? "index.ts" : "index.js",
18
19
  );
19
- const sliceLibrary = await args.actions.readSliceLibrary({
20
+
21
+ const slices = await args.actions.readAllSliceModelsForLibrary({
20
22
  libraryID: args.libraryID,
21
23
  });
22
24
 
@@ -24,9 +26,12 @@ export const buildSliceLibraryIndexFileContents = async (
24
26
  import dynamic from 'next/dynamic'
25
27
 
26
28
  export const components = {
27
- ${sliceLibrary.sliceIDs.map(
28
- (id) => `${id}: dynamic(() => import('./${pascalCase(id)}')),`,
29
- )}
29
+ ${slices.map((slice) => {
30
+ const id = slice.model.id;
31
+ const fileName = pascalCase(slice.model.name);
32
+
33
+ return `${id}: dynamic(() => import('./${fileName}')),`;
34
+ })}
30
35
  }
31
36
  `;
32
37
 
@@ -34,5 +39,5 @@ export const buildSliceLibraryIndexFileContents = async (
34
39
  contents = await args.helpers.format(contents, filePath);
35
40
  }
36
41
 
37
- return { filePath, contents };
42
+ await fs.writeFile(filePath, contents);
38
43
  };
package/src/plugin.ts CHANGED
@@ -3,18 +3,26 @@ import { defineSliceMachinePlugin } from "@slicemachine/plugin-kit";
3
3
  import { name as pkgName } from "../package.json";
4
4
  import { PluginOptions } from "./types";
5
5
 
6
- import { sliceCreate } from "./hooks/slice-create";
7
- import { sliceUpdate } from "./hooks/slice-update";
8
- import { sliceDelete } from "./hooks/slice-delete";
9
- import { sliceRead } from "./hooks/slice-read";
10
- import { sliceLibraryRead } from "./hooks/slice-library-read";
6
+ import { customTypeAssetDelete } from "./hooks/customType-asset-delete";
7
+ import { customTypeAssetRead } from "./hooks/customType-asset-read";
8
+ import { customTypeAssetUpdate } from "./hooks/customType-asset-update";
11
9
  import { customTypeCreate } from "./hooks/customType-create";
12
- import { customTypeUpdate } from "./hooks/customType-update";
13
10
  import { customTypeDelete } from "./hooks/customType-delete";
11
+ import { customTypeLibraryRead } from "./hooks/customTypeLibrary-read";
14
12
  import { customTypeRead } from "./hooks/customType-read";
15
- import { customTypeLibraryRead } from "./hooks/customType-library-read";
16
- import { snippetRead } from "./hooks/snippet-read";
13
+ import { customTypeRename } from "./hooks/customType-rename";
14
+ import { customTypeUpdate } from "./hooks/customType-update";
15
+ import { sliceAssetDelete } from "./hooks/slice-asset-delete";
16
+ import { sliceAssetRead } from "./hooks/slice-asset-read";
17
+ import { sliceAssetUpdate } from "./hooks/slice-asset-update";
18
+ import { sliceCreate } from "./hooks/slice-create";
19
+ import { sliceDelete } from "./hooks/slice-delete";
20
+ import { sliceLibraryRead } from "./hooks/sliceLibrary-read";
21
+ import { sliceRead } from "./hooks/slice-read";
22
+ import { sliceRename } from "./hooks/slice-rename";
17
23
  import { sliceSimulatorSetupRead } from "./hooks/sliceSimulator-setup-read";
24
+ import { sliceUpdate } from "./hooks/slice-update";
25
+ import { snippetRead } from "./hooks/snippet-read";
18
26
 
19
27
  export const plugin = defineSliceMachinePlugin<PluginOptions>({
20
28
  meta: {
@@ -26,14 +34,22 @@ export const plugin = defineSliceMachinePlugin<PluginOptions>({
26
34
  setup({ hook }) {
27
35
  hook("slice:create", sliceCreate);
28
36
  hook("slice:update", sliceUpdate);
37
+ hook("slice:rename", sliceRename);
29
38
  hook("slice:delete", sliceDelete);
30
39
  hook("slice:read", sliceRead);
40
+ hook("slice:asset:update", sliceAssetUpdate);
41
+ hook("slice:asset:delete", sliceAssetDelete);
42
+ hook("slice:asset:read", sliceAssetRead);
31
43
  hook("slice-library:read", sliceLibraryRead);
32
44
 
33
45
  hook("custom-type:create", customTypeCreate);
34
46
  hook("custom-type:update", customTypeUpdate);
47
+ hook("custom-type:rename", customTypeRename);
35
48
  hook("custom-type:delete", customTypeDelete);
36
49
  hook("custom-type:read", customTypeRead);
50
+ hook("custom-type:asset:update", customTypeAssetUpdate);
51
+ hook("custom-type:asset:delete", customTypeAssetDelete);
52
+ hook("custom-type:asset:read", customTypeAssetRead);
37
53
  hook("custom-type-library:read", customTypeLibraryRead);
38
54
 
39
55
  hook("snippet:read", snippetRead);