@slicemachine/adapter-next 0.0.1 → 0.0.2

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 (159) 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-update.cjs +6 -0
  37. package/dist/hooks/customType-update.cjs.map +1 -0
  38. package/dist/hooks/customType-update.d.ts +1 -0
  39. package/dist/hooks/customType-update.js +6 -0
  40. package/dist/hooks/customType-update.js.map +1 -0
  41. package/dist/hooks/customTypeLibrary-read.cjs +46 -0
  42. package/dist/hooks/customTypeLibrary-read.cjs.map +1 -0
  43. package/dist/hooks/customTypeLibrary-read.d.ts +3 -0
  44. package/dist/hooks/customTypeLibrary-read.js +26 -0
  45. package/dist/hooks/customTypeLibrary-read.js.map +1 -0
  46. package/dist/hooks/slice-asset-delete.cjs +29 -0
  47. package/dist/hooks/slice-asset-delete.cjs.map +1 -0
  48. package/dist/hooks/slice-asset-delete.d.ts +3 -0
  49. package/dist/hooks/slice-asset-delete.js +10 -0
  50. package/dist/hooks/slice-asset-delete.js.map +1 -0
  51. package/dist/hooks/slice-asset-read.cjs +32 -0
  52. package/dist/hooks/slice-asset-read.cjs.map +1 -0
  53. package/dist/hooks/slice-asset-read.d.ts +3 -0
  54. package/dist/hooks/slice-asset-read.js +13 -0
  55. package/dist/hooks/slice-asset-read.js.map +1 -0
  56. package/dist/hooks/slice-asset-update.cjs +32 -0
  57. package/dist/hooks/slice-asset-update.cjs.map +1 -0
  58. package/dist/hooks/slice-asset-update.d.ts +3 -0
  59. package/dist/hooks/slice-asset-update.js +12 -0
  60. package/dist/hooks/slice-asset-update.js.map +1 -0
  61. package/dist/hooks/slice-create.cjs +119 -0
  62. package/dist/hooks/slice-create.cjs.map +1 -0
  63. package/dist/hooks/slice-create.d.ts +3 -0
  64. package/dist/hooks/slice-create.js +99 -0
  65. package/dist/hooks/slice-create.js.map +1 -0
  66. package/dist/hooks/slice-delete.cjs +49 -0
  67. package/dist/hooks/slice-delete.cjs.map +1 -0
  68. package/dist/hooks/slice-delete.d.ts +3 -0
  69. package/dist/hooks/slice-delete.js +30 -0
  70. package/dist/hooks/slice-delete.js.map +1 -0
  71. package/dist/hooks/slice-read.cjs +13 -0
  72. package/dist/hooks/slice-read.cjs.map +1 -0
  73. package/dist/hooks/slice-read.d.ts +3 -0
  74. package/dist/hooks/slice-read.js +13 -0
  75. package/dist/hooks/slice-read.js.map +1 -0
  76. package/dist/hooks/slice-update.cjs +41 -0
  77. package/dist/hooks/slice-update.cjs.map +1 -0
  78. package/dist/hooks/slice-update.d.ts +3 -0
  79. package/dist/hooks/slice-update.js +21 -0
  80. package/dist/hooks/slice-update.js.map +1 -0
  81. package/dist/hooks/sliceLibrary-read.cjs +52 -0
  82. package/dist/hooks/sliceLibrary-read.cjs.map +1 -0
  83. package/dist/hooks/sliceLibrary-read.d.ts +3 -0
  84. package/dist/hooks/sliceLibrary-read.js +31 -0
  85. package/dist/hooks/sliceLibrary-read.js.map +1 -0
  86. package/dist/hooks/sliceSimulator-setup-read.cjs +217 -0
  87. package/dist/hooks/sliceSimulator-setup-read.cjs.map +1 -0
  88. package/dist/hooks/sliceSimulator-setup-read.d.ts +3 -0
  89. package/dist/hooks/sliceSimulator-setup-read.js +198 -0
  90. package/dist/hooks/sliceSimulator-setup-read.js.map +1 -0
  91. package/dist/hooks/snippet-read.cjs +96 -0
  92. package/dist/hooks/snippet-read.cjs.map +1 -0
  93. package/dist/hooks/snippet-read.d.ts +3 -0
  94. package/dist/hooks/snippet-read.js +77 -0
  95. package/dist/hooks/snippet-read.js.map +1 -0
  96. package/dist/index.cjs +6 -698
  97. package/dist/index.cjs.map +1 -1
  98. package/dist/index.d.ts +5 -45
  99. package/dist/index.js +6 -669
  100. package/dist/index.js.map +1 -1
  101. package/dist/lib/buildSliceLibraryIndexFileContents.cjs +23 -0
  102. package/dist/lib/buildSliceLibraryIndexFileContents.cjs.map +1 -0
  103. package/dist/lib/buildSliceLibraryIndexFileContents.d.ts +10 -0
  104. package/dist/lib/buildSliceLibraryIndexFileContents.js +23 -0
  105. package/dist/lib/buildSliceLibraryIndexFileContents.js.map +1 -0
  106. package/dist/lib/getJSOrTSXFileExtension.cjs +13 -0
  107. package/dist/lib/getJSOrTSXFileExtension.cjs.map +1 -0
  108. package/dist/lib/getJSOrTSXFileExtension.d.ts +2 -0
  109. package/dist/lib/getJSOrTSXFileExtension.js +13 -0
  110. package/dist/lib/getJSOrTSXFileExtension.js.map +1 -0
  111. package/dist/lib/pascalCase.cjs +8 -0
  112. package/dist/lib/pascalCase.cjs.map +1 -0
  113. package/dist/lib/pascalCase.d.ts +8 -0
  114. package/dist/lib/pascalCase.js +8 -0
  115. package/dist/lib/pascalCase.js.map +1 -0
  116. package/dist/lib/readJSONFile.cjs +28 -0
  117. package/dist/lib/readJSONFile.cjs.map +1 -0
  118. package/dist/lib/readJSONFile.d.ts +1 -0
  119. package/dist/lib/readJSONFile.js +9 -0
  120. package/dist/lib/readJSONFile.js.map +1 -0
  121. package/dist/lib/rejectIfNecessary.cjs +10 -0
  122. package/dist/lib/rejectIfNecessary.cjs.map +1 -0
  123. package/dist/lib/rejectIfNecessary.d.ts +1 -0
  124. package/dist/lib/rejectIfNecessary.js +10 -0
  125. package/dist/lib/rejectIfNecessary.js.map +1 -0
  126. package/dist/lib/upsertGlobalContentTypes.cjs +53 -0
  127. package/dist/lib/upsertGlobalContentTypes.cjs.map +1 -0
  128. package/dist/lib/upsertGlobalContentTypes.d.ts +12 -0
  129. package/dist/lib/upsertGlobalContentTypes.js +33 -0
  130. package/dist/lib/upsertGlobalContentTypes.js.map +1 -0
  131. package/dist/package.cjs +5 -0
  132. package/dist/package.cjs.map +1 -0
  133. package/dist/package.js +5 -0
  134. package/dist/package.js.map +1 -0
  135. package/dist/plugin.cjs +52 -0
  136. package/dist/plugin.cjs.map +1 -0
  137. package/dist/plugin.d.ts +2 -0
  138. package/dist/plugin.js +52 -0
  139. package/dist/plugin.js.map +1 -0
  140. package/dist/types.d.ts +9 -0
  141. package/package.json +26 -23
  142. package/src/hooks/customType-asset-delete.ts +16 -0
  143. package/src/hooks/customType-asset-read.ts +20 -0
  144. package/src/hooks/customType-asset-update.ts +18 -0
  145. package/src/hooks/customType-create.ts +5 -20
  146. package/src/hooks/customType-delete.ts +6 -2
  147. package/src/hooks/customType-read.ts +5 -1
  148. package/src/hooks/{customType-library-read.ts → customTypeLibrary-read.ts} +3 -0
  149. package/src/hooks/slice-asset-delete.ts +19 -0
  150. package/src/hooks/slice-asset-read.ts +23 -0
  151. package/src/hooks/slice-asset-update.ts +21 -0
  152. package/src/hooks/slice-create.ts +14 -21
  153. package/src/hooks/slice-delete.ts +9 -1
  154. package/src/hooks/slice-read.ts +5 -1
  155. package/src/hooks/slice-update.ts +4 -19
  156. package/src/hooks/{slice-library-read.ts → sliceLibrary-read.ts} +3 -0
  157. package/src/lib/rejectIfNecessary.ts +16 -0
  158. package/src/lib/upsertGlobalContentTypes.ts +59 -0
  159. package/src/plugin.ts +20 -8
