@prismicio/plugin-kit 0.0.1

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 (235) hide show
  1. package/dist/_node_modules/lower-case/dist.es2015/index.js +7 -0
  2. package/dist/_node_modules/lower-case/dist.es2015/index.js.map +1 -0
  3. package/dist/_node_modules/no-case/dist.es2015/index.js +28 -0
  4. package/dist/_node_modules/no-case/dist.es2015/index.js.map +1 -0
  5. package/dist/_node_modules/p-limit/_node_modules/yocto-queue/index.js +68 -0
  6. package/dist/_node_modules/p-limit/_node_modules/yocto-queue/index.js.map +1 -0
  7. package/dist/_node_modules/p-limit/index.js +77 -0
  8. package/dist/_node_modules/p-limit/index.js.map +1 -0
  9. package/dist/_node_modules/pascal-case/dist.es2015/index.js +25 -0
  10. package/dist/_node_modules/pascal-case/dist.es2015/index.js.map +1 -0
  11. package/dist/_node_modules/tslib/tslib.es6.js +14 -0
  12. package/dist/_node_modules/tslib/tslib.es6.js.map +1 -0
  13. package/dist/createPluginHookSystem.d.ts +6 -0
  14. package/dist/createPluginHookSystem.js +8 -0
  15. package/dist/createPluginHookSystem.js.map +1 -0
  16. package/dist/createPluginSystemActions.d.ts +46 -0
  17. package/dist/createPluginSystemActions.js +96 -0
  18. package/dist/createPluginSystemActions.js.map +1 -0
  19. package/dist/createPluginSystemContext.d.ts +31 -0
  20. package/dist/createPluginSystemContext.js +12 -0
  21. package/dist/createPluginSystemContext.js.map +1 -0
  22. package/dist/createPluginSystemHelpers.d.ts +37 -0
  23. package/dist/createPluginSystemHelpers.js +72 -0
  24. package/dist/createPluginSystemHelpers.js.map +1 -0
  25. package/dist/createPluginSystemRunner.d.ts +58 -0
  26. package/dist/createPluginSystemRunner.js +128 -0
  27. package/dist/createPluginSystemRunner.js.map +1 -0
  28. package/dist/definePlugin.d.ts +30 -0
  29. package/dist/definePlugin.js +5 -0
  30. package/dist/definePlugin.js.map +1 -0
  31. package/dist/fs/buildCustomTypeDirectoryPath.d.ts +5 -0
  32. package/dist/fs/buildCustomTypeDirectoryPath.js +9 -0
  33. package/dist/fs/buildCustomTypeDirectoryPath.js.map +1 -0
  34. package/dist/fs/buildCustomTypeFilePath.d.ts +5 -0
  35. package/dist/fs/buildCustomTypeFilePath.js +9 -0
  36. package/dist/fs/buildCustomTypeFilePath.js.map +1 -0
  37. package/dist/fs/buildCustomTypeLibraryDirectoryPath.d.ts +6 -0
  38. package/dist/fs/buildCustomTypeLibraryDirectoryPath.js +8 -0
  39. package/dist/fs/buildCustomTypeLibraryDirectoryPath.js.map +1 -0
  40. package/dist/fs/buildSliceDirectoryPath.d.ts +4 -0
  41. package/dist/fs/buildSliceDirectoryPath.js +12 -0
  42. package/dist/fs/buildSliceDirectoryPath.js.map +1 -0
  43. package/dist/fs/buildSliceFilePath.d.ts +5 -0
  44. package/dist/fs/buildSliceFilePath.js +9 -0
  45. package/dist/fs/buildSliceFilePath.js.map +1 -0
  46. package/dist/fs/buildSliceLibraryDirectoryPath.d.ts +7 -0
  47. package/dist/fs/buildSliceLibraryDirectoryPath.js +8 -0
  48. package/dist/fs/buildSliceLibraryDirectoryPath.js.map +1 -0
  49. package/dist/fs/checkHasProjectFile.d.ts +6 -0
  50. package/dist/fs/checkHasProjectFile.js +9 -0
  51. package/dist/fs/checkHasProjectFile.js.map +1 -0
  52. package/dist/fs/checkIsTypeScriptProject.d.ts +4 -0
  53. package/dist/fs/checkIsTypeScriptProject.js +12 -0
  54. package/dist/fs/checkIsTypeScriptProject.js.map +1 -0
  55. package/dist/fs/constants.d.ts +9 -0
  56. package/dist/fs/constants.js +15 -0
  57. package/dist/fs/constants.js.map +1 -0
  58. package/dist/fs/deleteCustomTypeDirectory.d.ts +3 -0
  59. package/dist/fs/deleteCustomTypeDirectory.js +18 -0
  60. package/dist/fs/deleteCustomTypeDirectory.js.map +1 -0
  61. package/dist/fs/deleteProjectFile.d.ts +6 -0
  62. package/dist/fs/deleteProjectFile.js +10 -0
  63. package/dist/fs/deleteProjectFile.js.map +1 -0
  64. package/dist/fs/deleteSliceDirectory.d.ts +3 -0
  65. package/dist/fs/deleteSliceDirectory.js +18 -0
  66. package/dist/fs/deleteSliceDirectory.js.map +1 -0
  67. package/dist/fs/deleteSliceFile.d.ts +3 -0
  68. package/dist/fs/deleteSliceFile.js +18 -0
  69. package/dist/fs/deleteSliceFile.js.map +1 -0
  70. package/dist/fs/index.d.ts +49 -0
  71. package/dist/fs/index.js +53 -0
  72. package/dist/fs/index.js.map +1 -0
  73. package/dist/fs/lib/checkPathExists.d.ts +2 -0
  74. package/dist/fs/lib/checkPathExists.js +13 -0
  75. package/dist/fs/lib/checkPathExists.js.map +1 -0
  76. package/dist/fs/lib/fsLimit.d.ts +31 -0
  77. package/dist/fs/lib/fsLimit.js +23 -0
  78. package/dist/fs/lib/fsLimit.js.map +1 -0
  79. package/dist/fs/lib/isCustomTypeModel.d.ts +2 -0
  80. package/dist/fs/lib/isCustomTypeModel.js +7 -0
  81. package/dist/fs/lib/isCustomTypeModel.js.map +1 -0
  82. package/dist/fs/lib/isSharedSliceModel.d.ts +2 -0
  83. package/dist/fs/lib/isSharedSliceModel.js +7 -0
  84. package/dist/fs/lib/isSharedSliceModel.js.map +1 -0
  85. package/dist/fs/lib/pascalCase.d.ts +8 -0
  86. package/dist/fs/lib/pascalCase.js +10 -0
  87. package/dist/fs/lib/pascalCase.js.map +1 -0
  88. package/dist/fs/lib/readJSONFile.d.ts +1 -0
  89. package/dist/fs/lib/readJSONFile.js +9 -0
  90. package/dist/fs/lib/readJSONFile.js.map +1 -0
  91. package/dist/fs/lib/resolveSliceModel.d.ts +10 -0
  92. package/dist/fs/lib/resolveSliceModel.js +14 -0
  93. package/dist/fs/lib/resolveSliceModel.js.map +1 -0
  94. package/dist/fs/readCustomTypeFile.d.ts +8 -0
  95. package/dist/fs/readCustomTypeFile.js +14 -0
  96. package/dist/fs/readCustomTypeFile.js.map +1 -0
  97. package/dist/fs/readCustomTypeLibrary.d.ts +6 -0
  98. package/dist/fs/readCustomTypeLibrary.js +53 -0
  99. package/dist/fs/readCustomTypeLibrary.js.map +1 -0
  100. package/dist/fs/readCustomTypeModel.d.ts +7 -0
  101. package/dist/fs/readCustomTypeModel.js +16 -0
  102. package/dist/fs/readCustomTypeModel.js.map +1 -0
  103. package/dist/fs/readProjectFile.d.ts +12 -0
  104. package/dist/fs/readProjectFile.js +9 -0
  105. package/dist/fs/readProjectFile.js.map +1 -0
  106. package/dist/fs/readSliceFile.d.ts +8 -0
  107. package/dist/fs/readSliceFile.js +17 -0
  108. package/dist/fs/readSliceFile.js.map +1 -0
  109. package/dist/fs/readSliceLibrary.d.ts +7 -0
  110. package/dist/fs/readSliceLibrary.js +56 -0
  111. package/dist/fs/readSliceLibrary.js.map +1 -0
  112. package/dist/fs/readSliceModel.d.ts +11 -0
  113. package/dist/fs/readSliceModel.js +46 -0
  114. package/dist/fs/readSliceModel.js.map +1 -0
  115. package/dist/fs/renameCustomType.d.ts +3 -0
  116. package/dist/fs/renameCustomType.js +8 -0
  117. package/dist/fs/renameCustomType.js.map +1 -0
  118. package/dist/fs/renameSlice.d.ts +7 -0
  119. package/dist/fs/renameSlice.js +22 -0
  120. package/dist/fs/renameSlice.js.map +1 -0
  121. package/dist/fs/types.d.ts +3 -0
  122. package/dist/fs/upsertGlobalTypeScriptTypes.d.ts +16 -0
  123. package/dist/fs/upsertGlobalTypeScriptTypes.js +37 -0
  124. package/dist/fs/upsertGlobalTypeScriptTypes.js.map +1 -0
  125. package/dist/fs/writeCustomTypeFile.d.ts +9 -0
  126. package/dist/fs/writeCustomTypeFile.js +18 -0
  127. package/dist/fs/writeCustomTypeFile.js.map +1 -0
  128. package/dist/fs/writeCustomTypeModel.d.ts +12 -0
  129. package/dist/fs/writeCustomTypeModel.js +14 -0
  130. package/dist/fs/writeCustomTypeModel.js.map +1 -0
  131. package/dist/fs/writeProjectFile.d.ts +15 -0
  132. package/dist/fs/writeProjectFile.js +16 -0
  133. package/dist/fs/writeProjectFile.js.map +1 -0
  134. package/dist/fs/writeSliceFile.d.ts +9 -0
  135. package/dist/fs/writeSliceFile.js +18 -0
  136. package/dist/fs/writeSliceFile.js.map +1 -0
  137. package/dist/fs/writeSliceModel.d.ts +11 -0
  138. package/dist/fs/writeSliceModel.js +13 -0
  139. package/dist/fs/writeSliceModel.js.map +1 -0
  140. package/dist/hooks/customType-create.d.ts +28 -0
  141. package/dist/hooks/customType-delete.d.ts +26 -0
  142. package/dist/hooks/customType-read.d.ts +32 -0
  143. package/dist/hooks/customType-rename.d.ts +29 -0
  144. package/dist/hooks/customType-update.d.ts +29 -0
  145. package/dist/hooks/customTypeLibrary-read.d.ts +25 -0
  146. package/dist/hooks/debug.d.ts +22 -0
  147. package/dist/hooks/project-init.d.ts +35 -0
  148. package/dist/hooks/slice-create.d.ts +30 -0
  149. package/dist/hooks/slice-delete.d.ts +26 -0
  150. package/dist/hooks/slice-read.d.ts +32 -0
  151. package/dist/hooks/slice-rename.d.ts +26 -0
  152. package/dist/hooks/slice-update.d.ts +29 -0
  153. package/dist/hooks/sliceLibrary-read.d.ts +27 -0
  154. package/dist/index.d.ts +24 -0
  155. package/dist/index.js +15 -0
  156. package/dist/index.js.map +1 -0
  157. package/dist/lib/DecodeError.d.ts +13 -0
  158. package/dist/lib/DecodeError.js +21 -0
  159. package/dist/lib/DecodeError.js.map +1 -0
  160. package/dist/lib/HookSystem.d.ts +93 -0
  161. package/dist/lib/HookSystem.js +121 -0
  162. package/dist/lib/HookSystem.js.map +1 -0
  163. package/dist/lib/decode.d.ts +10 -0
  164. package/dist/lib/decode.js +16 -0
  165. package/dist/lib/decode.js.map +1 -0
  166. package/dist/lib/decodePrismicConfig.d.ts +3 -0
  167. package/dist/lib/decodePrismicConfig.js +22 -0
  168. package/dist/lib/decodePrismicConfig.js.map +1 -0
  169. package/dist/types.d.ts +129 -0
  170. package/dist/types.js +21 -0
  171. package/dist/types.js.map +1 -0
  172. package/package.json +90 -0
  173. package/src/createPluginHookSystem.ts +9 -0
  174. package/src/createPluginSystemActions.ts +188 -0
  175. package/src/createPluginSystemContext.ts +51 -0
  176. package/src/createPluginSystemHelpers.ts +129 -0
  177. package/src/createPluginSystemRunner.ts +216 -0
  178. package/src/definePlugin.ts +48 -0
  179. package/src/fs/buildCustomTypeDirectoryPath.ts +19 -0
  180. package/src/fs/buildCustomTypeFilePath.ts +16 -0
  181. package/src/fs/buildCustomTypeLibraryDirectoryPath.ts +16 -0
  182. package/src/fs/buildSliceDirectoryPath.ts +25 -0
  183. package/src/fs/buildSliceFilePath.ts +16 -0
  184. package/src/fs/buildSliceLibraryDirectoryPath.ts +17 -0
  185. package/src/fs/checkHasProjectFile.ts +16 -0
  186. package/src/fs/checkIsTypeScriptProject.ts +16 -0
  187. package/src/fs/constants.ts +15 -0
  188. package/src/fs/deleteCustomTypeDirectory.ts +24 -0
  189. package/src/fs/deleteProjectFile.ts +18 -0
  190. package/src/fs/deleteSliceDirectory.ts +24 -0
  191. package/src/fs/deleteSliceFile.ts +24 -0
  192. package/src/fs/index.ts +103 -0
  193. package/src/fs/lib/checkPathExists.ts +13 -0
  194. package/src/fs/lib/fsLimit.ts +72 -0
  195. package/src/fs/lib/isCustomTypeModel.ts +7 -0
  196. package/src/fs/lib/isSharedSliceModel.ts +14 -0
  197. package/src/fs/lib/pascalCase.ts +17 -0
  198. package/src/fs/lib/readJSONFile.ts +7 -0
  199. package/src/fs/lib/resolveSliceModel.ts +28 -0
  200. package/src/fs/readCustomTypeFile.ts +27 -0
  201. package/src/fs/readCustomTypeLibrary.ts +86 -0
  202. package/src/fs/readCustomTypeModel.ts +27 -0
  203. package/src/fs/readProjectFile.ts +24 -0
  204. package/src/fs/readSliceFile.ts +27 -0
  205. package/src/fs/readSliceLibrary.ts +91 -0
  206. package/src/fs/readSliceModel.ts +90 -0
  207. package/src/fs/renameCustomType.ts +12 -0
  208. package/src/fs/renameSlice.ts +35 -0
  209. package/src/fs/types.ts +5 -0
  210. package/src/fs/upsertGlobalTypeScriptTypes.ts +64 -0
  211. package/src/fs/writeCustomTypeFile.ts +30 -0
  212. package/src/fs/writeCustomTypeModel.ts +31 -0
  213. package/src/fs/writeProjectFile.ts +39 -0
  214. package/src/fs/writeSliceFile.ts +30 -0
  215. package/src/fs/writeSliceModel.ts +26 -0
  216. package/src/hooks/customType-create.ts +42 -0
  217. package/src/hooks/customType-delete.ts +40 -0
  218. package/src/hooks/customType-read.ts +46 -0
  219. package/src/hooks/customType-rename.ts +43 -0
  220. package/src/hooks/customType-update.ts +43 -0
  221. package/src/hooks/customTypeLibrary-read.ts +38 -0
  222. package/src/hooks/debug.ts +31 -0
  223. package/src/hooks/project-init.ts +48 -0
  224. package/src/hooks/slice-create.ts +44 -0
  225. package/src/hooks/slice-delete.ts +40 -0
  226. package/src/hooks/slice-read.ts +44 -0
  227. package/src/hooks/slice-rename.ts +40 -0
  228. package/src/hooks/slice-update.ts +43 -0
  229. package/src/hooks/sliceLibrary-read.ts +41 -0
  230. package/src/index.ts +141 -0
  231. package/src/lib/DecodeError.ts +31 -0
  232. package/src/lib/HookSystem.ts +277 -0
  233. package/src/lib/decode.ts +30 -0
  234. package/src/lib/decodePrismicConfig.ts +26 -0
  235. package/src/types.ts +175 -0