package/dist/plugin.js ADDED
@@ -0,0 +1,52 @@
1
+ import { defineSliceMachinePlugin } from "@slicemachine/plugin-kit";
2
+ import { name } from "./package.js";
3
+ import { customTypeAssetDelete } from "./hooks/customType-asset-delete.js";
4
+ import { customTypeAssetRead } from "./hooks/customType-asset-read.js";
5
+ import { customTypeAssetUpdate } from "./hooks/customType-asset-update.js";
6
+ import { customTypeCreate } from "./hooks/customType-create.js";
7
+ import { customTypeDelete } from "./hooks/customType-delete.js";
8
+ import { customTypeLibraryRead } from "./hooks/customTypeLibrary-read.js";
9
+ import { customTypeRead } from "./hooks/customType-read.js";
10
+ import { customTypeUpdate } from "./hooks/customType-update.js";
11
+ import { sliceAssetDelete } from "./hooks/slice-asset-delete.js";
12
+ import { sliceAssetRead } from "./hooks/slice-asset-read.js";
13
+ import { sliceAssetUpdate } from "./hooks/slice-asset-update.js";
14
+ import { sliceCreate } from "./hooks/slice-create.js";
15
+ import { sliceDelete } from "./hooks/slice-delete.js";
16
+ import { sliceLibraryRead } from "./hooks/sliceLibrary-read.js";
17
+ import { sliceRead } from "./hooks/slice-read.js";
18
+ import { sliceSimulatorSetupRead } from "./hooks/sliceSimulator-setup-read.js";
19
+ import { sliceUpdate } from "./hooks/slice-update.js";
20
+ import { snippetRead } from "./hooks/snippet-read.js";
21
+ const plugin = defineSliceMachinePlugin({
22
+ meta: {
23
+ name
24
+ },
25
+ defaultOptions: {
26
+ format: true
27
+ },
28
+ setup({ hook }) {
29
+ hook("slice:create", sliceCreate);
30
+ hook("slice:update", sliceUpdate);
31
+ hook("slice:delete", sliceDelete);
32
+ hook("slice:read", sliceRead);
33
+ hook("slice:asset:update", sliceAssetUpdate);
34
+ hook("slice:asset:delete", sliceAssetDelete);
35
+ hook("slice:asset:read", sliceAssetRead);
36
+ hook("slice-library:read", sliceLibraryRead);
37
+ hook("custom-type:create", customTypeCreate);
38
+ hook("custom-type:update", customTypeUpdate);
39
+ hook("custom-type:delete", customTypeDelete);
40
+ hook("custom-type:read", customTypeRead);
41
+ hook("custom-type:asset:update", customTypeAssetUpdate);
42
+ hook("custom-type:asset:delete", customTypeAssetDelete);
43
+ hook("custom-type:asset:read", customTypeAssetRead);
44
+ hook("custom-type-library:read", customTypeLibraryRead);
45
+ hook("snippet:read", snippetRead);
46
+ hook("slice-simulator:setup:read", sliceSimulatorSetupRead);
47
+ }
48
+ });
49
+ export {
50
+ plugin
51
+ };
52
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["../src/plugin.ts"],"sourcesContent":["import { defineSliceMachinePlugin } from \"@slicemachine/plugin-kit\";\n\nimport { name as pkgName } from \"../package.json\";\nimport { PluginOptions } from \"./types\";\n\nimport { customTypeAssetDelete } from \"./hooks/customType-asset-delete\";\nimport { customTypeAssetRead } from \"./hooks/customType-asset-read\";\nimport { customTypeAssetUpdate } from \"./hooks/customType-asset-update\";\nimport { customTypeCreate } from \"./hooks/customType-create\";\nimport { customTypeDelete } from \"./hooks/customType-delete\";\nimport { customTypeLibraryRead } from \"./hooks/customTypeLibrary-read\";\nimport { customTypeRead } from \"./hooks/customType-read\";\nimport { customTypeUpdate } from \"./hooks/customType-update\";\nimport { sliceAssetDelete } from \"./hooks/slice-asset-delete\";\nimport { sliceAssetRead } from \"./hooks/slice-asset-read\";\nimport { sliceAssetUpdate } from \"./hooks/slice-asset-update\";\nimport { sliceCreate } from \"./hooks/slice-create\";\nimport { sliceDelete } from \"./hooks/slice-delete\";\nimport { sliceLibraryRead } from \"./hooks/sliceLibrary-read\";\nimport { sliceRead } from \"./hooks/slice-read\";\nimport { sliceSimulatorSetupRead } from \"./hooks/sliceSimulator-setup-read\";\nimport { sliceUpdate } from \"./hooks/slice-update\";\nimport { snippetRead } from \"./hooks/snippet-read\";\n\nexport const plugin = defineSliceMachinePlugin<PluginOptions>({\n\tmeta: {\n\t\tname: pkgName,\n\t},\n\tdefaultOptions: {\n\t\tformat: true,\n\t},\n\tsetup({ hook }) {\n\t\thook(\"slice:create\", sliceCreate);\n\t\thook(\"slice:update\", sliceUpdate);\n\t\thook(\"slice:delete\", sliceDelete);\n\t\thook(\"slice:read\", sliceRead);\n\t\thook(\"slice:asset:update\", sliceAssetUpdate);\n\t\thook(\"slice:asset:delete\", sliceAssetDelete);\n\t\thook(\"slice:asset:read\", sliceAssetRead);\n\t\thook(\"slice-library:read\", sliceLibraryRead);\n\n\t\thook(\"custom-type:create\", customTypeCreate);\n\t\thook(\"custom-type:update\", customTypeUpdate);\n\t\thook(\"custom-type:delete\", customTypeDelete);\n\t\thook(\"custom-type:read\", customTypeRead);\n\t\thook(\"custom-type:asset:update\", customTypeAssetUpdate);\n\t\thook(\"custom-type:asset:delete\", customTypeAssetDelete);\n\t\thook(\"custom-type:asset:read\", customTypeAssetRead);\n\t\thook(\"custom-type-library:read\", customTypeLibraryRead);\n\n\t\thook(\"snippet:read\", snippetRead);\n\n\t\thook(\"slice-simulator:setup:read\", sliceSimulatorSetupRead);\n\t},\n});\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAwBO,MAAM,SAAS,yBAAwC;AAAA,EAC7D,MAAM;AAAA,IACL;AAAA,EACA;AAAA,EACD,gBAAgB;AAAA,IACf,QAAQ;AAAA,EACR;AAAA,EACD,MAAM,EAAE,QAAM;AACb,SAAK,gBAAgB,WAAW;AAChC,SAAK,gBAAgB,WAAW;AAChC,SAAK,gBAAgB,WAAW;AAChC,SAAK,cAAc,SAAS;AAC5B,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,oBAAoB,cAAc;AACvC,SAAK,sBAAsB,gBAAgB;AAE3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,oBAAoB,cAAc;AACvC,SAAK,4BAA4B,qBAAqB;AACtD,SAAK,4BAA4B,qBAAqB;AACtD,SAAK,0BAA0B,mBAAmB;AAClD,SAAK,4BAA4B,qBAAqB;AAEtD,SAAK,gBAAgB,WAAW;AAEhC,SAAK,8BAA8B,uBAAuB;AAAA,EAC3D;AACA,CAAA;"}
@@ -0,0 +1,9 @@
1
+ export declare type PluginOptions = {
2
+ format?: boolean;
3
+ } & ({
4
+ typescript?: false;
5
+ jsxExtension?: boolean;
6
+ } | {
7
+ typescript: true;
8
+ jsxExtension?: never;
9
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slicemachine/adapter-next",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Slice Machine adapter for Next.js.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -28,8 +28,8 @@
28
28
  "src"
29
29
  ],
30
30
  "scripts": {
31
- "build": "siroc build",
32
- "dev": "siroc build --watch",
31
+ "build": "vite build",
32
+ "dev": "vite build --watch",
33
33
  "format": "prettier --write .",
34
34
  "lint": "eslint --ext .js,.ts .",
35
35
  "prepare": "npm run build",
@@ -43,39 +43,42 @@
43
43
  "unit:watch": "vitest watch"
44
44
  },
45
45
  "dependencies": {
46
- "@prismicio/slice-simulator-core": "^0.2.2",
47
- "@slicemachine/plugin-kit": "^0.0.2",
46
+ "@prismicio/slice-simulator-core": "^0.2.6",
47
+ "@prismicio/types": "^0.2.3",
48
+ "@slicemachine/plugin-kit": "^0.1.5",
48
49
  "common-tags": "^1.8.2",
49
50
  "pascal-case": "^3.1.2",
50
- "prismic-ts-codegen": "^0.1.0"
51
+ "prismic-ts-codegen": "^0.1.5"
51
52
  },
52
53
  "devDependencies": {
53
- "@prismicio/mock": "^0.1.0",
54
- "@prismicio/types": "^0.2.0",
55
- "@size-limit/preset-small-lib": "^7.0.8",
54
+ "@prismicio/mock": "^0.1.1",
55
+ "@size-limit/preset-small-lib": "^8.1.0",
56
56
  "@types/common-tags": "^1.8.1",
57
- "@types/react": "^18.0.15",
58
- "@types/semver": "^7.3.10",
59
- "@typescript-eslint/eslint-plugin": "^5.30.5",
60
- "@typescript-eslint/parser": "^5.30.5",
61
- "c8": "^7.11.3",
62
- "eslint": "^8.19.0",
57
+ "@types/prettier": "^2.7.1",
58
+ "@types/react": "^18.0.25",
59
+ "@types/semver": "^7.3.13",
60
+ "@typescript-eslint/eslint-plugin": "^5.42.1",
61
+ "@typescript-eslint/parser": "^5.42.1",
62
+ "@vitejs/plugin-react": "^2.2.0",
63
+ "@vitest/coverage-c8": "^0.25.1",
64
+ "eslint": "^8.27.0",
63
65
  "eslint-config-prettier": "^8.5.0",
64
66
  "eslint-plugin-prettier": "^4.2.1",
65
- "eslint-plugin-tsdoc": "^0.2.16",
67
+ "eslint-plugin-tsdoc": "^0.2.17",
66
68
  "npm-run-all": "^4.1.5",
67
69
  "prettier": "^2.7.1",
68
- "prettier-plugin-jsdoc": "^0.3.38",
70
+ "prettier-plugin-jsdoc": "^0.4.2",
69
71
  "react": "^18.2.0",
70
- "siroc": "^0.16.0",
71
- "size-limit": "^7.0.8",
72
+ "size-limit": "^8.1.0",
72
73
  "standard-version": "^9.5.0",
73
- "ts-morph": "^15.1.0",
74
- "typescript": "^4.7.4",
75
- "vitest": "^0.18.0"
74
+ "ts-morph": "^16.0.0",
75
+ "typescript": "^4.8.4",
76
+ "vite": "^3.2.3",
77
+ "vite-plugin-sdk": "^0.0.5",
78
+ "vitest": "^0.25.1"
76
79
  },
77
80
  "peerDependencies": {
78
- "next": "^11 || ^12",
81
+ "next": "^11 || ^12 || ^13",
79
82
  "react": "^17 || ^18"
80
83
  },
81
84
  "engines": {
@@ -0,0 +1,16 @@
1
+ import type { CustomTypeAssetDeleteHook } from "@slicemachine/plugin-kit";
2
+ import * as fs from "node:fs/promises";
3
+
4
+ import type { PluginOptions } from "../types";
5
+
6
+ export const customTypeAssetDelete: CustomTypeAssetDeleteHook<
7
+ PluginOptions
8
+ > = async (data, context) => {
9
+ const filePath = context.helpers.joinPathFromRoot(
10
+ "customtypes",
11
+ data.customTypeID,
12
+ data.assetID,
13
+ );
14
+
15
+ await fs.rm(filePath);
16
+ };
@@ -0,0 +1,20 @@
1
+ import type { CustomTypeAssetReadHook } from "@slicemachine/plugin-kit";
2
+ import * as fs from "node:fs/promises";
3
+
4
+ import type { PluginOptions } from "../types";
5
+
6
+ export const customTypeAssetRead: CustomTypeAssetReadHook<
7
+ PluginOptions
8
+ > = async (data, context) => {
9
+ const filePath = context.helpers.joinPathFromRoot(
10
+ "customtypes",
11
+ data.customTypeID,
12
+ data.assetID,
13
+ );
14
+
15
+ const assetData = await fs.readFile(filePath);
16
+
17
+ return {
18
+ data: assetData,
19
+ };
20
+ };
@@ -0,0 +1,18 @@
1
+ import type { CustomTypeAssetUpdateHook } from "@slicemachine/plugin-kit";
2
+ import * as fs from "node:fs/promises";
3
+ import * as path from "node:path";
4
+
5
+ import type { PluginOptions } from "../types";
6
+
7
+ export const customTypeAssetUpdate: CustomTypeAssetUpdateHook<
8
+ PluginOptions
9
+ > = async (data, context) => {
10
+ const filePath = context.helpers.joinPathFromRoot(
11
+ "customtypes",
12
+ data.customTypeID,
13
+ data.asset.id,
14
+ );
15
+
16
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
17
+ await fs.writeFile(filePath, data.asset.data);
18
+ };
@@ -3,10 +3,11 @@ import type {
3
3
  CustomTypeCreateHookData,
4
4
  SliceMachineContext,
5
5
  } from "@slicemachine/plugin-kit";
6
- import { generateTypes } from "prismic-ts-codegen";
7
6
  import * as fs from "node:fs/promises";
8
7
  import * as path from "node:path";
9
8
 
9
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
10
+
10
11
  import type { PluginOptions } from "../types";
11
12
 
12
13
  type Args = {
@@ -26,21 +27,6 @@ const createModelFile = async ({ dir, data, helpers, options }: Args) => {
26
27
  await fs.writeFile(filePath, contents);
27
28
  };
28
29
 
29
- const createTypesFile = async ({ dir, data, helpers, options }: Args) => {
30
- const filePath = path.join(dir, "types.ts");
31
-
32
- // TODO: Figure out how to import Shared Slice types
33
- let contents = generateTypes({
34
- customTypeModels: [data.model],
35
- });
36
-
37
- if (options.format) {
38
- contents = await helpers.format(contents, filePath);
39
- }
40
-
41
- await fs.writeFile(filePath, contents);
42
- };
43
-
44
30
  export const customTypeCreate: CustomTypeCreateHook<PluginOptions> = async (
45
31
  data,
46
32
  context,
@@ -49,8 +35,7 @@ export const customTypeCreate: CustomTypeCreateHook<PluginOptions> = async (
49
35
 
50
36
  await fs.mkdir(dir, { recursive: true });
51
37
 
52
- await Promise.allSettled([
53
- createModelFile({ dir, data, ...context }),
54
- createTypesFile({ dir, data, ...context }),
55
- ]);
38
+ await createModelFile({ dir, data, ...context });
39
+
40
+ await upsertGlobalContentTypes(context);
56
41
  };
@@ -1,13 +1,17 @@
1
1
  import type { CustomTypeDeleteHook } from "@slicemachine/plugin-kit";
2
2
  import * as fs from "node:fs/promises";
3
3
 
4
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
5
+
4
6
  import type { PluginOptions } from "../types";
5
7
 
6
8
  export const customTypeDelete: CustomTypeDeleteHook<PluginOptions> = async (
7
9
  data,
8
- { helpers },
10
+ context,
9
11
  ) => {
10
- const dir = helpers.joinPathFromRoot("customtypes", data.model.id);
12
+ const dir = context.helpers.joinPathFromRoot("customtypes", data.model.id);
11
13
 
12
14
  await fs.rm(dir, { recursive: true });
15
+
16
+ await upsertGlobalContentTypes(context);
13
17
  };
@@ -15,5 +15,9 @@ export const customTypeRead: CustomTypeReadHook<PluginOptions> = async (
15
15
  "index.json",
16
16
  );
17
17
 
18
- return await readJSONFile<prismicT.CustomTypeModel>(filePath);
18
+ const model = await readJSONFile<prismicT.CustomTypeModel>(filePath);
19
+
20
+ return {
21
+ model,
22
+ };
19
23
  };
@@ -19,6 +19,9 @@ export const customTypeLibraryRead: CustomTypeLibraryReadHook<
19
19
  > = async (_data, { helpers }) => {
20
20
  const dirPath = helpers.joinPathFromRoot("customtypes");
21
21
 
22
+ // Ensure the directory exists.
23
+ await fs.mkdir(dirPath, { recursive: true });
24
+
22
25
  const childDirs = await fs.readdir(dirPath);
23
26
 
24
27
  const ids: string[] = [];
@@ -0,0 +1,19 @@
1
+ import type { SliceAssetDeleteHook } from "@slicemachine/plugin-kit";
2
+ import * as fs from "node:fs/promises";
3
+
4
+ import { pascalCase } from "../lib/pascalCase";
5
+
6
+ import type { PluginOptions } from "../types";
7
+
8
+ export const sliceAssetDelete: SliceAssetDeleteHook<PluginOptions> = async (
9
+ data,
10
+ context,
11
+ ) => {
12
+ const filePath = context.helpers.joinPathFromRoot(
13
+ data.libraryID,
14
+ pascalCase(data.sliceID),
15
+ data.assetID,
16
+ );
17
+
18
+ await fs.rm(filePath);
19
+ };
@@ -0,0 +1,23 @@
1
+ import type { SliceAssetReadHook } from "@slicemachine/plugin-kit";
2
+ import * as fs from "node:fs/promises";
3
+
4
+ import { pascalCase } from "../lib/pascalCase";
5
+
6
+ import type { PluginOptions } from "../types";
7
+
8
+ export const sliceAssetRead: SliceAssetReadHook<PluginOptions> = async (
9
+ data,
10
+ context,
11
+ ) => {
12
+ const filePath = context.helpers.joinPathFromRoot(
13
+ data.libraryID,
14
+ pascalCase(data.sliceID),
15
+ data.assetID,
16
+ );
17
+
18
+ const assetData = await fs.readFile(filePath);
19
+
20
+ return {
21
+ data: assetData,
22
+ };
23
+ };
@@ -0,0 +1,21 @@
1
+ import type { SliceAssetUpdateHook } from "@slicemachine/plugin-kit";
2
+ import * as fs from "node:fs/promises";
3
+ import * as path from "node:path";
4
+
5
+ import { pascalCase } from "../lib/pascalCase";
6
+
7
+ import type { PluginOptions } from "../types";
8
+
9
+ export const sliceAssetUpdate: SliceAssetUpdateHook<PluginOptions> = async (
10
+ data,
11
+ context,
12
+ ) => {
13
+ const filePath = context.helpers.joinPathFromRoot(
14
+ data.libraryID,
15
+ pascalCase(data.sliceID),
16
+ data.asset.id,
17
+ );
18
+
19
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
20
+ await fs.writeFile(filePath, data.asset.data);
21
+ };
@@ -3,7 +3,6 @@ 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";
@@ -11,6 +10,8 @@ import * as path from "node:path";
11
10
  import { buildSliceLibraryIndexFileContents } from "../lib/buildSliceLibraryIndexFileContents";
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
 
@@ -93,20 +94,6 @@ 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
97
  const upsertSliceLibraryIndexFile = async ({
111
98
  data,
112
99
  actions,
@@ -136,11 +123,17 @@ export const sliceCreate: SliceCreateHook<PluginOptions> = async (
136
123
 
137
124
  await fs.mkdir(dir, { recursive: true });
138
125
 
139
- await Promise.allSettled([
140
- createModelFile({ dir, data, ...context }),
141
- createComponentFile({ dir, data, ...context }),
142
- createTypesFile({ dir, data, ...context }),
143
- ]);
126
+ rejectIfNecessary(
127
+ await Promise.allSettled([
128
+ createModelFile({ dir, data, ...context }),
129
+ createComponentFile({ dir, data, ...context }),
130
+ ]),
131
+ );
144
132
 
145
- await upsertSliceLibraryIndexFile({ data, ...context });
133
+ rejectIfNecessary(
134
+ await Promise.allSettled([
135
+ upsertGlobalContentTypes(context),
136
+ upsertSliceLibraryIndexFile({ data, ...context }),
137
+ ]),
138
+ );
146
139
  };
@@ -7,6 +7,8 @@ import * as fs from "node:fs/promises";
7
7
 
8
8
  import { buildSliceLibraryIndexFileContents } from "../lib/buildSliceLibraryIndexFileContents";
9
9
  import { pascalCase } from "../lib/pascalCase";
10
+ import { rejectIfNecessary } from "../lib/rejectIfNecessary";
11
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
10
12
 
11
13
  import type { PluginOptions } from "../types";
12
14
 
@@ -46,5 +48,11 @@ export const sliceDelete: SliceDeleteHook<PluginOptions> = async (
46
48
  context,
47
49
  ) => {
48
50
  await deleteSliceDir({ data, ...context });
49
- await updateSliceLibraryIndexFile({ data, ...context });
51
+
52
+ rejectIfNecessary(
53
+ await Promise.allSettled([
54
+ upsertGlobalContentTypes(context),
55
+ updateSliceLibraryIndexFile({ data, ...context }),
56
+ ]),
57
+ );
50
58
  };
@@ -16,5 +16,9 @@ export const sliceRead: SliceReadHook<PluginOptions> = async (
16
16
  "model.json",
17
17
  );
18
18
 
19
- return await readJSONFile<prismicT.SharedSliceModel>(filePath);
19
+ const model = await readJSONFile<prismicT.SharedSliceModel>(filePath);
20
+
21
+ return {
22
+ model,
23
+ };
20
24
  };
@@ -3,11 +3,11 @@ import type {
3
3
  SliceUpdateHook,
4
4
  SliceUpdateHookData,
5
5
  } from "@slicemachine/plugin-kit";
6
- import { generateTypes } from "prismic-ts-codegen";
7
6
  import * as fs from "node:fs/promises";
8
7
  import * as path from "node:path";
9
8
 
10
9
  import { pascalCase } from "../lib/pascalCase";
10
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
11
11
 
12
12
  import type { PluginOptions } from "../types";
13
13
 
@@ -28,20 +28,6 @@ const updateModelFile = async ({ dir, data, helpers, options }: Args) => {
28
28
  await fs.writeFile(filePath, contents);
29
29
  };
30
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
31
  export const sliceUpdate: SliceUpdateHook<PluginOptions> = async (
46
32
  data,
47
33
  context,
@@ -51,8 +37,7 @@ export const sliceUpdate: SliceUpdateHook<PluginOptions> = async (
51
37
  pascalCase(data.model.id),
52
38
  );
53
39
 
54
- await Promise.allSettled([
55
- updateModelFile({ dir, data, ...context }),
56
- updateTypesFile({ dir, data, ...context }),
57
- ]);
40
+ await updateModelFile({ dir, data, ...context });
41
+
42
+ await upsertGlobalContentTypes(context);
58
43
  };
@@ -25,6 +25,9 @@ export const sliceLibraryRead: SliceLibraryReadHook<PluginOptions> = async (
25
25
  ) => {
26
26
  const dirPath = helpers.joinPathFromRoot(data.libraryID);
27
27
 
28
+ // Ensure the directory exists.
29
+ await fs.mkdir(dirPath, { recursive: true });
30
+
28
31
  const childDirs = await fs.readdir(dirPath);
29
32
 
30
33
  const sliceIDs: string[] = [];
@@ -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,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
+ };
package/src/plugin.ts CHANGED
@@ -3,18 +3,24 @@ 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 { customTypeUpdate } from "./hooks/customType-update";
14
+ import { sliceAssetDelete } from "./hooks/slice-asset-delete";
15
+ import { sliceAssetRead } from "./hooks/slice-asset-read";
16
+ import { sliceAssetUpdate } from "./hooks/slice-asset-update";
17
+ import { sliceCreate } from "./hooks/slice-create";
18
+ import { sliceDelete } from "./hooks/slice-delete";
19
+ import { sliceLibraryRead } from "./hooks/sliceLibrary-read";
20
+ import { sliceRead } from "./hooks/slice-read";
17
21
  import { sliceSimulatorSetupRead } from "./hooks/sliceSimulator-setup-read";
22
+ import { sliceUpdate } from "./hooks/slice-update";
23
+ import { snippetRead } from "./hooks/snippet-read";
18
24
 
19
25
  export const plugin = defineSliceMachinePlugin<PluginOptions>({
20
26
  meta: {
@@ -28,12 +34,18 @@ export const plugin = defineSliceMachinePlugin<PluginOptions>({
28
34
  hook("slice:update", sliceUpdate);
29
35
  hook("slice:delete", sliceDelete);
30
36
  hook("slice:read", sliceRead);
37
+ hook("slice:asset:update", sliceAssetUpdate);
38
+ hook("slice:asset:delete", sliceAssetDelete);
39
+ hook("slice:asset:read", sliceAssetRead);
31
40
  hook("slice-library:read", sliceLibraryRead);
32
41
 
33
42
  hook("custom-type:create", customTypeCreate);
34
43
  hook("custom-type:update", customTypeUpdate);
35
44
  hook("custom-type:delete", customTypeDelete);
36
45
  hook("custom-type:read", customTypeRead);
46
+ hook("custom-type:asset:update", customTypeAssetUpdate);
47
+ hook("custom-type:asset:delete", customTypeAssetDelete);
48
+ hook("custom-type:asset:read", customTypeAssetRead);
37
49
  hook("custom-type-library:read", customTypeLibraryRead);
38
50
 
39
51
  hook("snippet:read", snippetRead);