@@ -0,0 +1,129 @@
1
+ import { PluginSystemContext } from "./createPluginSystemContext.js";
2
+ import { Plugin } from "./definePlugin.js";
3
+ import { CustomTypeCreateHookBase } from "./hooks/customType-create.js";
4
+ import { CustomTypeDeleteHookBase } from "./hooks/customType-delete.js";
5
+ import { CustomTypeReadHookBase } from "./hooks/customType-read.js";
6
+ import { CustomTypeRenameHookBase } from "./hooks/customType-rename.js";
7
+ import { CustomTypeUpdateHookBase } from "./hooks/customType-update.js";
8
+ import { CustomTypeLibraryReadHookBase } from "./hooks/customTypeLibrary-read.js";
9
+ import { DebugHookBase } from "./hooks/debug.js";
10
+ import { ProjectInitHookBase } from "./hooks/project-init.js";
11
+ import { SliceCreateHookBase } from "./hooks/slice-create.js";
12
+ import { SliceDeleteHookBase } from "./hooks/slice-delete.js";
13
+ import { SliceReadHookBase } from "./hooks/slice-read.js";
14
+ import { SliceRenameHookBase } from "./hooks/slice-rename.js";
15
+ import { SliceUpdateHookBase } from "./hooks/slice-update.js";
16
+ import { SliceLibraryReadHookBase } from "./hooks/sliceLibrary-read.js";
17
+ import { Hook } from "./lib/HookSystem.js";
18
+ /**
19
+ * A value optionally wrapped in a `PromiseLike`.
20
+ *
21
+ * @typeParam T - The value that can optionally be wrapped.
22
+ */
23
+ export type Promisable<T> = T | PromiseLike<T>;
24
+ /**
25
+ * A generic type for a user-provided plugin options. Prefer using a
26
+ * plugin-specific type over this type.
27
+ */
28
+ export type PluginOptions = Record<string, unknown>;
29
+ /**
30
+ * A string, object, or instance representing a registered plugin.
31
+ *
32
+ * @typeParam TPluginOptions - User-provided options for the plugin.
33
+ */
34
+ export type PrismicConfigPluginRegistration<TPluginOptions extends PluginOptions = PluginOptions> = string | Plugin | {
35
+ resolve: string | Plugin;
36
+ options?: TPluginOptions;
37
+ };
38
+ /**
39
+ * Prismic configuration from `prismic.config.js`.
40
+ */
41
+ export type PrismicConfig = {
42
+ repositoryName: string;
43
+ adapter: PrismicConfigPluginRegistration;
44
+ libraries?: string[];
45
+ apiEndpoint?: string;
46
+ };
47
+ /**
48
+ * Prismic project metadata.
49
+ */
50
+ export type PrismicProject = {
51
+ /**
52
+ * An absolute path to project root.
53
+ */
54
+ root: string;
55
+ /**
56
+ * Prismic `prismic.config.json` content, validated.
57
+ */
58
+ config: PrismicConfig;
59
+ };
60
+ /**
61
+ * A Slice Library's metadata.
62
+ */
63
+ export type SliceLibrary = {
64
+ id: string;
65
+ };
66
+ /**
67
+ * A hook handler.
68
+ */
69
+ export type PluginHook<TData, TReturn> = (data: TData) => Promisable<TReturn>;
70
+ /**
71
+ * Extra arguments provided to hooks when called.
72
+ *
73
+ * @typeParam TPluginOptions - User-provided options for the hook's plugin.
74
+ */
75
+ export type PluginHookExtraArgs<TPluginOptions extends PluginOptions = PluginOptions> = [context: PluginSystemContext<TPluginOptions>];
76
+ /**
77
+ * Utility type to extend a hook handler's type with Plugin System-specific
78
+ * extra arguments.
79
+ *
80
+ * @typeParam THook - Hook handler to extend.
81
+ * @typeParam TPluginOptions - User-provided options for the hook's plugin.
82
+ */
83
+ export type ExtendPluginSystemHook<THook extends PluginHook<any, any>, TPluginOptions extends PluginOptions = PluginOptions> = (...args: [
84
+ ...args: Parameters<THook>,
85
+ ...extraArgs: PluginHookExtraArgs<TPluginOptions>
86
+ ]) => ReturnType<THook>;
87
+ /**
88
+ * Hook types.
89
+ */
90
+ export declare const PluginHookType: {
91
+ readonly slice_create: "slice:create";
92
+ readonly slice_update: "slice:update";
93
+ readonly slice_rename: "slice:rename";
94
+ readonly slice_delete: "slice:delete";
95
+ readonly slice_read: "slice:read";
96
+ readonly sliceLibrary_read: "slice-library:read";
97
+ readonly customType_create: "custom-type:create";
98
+ readonly customType_update: "custom-type:update";
99
+ readonly customType_rename: "custom-type:rename";
100
+ readonly customType_delete: "custom-type:delete";
101
+ readonly customType_read: "custom-type:read";
102
+ readonly customTypeLibrary_read: "custom-type-library:read";
103
+ readonly snippet_read: "snippet:read";
104
+ readonly project_init: "project:init";
105
+ readonly debug: "debug";
106
+ };
107
+ /**
108
+ * Hook types.
109
+ */
110
+ export type PluginHookTypes = (typeof PluginHookType)[keyof typeof PluginHookType];
111
+ /**
112
+ * Plugin hook handlers.
113
+ */
114
+ export type PluginHooks = {
115
+ [PluginHookType.slice_create]: Hook<SliceCreateHookBase>;
116
+ [PluginHookType.slice_update]: Hook<SliceUpdateHookBase>;
117
+ [PluginHookType.slice_rename]: Hook<SliceRenameHookBase>;
118
+ [PluginHookType.slice_delete]: Hook<SliceDeleteHookBase>;
119
+ [PluginHookType.slice_read]: Hook<SliceReadHookBase>;
120
+ [PluginHookType.sliceLibrary_read]: Hook<SliceLibraryReadHookBase>;
121
+ [PluginHookType.customType_create]: Hook<CustomTypeCreateHookBase>;
122
+ [PluginHookType.customType_update]: Hook<CustomTypeUpdateHookBase>;
123
+ [PluginHookType.customType_rename]: Hook<CustomTypeRenameHookBase>;
124
+ [PluginHookType.customType_delete]: Hook<CustomTypeDeleteHookBase>;
125
+ [PluginHookType.customType_read]: Hook<CustomTypeReadHookBase>;
126
+ [PluginHookType.customTypeLibrary_read]: Hook<CustomTypeLibraryReadHookBase>;
127
+ [PluginHookType.project_init]: Hook<ProjectInitHookBase>;
128
+ [PluginHookType.debug]: Hook<DebugHookBase>;
129
+ };
package/dist/types.js ADDED
@@ -0,0 +1,21 @@
1
+ const PluginHookType = {
2
+ slice_create: "slice:create",
3
+ slice_update: "slice:update",
4
+ slice_rename: "slice:rename",
5
+ slice_delete: "slice:delete",
6
+ slice_read: "slice:read",
7
+ sliceLibrary_read: "slice-library:read",
8
+ customType_create: "custom-type:create",
9
+ customType_update: "custom-type:update",
10
+ customType_rename: "custom-type:rename",
11
+ customType_delete: "custom-type:delete",
12
+ customType_read: "custom-type:read",
13
+ customTypeLibrary_read: "custom-type-library:read",
14
+ snippet_read: "snippet:read",
15
+ project_init: "project:init",
16
+ debug: "debug"
17
+ };
18
+ export {
19
+ PluginHookType
20
+ };
21
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sources":["../../src/types.ts"],"sourcesContent":["import { PluginSystemContext } from \"./createPluginSystemContext\";\nimport { Plugin } from \"./definePlugin\";\nimport { CustomTypeCreateHookBase } from \"./hooks/customType-create\";\nimport { CustomTypeDeleteHookBase } from \"./hooks/customType-delete\";\nimport { CustomTypeReadHookBase } from \"./hooks/customType-read\";\nimport { CustomTypeRenameHookBase } from \"./hooks/customType-rename\";\nimport { CustomTypeUpdateHookBase } from \"./hooks/customType-update\";\nimport { CustomTypeLibraryReadHookBase } from \"./hooks/customTypeLibrary-read\";\nimport { DebugHookBase } from \"./hooks/debug\";\nimport { ProjectInitHookBase } from \"./hooks/project-init\";\nimport { SliceCreateHookBase } from \"./hooks/slice-create\";\nimport { SliceDeleteHookBase } from \"./hooks/slice-delete\";\nimport { SliceReadHookBase } from \"./hooks/slice-read\";\nimport { SliceRenameHookBase } from \"./hooks/slice-rename\";\nimport { SliceUpdateHookBase } from \"./hooks/slice-update\";\nimport { SliceLibraryReadHookBase } from \"./hooks/sliceLibrary-read\";\nimport { Hook } from \"./lib/HookSystem\";\n\n/**\n * A value optionally wrapped in a `PromiseLike`.\n *\n * @typeParam T - The value that can optionally be wrapped.\n */\nexport type Promisable<T> = T | PromiseLike<T>;\n\n/**\n * A generic type for a user-provided plugin options. Prefer using a\n * plugin-specific type over this type.\n */\nexport type PluginOptions = Record<string, unknown>;\n\n/**\n * A string, object, or instance representing a registered plugin.\n *\n * @typeParam TPluginOptions - User-provided options for the plugin.\n */\nexport type PrismicConfigPluginRegistration<\n\tTPluginOptions extends PluginOptions = PluginOptions,\n> =\n\t| string\n\t| Plugin\n\t| {\n\t\t\tresolve: string | Plugin;\n\t\t\toptions?: TPluginOptions;\n\t };\n\n/**\n * Prismic configuration from `prismic.config.js`.\n */\nexport type PrismicConfig = {\n\trepositoryName: string;\n\tadapter: PrismicConfigPluginRegistration;\n\tlibraries?: string[];\n\tapiEndpoint?: string;\n};\n\n/**\n * Prismic project metadata.\n */\nexport type PrismicProject = {\n\t/**\n\t * An absolute path to project root.\n\t */\n\troot: string;\n\t/**\n\t * Prismic `prismic.config.json` content, validated.\n\t */\n\tconfig: PrismicConfig;\n};\n\n/**\n * A Slice Library's metadata.\n */\nexport type SliceLibrary = {\n\tid: string;\n};\n\n// ============================================================================\n//\n// # HOOK TYPES\n//\n// ============================================================================\n\n/**\n * A hook handler.\n */\nexport type PluginHook<TData, TReturn> = (data: TData) => Promisable<TReturn>;\n\n/**\n * Extra arguments provided to hooks when called.\n *\n * @typeParam TPluginOptions - User-provided options for the hook's plugin.\n */\nexport type PluginHookExtraArgs<\n\tTPluginOptions extends PluginOptions = PluginOptions,\n> = [context: PluginSystemContext<TPluginOptions>];\n\n/**\n * Utility type to extend a hook handler's type with Plugin System-specific\n * extra arguments.\n *\n * @typeParam THook - Hook handler to extend.\n * @typeParam TPluginOptions - User-provided options for the hook's plugin.\n */\nexport type ExtendPluginSystemHook<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tTHook extends PluginHook<any, any>,\n\tTPluginOptions extends PluginOptions = PluginOptions,\n> = (\n\t...args: [\n\t\t...args: Parameters<THook>,\n\t\t...extraArgs: PluginHookExtraArgs<TPluginOptions>,\n\t]\n) => ReturnType<THook>;\n\n/**\n * Hook types.\n */\nexport const PluginHookType = {\n\tslice_create: \"slice:create\",\n\tslice_update: \"slice:update\",\n\tslice_rename: \"slice:rename\",\n\tslice_delete: \"slice:delete\",\n\tslice_read: \"slice:read\",\n\tsliceLibrary_read: \"slice-library:read\",\n\n\tcustomType_create: \"custom-type:create\",\n\tcustomType_update: \"custom-type:update\",\n\tcustomType_rename: \"custom-type:rename\",\n\tcustomType_delete: \"custom-type:delete\",\n\tcustomType_read: \"custom-type:read\",\n\tcustomTypeLibrary_read: \"custom-type-library:read\",\n\n\tsnippet_read: \"snippet:read\",\n\n\tproject_init: \"project:init\",\n\n\tdebug: \"debug\",\n} as const;\n\n/**\n * Hook types.\n */\nexport type PluginHookTypes =\n\t(typeof PluginHookType)[keyof typeof PluginHookType];\n\n/**\n * Plugin hook handlers.\n */\nexport type PluginHooks = {\n\t// Slices\n\t[PluginHookType.slice_create]: Hook<SliceCreateHookBase>;\n\t[PluginHookType.slice_update]: Hook<SliceUpdateHookBase>;\n\t[PluginHookType.slice_rename]: Hook<SliceRenameHookBase>;\n\t[PluginHookType.slice_delete]: Hook<SliceDeleteHookBase>;\n\t[PluginHookType.slice_read]: Hook<SliceReadHookBase>;\n\n\t// Slice Libraries\n\t[PluginHookType.sliceLibrary_read]: Hook<SliceLibraryReadHookBase>;\n\n\t// Custom Types\n\t[PluginHookType.customType_create]: Hook<CustomTypeCreateHookBase>;\n\t[PluginHookType.customType_update]: Hook<CustomTypeUpdateHookBase>;\n\t[PluginHookType.customType_rename]: Hook<CustomTypeRenameHookBase>;\n\t[PluginHookType.customType_delete]: Hook<CustomTypeDeleteHookBase>;\n\t[PluginHookType.customType_read]: Hook<CustomTypeReadHookBase>;\n\t// Custom Type Libraries\n\t[PluginHookType.customTypeLibrary_read]: Hook<CustomTypeLibraryReadHookBase>;\n\n\t// Project\n\t[PluginHookType.project_init]: Hook<ProjectInitHookBase>;\n\n\t// Debug\n\t[PluginHookType.debug]: Hook<DebugHookBase>;\n};\n"],"names":[],"mappings":"AAsHO,MAAM,iBAAiB;AAAA,EAC7B,cAAc;AAAA,EACd,cAAc;AAAA,EACd,cAAc;AAAA,EACd,cAAc;AAAA,EACd,YAAY;AAAA,EACZ,mBAAmB;AAAA,EAEnB,mBAAmB;AAAA,EACnB,mBAAmB;AAAA,EACnB,mBAAmB;AAAA,EACnB,mBAAmB;AAAA,EACnB,iBAAiB;AAAA,EACjB,wBAAwB;AAAA,EAExB,cAAc;AAAA,EAEd,cAAc;AAAA,EAEd,OAAO;;"}
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "@prismicio/plugin-kit",
3
+ "version": "0.0.1",
4
+ "description": "A set of helpers to develop and run Prismic plugins",
5
+ "keywords": [
6
+ "typescript",
7
+ "helpers",
8
+ "utils",
9
+ "toolbox",
10
+ "prismic"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "ssh://git@github.com/prismicio/devtools.git",
15
+ "directory": "packages/plugin-kit"
16
+ },
17
+ "license": "Apache-2.0",
18
+ "author": "Prismic <contact@prismic.io> (https://prismic.io)",
19
+ "type": "module",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ },
25
+ "./fs": {
26
+ "types": "./dist/fs/index.d.ts",
27
+ "import": "./dist/fs/index.js"
28
+ },
29
+ "./package.json": "./package.json"
30
+ },
31
+ "main": "dist/index.js",
32
+ "module": "dist/index.js",
33
+ "types": "dist/index.d.ts",
34
+ "typesVersions": {
35
+ "*": {
36
+ "*": [
37
+ "dist/index.d.ts"
38
+ ],
39
+ "fs": [
40
+ "dist/fs/index.d.ts"
41
+ ]
42
+ }
43
+ },
44
+ "files": [
45
+ "dist",
46
+ "src"
47
+ ],
48
+ "engines": {
49
+ "node": ">=20.19.0"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "scripts": {
55
+ "build": "vite build",
56
+ "dev": "vite build --watch",
57
+ "types": "tsc --noEmit",
58
+ "format": "prettier --write .",
59
+ "lint": "eslint --max-warnings 0 .",
60
+ "prepack": "$npm_execpath run build",
61
+ "depcheck": "depcheck --config=.depcheckrc",
62
+ "audit": "yarn npm audit --environment production --severity high"
63
+ },
64
+ "dependencies": {
65
+ "@prismicio/client": "7.21.0",
66
+ "common-tags": "1.8.2",
67
+ "defu": "6.1.4",
68
+ "fs-extra": "11.3.2",
69
+ "p-limit": "7.2.0",
70
+ "prettier": "3.7.1",
71
+ "prismic-ts-codegen": "0.1.28",
72
+ "zod": "4.1.13"
73
+ },
74
+ "devDependencies": {
75
+ "@eslint/js": "9.28.0",
76
+ "@prismicio/types-internal": "3.16.1",
77
+ "@trivago/prettier-plugin-sort-imports": "6.0.0",
78
+ "@types/common-tags": "1.8.4",
79
+ "@types/fs-extra": "11.0.4",
80
+ "depcheck": "1.4.7",
81
+ "eslint": "9.39.1",
82
+ "eslint-config-prettier": "10.1.8",
83
+ "eslint-plugin-tsdoc": "0.5.0",
84
+ "prettier-plugin-jsdoc": "1.7.0",
85
+ "typescript": "5.9.3",
86
+ "typescript-eslint": "8.33.0",
87
+ "vite": "7.2.4",
88
+ "vite-plugin-sdk": "0.1.5"
89
+ }
90
+ }
@@ -0,0 +1,9 @@
1
+ import { HookSystem } from "./lib/HookSystem";
2
+ import type { PluginHooks } from "./types";
3
+
4
+ /**
5
+ * @internal
6
+ */
7
+ export const createPluginHookSystem = (): HookSystem<PluginHooks> => {
8
+ return new HookSystem<PluginHooks>();
9
+ };
@@ -0,0 +1,188 @@
1
+ import {
2
+ CustomTypeReadHookData,
3
+ CustomTypeReadHookReturnType,
4
+ } from "./hooks/customType-read";
5
+ import { CustomTypeLibraryReadHookReturnType } from "./hooks/customTypeLibrary-read";
6
+ import { SliceReadHookData, SliceReadHookReturnType } from "./hooks/slice-read";
7
+ import {
8
+ SliceLibraryReadHookData,
9
+ SliceLibraryReadHookReturnType,
10
+ } from "./hooks/sliceLibrary-read";
11
+ import { HookSystem } from "./lib/HookSystem";
12
+ import { PluginHooks, PrismicProject } from "./types";
13
+
14
+ export type ReadAllSliceModelsActionArgs<
15
+ TWithMetadata extends boolean = false,
16
+ > = {
17
+ withMetadata?: TWithMetadata;
18
+ };
19
+
20
+ export type ReadAllSliceModelsActionReturnType = (SliceReadHookReturnType & {
21
+ libraryID: string;
22
+ })[];
23
+
24
+ export type ReadAllSliceModelsForLibraryActionArgs = {
25
+ libraryID: string;
26
+ };
27
+
28
+ /**
29
+ * Creates Plugin System actions.
30
+ *
31
+ * @internal
32
+ */
33
+ export const createPluginSystemActions = (
34
+ project: PrismicProject,
35
+ hookSystem: HookSystem<PluginHooks>,
36
+ ): PluginSystemActions => {
37
+ return new PluginSystemActions(project, hookSystem);
38
+ };
39
+
40
+ /**
41
+ * Plugin System actions shared to plugins and hooks.
42
+ */
43
+ export class PluginSystemActions {
44
+ /**
45
+ * The Prismic project's metadata.
46
+ *
47
+ * @internal
48
+ */
49
+ private _project: PrismicProject;
50
+ /**
51
+ * The actions' hook system used to internally trigger hook calls.
52
+ *
53
+ * @internal
54
+ */
55
+ private _hookSystem: HookSystem<PluginHooks>;
56
+
57
+ constructor(project: PrismicProject, hookSystem: HookSystem<PluginHooks>) {
58
+ this._project = project;
59
+ this._hookSystem = hookSystem;
60
+ }
61
+
62
+ readAllSliceModels =
63
+ async (): Promise<ReadAllSliceModelsActionReturnType> => {
64
+ const libraryIDs = this._project.config.libraries || [];
65
+
66
+ return (
67
+ await Promise.all(
68
+ libraryIDs.map(async (libraryID) => {
69
+ const models = await this.readAllSliceModelsForLibrary({
70
+ libraryID,
71
+ });
72
+
73
+ return models.map((model) => {
74
+ return {
75
+ libraryID,
76
+ ...model,
77
+ };
78
+ });
79
+ }),
80
+ )
81
+ ).flat();
82
+ };
83
+
84
+ readAllSliceModelsForLibrary = async (
85
+ args: ReadAllSliceModelsForLibraryActionArgs,
86
+ ): Promise<SliceReadHookReturnType[]> => {
87
+ const { sliceIDs } = await this.readSliceLibrary({
88
+ libraryID: args.libraryID,
89
+ });
90
+
91
+ return await Promise.all(
92
+ sliceIDs.map(async (sliceID) => {
93
+ return await this.readSliceModel({
94
+ libraryID: args.libraryID,
95
+ sliceID,
96
+ });
97
+ }),
98
+ );
99
+ };
100
+
101
+ readSliceModel = async (
102
+ args: SliceReadHookData,
103
+ ): Promise<SliceReadHookReturnType> => {
104
+ const {
105
+ data: [model],
106
+ errors: [cause],
107
+ } = await this._hookSystem.callHook("slice:read", {
108
+ libraryID: args.libraryID,
109
+ sliceID: args.sliceID,
110
+ });
111
+
112
+ if (!model) {
113
+ throw new Error(
114
+ `Slice \`${args.sliceID}\` not found in the \`${args.libraryID}\` library.`,
115
+ { cause },
116
+ );
117
+ }
118
+
119
+ return model;
120
+ };
121
+
122
+ readSliceLibrary = async (
123
+ args: SliceLibraryReadHookData,
124
+ ): Promise<SliceLibraryReadHookReturnType> => {
125
+ const {
126
+ data: [library],
127
+ errors: [cause],
128
+ } = await this._hookSystem.callHook("slice-library:read", {
129
+ libraryID: args.libraryID,
130
+ });
131
+
132
+ if (!library) {
133
+ throw new Error(`Slice library \`${args.libraryID}\` not found.`, {
134
+ cause,
135
+ });
136
+ }
137
+
138
+ return library;
139
+ };
140
+
141
+ readAllCustomTypeModels = async (): Promise<
142
+ CustomTypeReadHookReturnType[]
143
+ > => {
144
+ const { ids } = await this.readCustomTypeLibrary();
145
+
146
+ return await Promise.all(
147
+ ids.map(async (id) => {
148
+ return this.readCustomTypeModel({ id });
149
+ }),
150
+ );
151
+ };
152
+
153
+ readCustomTypeModel = async (
154
+ args: CustomTypeReadHookData,
155
+ ): Promise<CustomTypeReadHookReturnType> => {
156
+ const {
157
+ data: [model],
158
+ errors: [cause],
159
+ } = await this._hookSystem.callHook("custom-type:read", {
160
+ id: args.id,
161
+ });
162
+
163
+ if (!model) {
164
+ throw new Error(`Custom type \`${args.id}\` not found.`, { cause });
165
+ }
166
+
167
+ return model;
168
+ };
169
+
170
+ readCustomTypeLibrary =
171
+ async (): Promise<CustomTypeLibraryReadHookReturnType> => {
172
+ const {
173
+ data: [library],
174
+ errors: [cause],
175
+ } = await this._hookSystem.callHook(
176
+ "custom-type-library:read",
177
+ undefined,
178
+ );
179
+
180
+ if (!library) {
181
+ throw new Error(`Couldn't read custom type library.`, {
182
+ cause,
183
+ });
184
+ }
185
+
186
+ return library;
187
+ };
188
+ }
@@ -0,0 +1,51 @@
1
+ import { PluginSystemActions } from "./createPluginSystemActions";
2
+ import { PluginSystemHelpers } from "./createPluginSystemHelpers";
3
+ import { LoadedPlugin } from "./definePlugin";
4
+ import { PrismicProject } from "./types";
5
+
6
+ /**
7
+ * Plugin context shared to plugins and hooks.
8
+ */
9
+ export type PluginSystemContext<
10
+ TPluginOptions extends Record<string, unknown>,
11
+ > = {
12
+ actions: PluginSystemActions;
13
+ helpers: PluginSystemHelpers;
14
+ project: PrismicProject;
15
+ options: TPluginOptions;
16
+ };
17
+
18
+ /**
19
+ * Arguments for `createPluginSystemContext()`.
20
+ *
21
+ * @typeParam TPluginOptions - Options for the plugin's context.
22
+ */
23
+ type CreatePluginSystemContextArgs<
24
+ TPluginOptions extends Record<string, unknown>,
25
+ > = {
26
+ actions: PluginSystemActions;
27
+ helpers: PluginSystemHelpers;
28
+ project: PrismicProject;
29
+ plugin: LoadedPlugin<TPluginOptions>;
30
+ };
31
+
32
+ /**
33
+ * Creates Plugin context.
34
+ *
35
+ * @internal
36
+ */
37
+ export const createPluginSystemContext = <
38
+ TPluginOptions extends Record<string, unknown>,
39
+ >({
40
+ actions,
41
+ helpers,
42
+ project,
43
+ plugin,
44
+ }: CreatePluginSystemContextArgs<TPluginOptions>): PluginSystemContext<TPluginOptions> => {
45
+ return {
46
+ actions,
47
+ helpers,
48
+ project,
49
+ options: plugin.options,
50
+ };
51
+ };
@@ -0,0 +1,129 @@
1
+ import * as fs from "node:fs/promises";
2
+ import * as path from "node:path";
3
+
4
+ import { stripIndent } from "common-tags";
5
+ import * as prettier from "prettier";
6
+
7
+ import { decodePrismicConfig } from "./lib/decodePrismicConfig";
8
+ import { PrismicConfig, PrismicProject } from "./types";
9
+
10
+ type UpdatePrismicConfigOptions = {
11
+ format?: boolean;
12
+ };
13
+
14
+ type FormatOptions = {
15
+ prettier?: prettier.Options;
16
+ /**
17
+ * Determines if a newline is included at the end of the formatted result.
18
+ *
19
+ * @defaultValue `true`
20
+ */
21
+ includeNewlineAtEnd?: boolean;
22
+ };
23
+
24
+ /**
25
+ * Creates Plugin System helpers.
26
+ *
27
+ * @internal
28
+ */
29
+ export const createPluginSystemHelpers = (
30
+ project: PrismicProject,
31
+ ): PluginSystemHelpers => {
32
+ return new PluginSystemHelpers(project);
33
+ };
34
+
35
+ /**
36
+ * Plugin System helpers shared to plugins and hooks.
37
+ */
38
+ export class PluginSystemHelpers {
39
+ /**
40
+ * Project's metadata.
41
+ *
42
+ * @internal
43
+ */
44
+ private _project: PrismicProject;
45
+
46
+ constructor(project: PrismicProject) {
47
+ this._project = project;
48
+ }
49
+
50
+ getProject = async (): Promise<PrismicProject> => {
51
+ const configFilePath = this.joinPathFromRoot("prismic.config.json");
52
+
53
+ let rawConfig: unknown | undefined;
54
+ try {
55
+ const contents = await fs.readFile(configFilePath, "utf8");
56
+ rawConfig = JSON.parse(contents);
57
+ } catch {
58
+ // noop
59
+ }
60
+
61
+ if (!rawConfig) {
62
+ throw new Error(
63
+ "No Prismic config found, please initialize your project with first.",
64
+ );
65
+ }
66
+
67
+ const { value: prismicConfig, error } = decodePrismicConfig(rawConfig);
68
+
69
+ if (error) {
70
+ throw new Error(`Invalid Prismic config. ${error.errors.join(", ")}`);
71
+ }
72
+
73
+ return {
74
+ ...this._project,
75
+ config: prismicConfig,
76
+ };
77
+ };
78
+
79
+ updatePrismicConfig = async (
80
+ prismicConfig: PrismicConfig,
81
+ options?: UpdatePrismicConfigOptions,
82
+ ): Promise<void> => {
83
+ const { value: decodedPrismicConfig, error } =
84
+ decodePrismicConfig(prismicConfig);
85
+
86
+ if (error) {
87
+ throw new Error(
88
+ `Invalid Prismic config provided. ${error.errors.join(", ")}`,
89
+ );
90
+ }
91
+
92
+ const configFilePath = this.joinPathFromRoot("prismic.config.json");
93
+ let content = JSON.stringify(decodedPrismicConfig, null, 2);
94
+
95
+ if (options?.format) {
96
+ content = await this.format(content, configFilePath);
97
+ }
98
+
99
+ await fs.writeFile(configFilePath, content);
100
+ };
101
+
102
+ format = async (
103
+ source: string,
104
+ filePath?: string,
105
+ options?: FormatOptions,
106
+ ): Promise<string> => {
107
+ let formatted = stripIndent(source);
108
+
109
+ const prettierOptions = await prettier.resolveConfig(
110
+ filePath || this._project.root,
111
+ );
112
+
113
+ formatted = await prettier.format(formatted, {
114
+ ...prettierOptions,
115
+ filepath: filePath,
116
+ ...(options?.prettier ?? {}),
117
+ });
118
+
119
+ if (options?.includeNewlineAtEnd === false) {
120
+ formatted = formatted.replace(/[\r\n]+$/, "");
121
+ }
122
+
123
+ return formatted;
124
+ };
125
+
126
+ joinPathFromRoot = (...paths: string[]): string => {
127
+ return path.join(this._project.root, ...paths);
128
+ };
129
+ }