@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,216 @@
1
+ import _module from "node:module";
2
+
3
+ import { defu } from "defu";
4
+
5
+ import { createPluginHookSystem } from "./createPluginHookSystem";
6
+ import {
7
+ createPluginSystemActions,
8
+ PluginSystemActions,
9
+ } from "./createPluginSystemActions";
10
+ import { createPluginSystemContext } from "./createPluginSystemContext";
11
+ import {
12
+ createPluginSystemHelpers,
13
+ PluginSystemHelpers,
14
+ } from "./createPluginSystemHelpers";
15
+ import { LoadedPlugin, Plugin } from "./definePlugin";
16
+ import { HookSystem } from "./lib/HookSystem";
17
+ import {
18
+ PrismicConfigPluginRegistration,
19
+ PluginHookExtraArgs,
20
+ PluginHookTypes,
21
+ PluginHooks,
22
+ PrismicProject,
23
+ } from "./types";
24
+
25
+ /**
26
+ * @internal
27
+ */
28
+ export const REQUIRED_ADAPTER_HOOKS: PluginHookTypes[] = [
29
+ "slice:create",
30
+ "slice:read",
31
+ "slice:rename",
32
+ "slice:delete",
33
+ "slice:update",
34
+ "slice-library:read",
35
+ "custom-type:create",
36
+ "custom-type:read",
37
+ "custom-type:rename",
38
+ "custom-type:delete",
39
+ "custom-type:update",
40
+ "custom-type-library:read",
41
+ ];
42
+
43
+ type PluginSystemRunnerConstructorArgs = {
44
+ project: PrismicProject;
45
+ hookSystem: HookSystem<PluginHooks>;
46
+ nativePlugins: Record<string, Plugin>;
47
+ };
48
+
49
+ /**
50
+ * @internal
51
+ */
52
+ export class PluginSystemRunner {
53
+ private _project: PrismicProject;
54
+ private _hookSystem: HookSystem<PluginHooks>;
55
+ private _nativePlugins: Record<string, Plugin>;
56
+
57
+ /**
58
+ * Plugin System actions provided to hooks.
59
+ *
60
+ * IMPORTANT: Prefer creating your own abstraction over using `rawActions`
61
+ * directly to prevent code breakage if this internal API changes.
62
+ *
63
+ * @internal
64
+ */
65
+ rawActions: PluginSystemActions;
66
+
67
+ /**
68
+ * Plugin System helpers provided to hooks.
69
+ *
70
+ * IMPORTANT: Prefer creating your own abstraction over using `rawHelpers`
71
+ * directly to prevent code breakage if this internal API changes.
72
+ *
73
+ * @internal
74
+ */
75
+ rawHelpers: PluginSystemHelpers;
76
+
77
+ // Methods forwarded to the plugin runner's hook system.
78
+ callHook: HookSystem<PluginHooks>["callHook"];
79
+ hooksForOwner: HookSystem<PluginHooks>["hooksForOwner"];
80
+ hooksForType: HookSystem<PluginHooks>["hooksForType"];
81
+ createScope: HookSystem<PluginHooks>["createScope"];
82
+
83
+ constructor({
84
+ project,
85
+ hookSystem,
86
+ nativePlugins,
87
+ }: PluginSystemRunnerConstructorArgs) {
88
+ this._project = project;
89
+ this._hookSystem = hookSystem;
90
+ this._nativePlugins = nativePlugins;
91
+
92
+ this.rawActions = createPluginSystemActions(
93
+ this._project,
94
+ this._hookSystem,
95
+ );
96
+ this.rawHelpers = createPluginSystemHelpers(this._project);
97
+
98
+ this.callHook = this._hookSystem.callHook.bind(this._hookSystem);
99
+ this.hooksForOwner = this._hookSystem.hooksForOwner.bind(this._hookSystem);
100
+ this.hooksForType = this._hookSystem.hooksForType.bind(this._hookSystem);
101
+ this.createScope = this._hookSystem.createScope.bind(this._hookSystem);
102
+ }
103
+
104
+ private async _loadPlugin(
105
+ pluginRegistration: PrismicConfigPluginRegistration,
106
+ ): Promise<LoadedPlugin> {
107
+ // Sanitize registration
108
+ const { resolve, options = {} } =
109
+ typeof pluginRegistration === "object" && "resolve" in pluginRegistration
110
+ ? pluginRegistration
111
+ : { resolve: pluginRegistration };
112
+
113
+ let plugin: Plugin | undefined = undefined;
114
+
115
+ if (typeof resolve === "string") {
116
+ plugin = this._nativePlugins[resolve];
117
+
118
+ if (!plugin) {
119
+ throw new Error(
120
+ `Could not resolve plugin \`${resolve}\`. Check that it has been installed.`,
121
+ );
122
+ }
123
+ } else {
124
+ plugin = resolve;
125
+ }
126
+
127
+ if (!plugin) {
128
+ throw new Error(`Could not load plugin: \`${resolve}\``);
129
+ }
130
+
131
+ const mergedOptions = defu(options, plugin.defaultOptions || {});
132
+
133
+ return {
134
+ ...plugin,
135
+ resolve,
136
+ options: mergedOptions,
137
+ };
138
+ }
139
+
140
+ private async _setupPlugin(plugin: LoadedPlugin): Promise<void> {
141
+ const context = createPluginSystemContext({
142
+ actions: this.rawActions,
143
+ helpers: this.rawHelpers,
144
+ project: this._project,
145
+ plugin,
146
+ });
147
+ const hookSystemScope = this._hookSystem.createScope<PluginHookExtraArgs>(
148
+ plugin.meta.name,
149
+ [context],
150
+ );
151
+
152
+ // Run plugin setup with actions and context
153
+ try {
154
+ await plugin.setup({
155
+ ...context,
156
+ hook: hookSystemScope.hook,
157
+ unhook: hookSystemScope.unhook,
158
+ });
159
+ } catch (error) {
160
+ if (error instanceof Error) {
161
+ throw new Error(
162
+ `Plugin \`${plugin.meta.name}\` errored during setup: ${error.message}`,
163
+ { cause: error },
164
+ );
165
+ } else {
166
+ throw new Error(
167
+ `Plugin \`${plugin.meta.name}\` errored during setup: ${error}`,
168
+ );
169
+ }
170
+ }
171
+ }
172
+
173
+ private _validateAdapter(adapter: LoadedPlugin): void {
174
+ const hooks = this._hookSystem.hooksForOwner(adapter.meta.name);
175
+ const hookTypes = hooks.map((hook) => hook.meta.type);
176
+
177
+ const missingHooks = REQUIRED_ADAPTER_HOOKS.filter(
178
+ (requiredHookType) => !hookTypes.includes(requiredHookType),
179
+ );
180
+
181
+ if (missingHooks.length) {
182
+ throw new Error(
183
+ `Adapter \`${
184
+ adapter.meta.name
185
+ }\` is missing hooks: \`${missingHooks.join("`, `")}\``,
186
+ );
187
+ }
188
+ }
189
+
190
+ async init(): Promise<void> {
191
+ const adapter = await this._loadPlugin(this._project.config.adapter);
192
+ await this._setupPlugin(adapter);
193
+ this._validateAdapter(adapter);
194
+ }
195
+ }
196
+
197
+ type CreatePluginSystemRunnerArgs = {
198
+ project: PrismicProject;
199
+ nativePlugins: Record<string, Plugin>;
200
+ };
201
+
202
+ /**
203
+ * @internal
204
+ */
205
+ export const createPluginSystemRunner = ({
206
+ project,
207
+ nativePlugins,
208
+ }: CreatePluginSystemRunnerArgs): PluginSystemRunner => {
209
+ const hookSystem = createPluginHookSystem();
210
+
211
+ return new PluginSystemRunner({
212
+ project,
213
+ hookSystem,
214
+ nativePlugins,
215
+ });
216
+ };
@@ -0,0 +1,48 @@
1
+ import { PluginSystemContext } from "./createPluginSystemContext";
2
+ import { CreateScopeReturnType } from "./lib/HookSystem";
3
+ import { PluginOptions, PluginHookExtraArgs, PluginHooks } from "./types";
4
+
5
+ /**
6
+ * Plugin definition.
7
+ */
8
+ export type Plugin<
9
+ TPluginOptions extends Record<string, unknown> = Record<string, unknown>,
10
+ > = {
11
+ /**
12
+ * Information about the plugin.
13
+ */
14
+ meta: {
15
+ name: string;
16
+ };
17
+
18
+ /**
19
+ * Default options.
20
+ */
21
+ defaultOptions?: TPluginOptions;
22
+
23
+ /**
24
+ * Plugin setup.
25
+ */
26
+ setup: (
27
+ context: Omit<PluginSystemContext<TPluginOptions>, "actions"> &
28
+ Pick<
29
+ CreateScopeReturnType<PluginHooks, PluginHookExtraArgs<TPluginOptions>>,
30
+ "hook" | "unhook"
31
+ >,
32
+ ) => void | Promise<void>;
33
+ };
34
+
35
+ /**
36
+ * @internal
37
+ */
38
+ export type LoadedPlugin<TPluginOptions extends PluginOptions = PluginOptions> =
39
+ Plugin<TPluginOptions> & {
40
+ resolve: string | Plugin;
41
+ options: TPluginOptions;
42
+ };
43
+
44
+ export const definePlugin = <
45
+ TPluginOptions extends PluginOptions = PluginOptions,
46
+ >(
47
+ plugin: Plugin<TPluginOptions>,
48
+ ): Plugin<TPluginOptions> => plugin;
@@ -0,0 +1,19 @@
1
+ import * as path from "node:path";
2
+
3
+ import {
4
+ buildCustomTypeLibraryDirectoryPath,
5
+ BuildCustomTypeLibraryDirectoryPathArgs,
6
+ } from "./buildCustomTypeLibraryDirectoryPath";
7
+
8
+ export type BuildCustomTypeDirectoryPathArgs = {
9
+ customTypeID: string;
10
+ } & BuildCustomTypeLibraryDirectoryPathArgs;
11
+
12
+ export const buildCustomTypeDirectoryPath = (
13
+ args: BuildCustomTypeDirectoryPathArgs,
14
+ ): string => {
15
+ return path.join(
16
+ buildCustomTypeLibraryDirectoryPath(args),
17
+ args.customTypeID,
18
+ );
19
+ };
@@ -0,0 +1,16 @@
1
+ import * as path from "node:path";
2
+
3
+ import {
4
+ buildCustomTypeDirectoryPath,
5
+ BuildCustomTypeDirectoryPathArgs,
6
+ } from "./buildCustomTypeDirectoryPath";
7
+
8
+ export type BuildCustomTypeFilePathArgs = {
9
+ filename: string;
10
+ } & BuildCustomTypeDirectoryPathArgs;
11
+
12
+ export const buildCustomTypeFilePath = (
13
+ args: BuildCustomTypeFilePathArgs,
14
+ ): string => {
15
+ return path.join(buildCustomTypeDirectoryPath(args), args.filename);
16
+ };
@@ -0,0 +1,16 @@
1
+ import { PluginSystemHelpers } from "../createPluginSystemHelpers";
2
+
3
+ import { CUSTOM_TYPES_LIBRARY_DIRNAME } from "./constants";
4
+
5
+ export type BuildCustomTypeLibraryDirectoryPathArgs = {
6
+ absolute?: boolean;
7
+ helpers: PluginSystemHelpers;
8
+ };
9
+
10
+ export const buildCustomTypeLibraryDirectoryPath = (
11
+ args: BuildCustomTypeLibraryDirectoryPathArgs,
12
+ ): string => {
13
+ return args.absolute
14
+ ? args.helpers.joinPathFromRoot(CUSTOM_TYPES_LIBRARY_DIRNAME)
15
+ : CUSTOM_TYPES_LIBRARY_DIRNAME;
16
+ };
@@ -0,0 +1,25 @@
1
+ import * as path from "node:path";
2
+
3
+ import {
4
+ buildSliceLibraryDirectoryPath,
5
+ BuildSliceLibraryDirectoryPathArgs,
6
+ } from "./buildSliceLibraryDirectoryPath";
7
+ import { pascalCase } from "./lib/pascalCase";
8
+ import {
9
+ resolveSliceModel,
10
+ ResolveSliceModelArgs,
11
+ } from "./lib/resolveSliceModel";
12
+
13
+ export type BuildSliceDirectoryPathArgs = BuildSliceLibraryDirectoryPathArgs &
14
+ ResolveSliceModelArgs;
15
+
16
+ export const buildSliceDirectoryPath = async (
17
+ args: BuildSliceDirectoryPathArgs,
18
+ ): Promise<string> => {
19
+ const model = await resolveSliceModel(args);
20
+
21
+ return path.join(
22
+ buildSliceLibraryDirectoryPath(args),
23
+ pascalCase(model.name),
24
+ );
25
+ };
@@ -0,0 +1,16 @@
1
+ import * as path from "node:path";
2
+
3
+ import {
4
+ buildSliceDirectoryPath,
5
+ BuildSliceDirectoryPathArgs,
6
+ } from "./buildSliceDirectoryPath";
7
+
8
+ export type BuildSliceFilePathArgs = {
9
+ filename: string;
10
+ } & BuildSliceDirectoryPathArgs;
11
+
12
+ export const buildSliceFilePath = async (
13
+ args: BuildSliceFilePathArgs,
14
+ ): Promise<string> => {
15
+ return path.join(await buildSliceDirectoryPath(args), args.filename);
16
+ };
@@ -0,0 +1,17 @@
1
+ import * as path from "node:path";
2
+
3
+ import { PluginSystemHelpers } from "../createPluginSystemHelpers";
4
+
5
+ export type BuildSliceLibraryDirectoryPathArgs = {
6
+ libraryID: string;
7
+ absolute?: boolean;
8
+ helpers: PluginSystemHelpers;
9
+ };
10
+
11
+ export const buildSliceLibraryDirectoryPath = (
12
+ args: BuildSliceLibraryDirectoryPathArgs,
13
+ ): string => {
14
+ return args.absolute
15
+ ? args.helpers.joinPathFromRoot(args.libraryID)
16
+ : path.join(args.libraryID);
17
+ };
@@ -0,0 +1,16 @@
1
+ import { PluginSystemHelpers } from "../createPluginSystemHelpers";
2
+
3
+ import { checkPathExists } from "./lib/checkPathExists";
4
+
5
+ export type CheckHasProjectFileArgs = {
6
+ filename: string;
7
+ helpers: PluginSystemHelpers;
8
+ };
9
+
10
+ export const checkHasProjectFile = async (
11
+ args: CheckHasProjectFileArgs,
12
+ ): Promise<boolean> => {
13
+ const filePath = args.helpers.joinPathFromRoot(args.filename);
14
+
15
+ return checkPathExists(filePath);
16
+ };
@@ -0,0 +1,16 @@
1
+ import {
2
+ checkHasProjectFile,
3
+ CheckHasProjectFileArgs,
4
+ } from "./checkHasProjectFile";
5
+ import { TSCONFIG_FILENAME } from "./constants";
6
+
7
+ type CheckIsTypeScriptProjectArgs = Omit<CheckHasProjectFileArgs, "filename">;
8
+
9
+ export const checkIsTypeScriptProject = async (
10
+ args: CheckIsTypeScriptProjectArgs,
11
+ ): Promise<boolean> => {
12
+ return checkHasProjectFile({
13
+ ...args,
14
+ filename: TSCONFIG_FILENAME,
15
+ });
16
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Banner text added to files that should not be edited by users.
3
+ */
4
+ export const NON_EDITABLE_FILE_BANNER =
5
+ "// Code generated by Prismic. DO NOT EDIT.";
6
+
7
+ export const CUSTOM_TYPES_LIBRARY_DIRNAME = "customtypes";
8
+
9
+ export const CUSTOM_TYPE_MODEL_FILENAME = "index.json";
10
+
11
+ export const SHARED_SLICE_MODEL_FILENAME = "model.json";
12
+
13
+ export const TSCONFIG_FILENAME = "tsconfig.json";
14
+
15
+ export const GLOBAL_TYPESCRIPT_TYPES_FILENAME = "prismicio-types.d.ts";
@@ -0,0 +1,24 @@
1
+ import {
2
+ buildCustomTypeDirectoryPath,
3
+ BuildCustomTypeDirectoryPathArgs,
4
+ } from "./buildCustomTypeDirectoryPath";
5
+ import { deleteProjectFile } from "./deleteProjectFile";
6
+
7
+ export type DeleteCustomTypeDirectoryArgs = BuildCustomTypeDirectoryPathArgs;
8
+
9
+ export const deleteCustomTypeDirectory = async (
10
+ args: DeleteCustomTypeDirectoryArgs,
11
+ ): Promise<string> => {
12
+ const dir = buildCustomTypeDirectoryPath(args);
13
+ const relativeDir = buildCustomTypeDirectoryPath({
14
+ ...args,
15
+ absolute: false,
16
+ });
17
+
18
+ await deleteProjectFile({
19
+ filename: relativeDir,
20
+ helpers: args.helpers,
21
+ });
22
+
23
+ return dir;
24
+ };
@@ -0,0 +1,18 @@
1
+ import { PluginSystemHelpers } from "../createPluginSystemHelpers";
2
+
3
+ import * as fs from "./lib/fsLimit";
4
+
5
+ export type DeleteProjectFileArgs = {
6
+ filename: string;
7
+ helpers: PluginSystemHelpers;
8
+ };
9
+
10
+ export const deleteProjectFile = async (
11
+ args: DeleteProjectFileArgs,
12
+ ): Promise<string> => {
13
+ const filePath = args.helpers.joinPathFromRoot(args.filename);
14
+
15
+ await fs.rm(filePath, { recursive: true });
16
+
17
+ return filePath;
18
+ };
@@ -0,0 +1,24 @@
1
+ import {
2
+ buildSliceDirectoryPath,
3
+ BuildSliceDirectoryPathArgs,
4
+ } from "./buildSliceDirectoryPath";
5
+ import { deleteProjectFile } from "./deleteProjectFile";
6
+
7
+ export type DeleteSliceDirectoryArgs = BuildSliceDirectoryPathArgs;
8
+
9
+ export const deleteSliceDirectory = async (
10
+ args: DeleteSliceDirectoryArgs,
11
+ ): Promise<string> => {
12
+ const dir = await buildSliceDirectoryPath(args);
13
+ const relativeDir = await buildSliceDirectoryPath({
14
+ ...args,
15
+ absolute: false,
16
+ });
17
+
18
+ await deleteProjectFile({
19
+ filename: relativeDir,
20
+ helpers: args.helpers,
21
+ });
22
+
23
+ return dir;
24
+ };
@@ -0,0 +1,24 @@
1
+ import {
2
+ buildSliceFilePath,
3
+ BuildSliceFilePathArgs,
4
+ } from "./buildSliceFilePath";
5
+ import { deleteProjectFile } from "./deleteProjectFile";
6
+
7
+ export type DeleteSliceFileArgs = BuildSliceFilePathArgs;
8
+
9
+ export const deleteSliceFile = async (
10
+ args: DeleteSliceFileArgs,
11
+ ): Promise<string> => {
12
+ const filePath = await buildSliceFilePath(args);
13
+ const relativeFilePath = await buildSliceFilePath({
14
+ ...args,
15
+ absolute: false,
16
+ });
17
+
18
+ await deleteProjectFile({
19
+ filename: relativeFilePath,
20
+ helpers: args.helpers,
21
+ });
22
+
23
+ return filePath;
24
+ };
@@ -0,0 +1,103 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // Project helpers
4
+ //
5
+ ///////////////////////////////////////////////////////////////////////////////
6
+
7
+ export { checkIsTypeScriptProject } from "./checkIsTypeScriptProject";
8
+
9
+ export { upsertGlobalTypeScriptTypes } from "./upsertGlobalTypeScriptTypes";
10
+ export type { UpsertGlobalTypeScriptTypesArgs } from "./upsertGlobalTypeScriptTypes";
11
+
12
+ export { checkHasProjectFile } from "./checkHasProjectFile";
13
+ export type { CheckHasProjectFileArgs } from "./checkHasProjectFile";
14
+
15
+ export { writeProjectFile } from "./writeProjectFile";
16
+ export type { WriteProjectFileArgs } from "./writeProjectFile";
17
+
18
+ export { readProjectFile } from "./readProjectFile";
19
+ export type { ReadProjectFileArgs } from "./readProjectFile";
20
+
21
+ export { deleteProjectFile } from "./deleteProjectFile";
22
+ export type { DeleteProjectFileArgs } from "./deleteProjectFile";
23
+
24
+ ///////////////////////////////////////////////////////////////////////////////
25
+ //
26
+ // Slice helpers
27
+ //
28
+ ///////////////////////////////////////////////////////////////////////////////
29
+
30
+ export { buildSliceDirectoryPath } from "./buildSliceDirectoryPath";
31
+ export type { BuildSliceDirectoryPathArgs } from "./buildSliceDirectoryPath";
32
+
33
+ export { buildSliceFilePath } from "./buildSliceFilePath";
34
+ export type { BuildSliceFilePathArgs } from "./buildSliceFilePath";
35
+
36
+ export { buildSliceLibraryDirectoryPath } from "./buildSliceLibraryDirectoryPath";
37
+ export type { BuildSliceLibraryDirectoryPathArgs } from "./buildSliceLibraryDirectoryPath";
38
+
39
+ export { readSliceLibrary } from "./readSliceLibrary";
40
+ export type {
41
+ ReadSliceLibraryArgs,
42
+ ReadSliceLibraryReturnType,
43
+ } from "./readSliceLibrary";
44
+
45
+ export { writeSliceFile } from "./writeSliceFile";
46
+ export type { WriteSliceFileArgs } from "./writeSliceFile";
47
+
48
+ export { writeSliceModel } from "./writeSliceModel";
49
+ export type { WriteSliceModelArgs } from "./writeSliceModel";
50
+
51
+ export { readSliceFile } from "./readSliceFile";
52
+ export type { ReadSliceFileArgs } from "./readSliceFile";
53
+
54
+ export { readSliceModel } from "./readSliceModel";
55
+ export type {
56
+ ReadSliceModelArgs,
57
+ ReadSliceModelReturnType,
58
+ } from "./readSliceModel";
59
+
60
+ export { renameSlice } from "./renameSlice";
61
+ export type { RenameSliceArgs } from "./renameSlice";
62
+
63
+ export { deleteSliceFile } from "./deleteSliceFile";
64
+ export type { DeleteSliceFileArgs } from "./deleteSliceFile";
65
+
66
+ export { deleteSliceDirectory } from "./deleteSliceDirectory";
67
+ export type { DeleteSliceDirectoryArgs } from "./deleteSliceDirectory";
68
+
69
+ ///////////////////////////////////////////////////////////////////////////////
70
+ //
71
+ // Custom type helpers
72
+ //
73
+ ///////////////////////////////////////////////////////////////////////////////
74
+
75
+ export { buildCustomTypeDirectoryPath } from "./buildCustomTypeDirectoryPath";
76
+ export type { BuildCustomTypeDirectoryPathArgs } from "./buildCustomTypeDirectoryPath";
77
+
78
+ export { buildCustomTypeFilePath } from "./buildCustomTypeFilePath";
79
+ export type { BuildCustomTypeFilePathArgs } from "./buildCustomTypeFilePath";
80
+
81
+ export { buildCustomTypeLibraryDirectoryPath } from "./buildCustomTypeLibraryDirectoryPath";
82
+ export type { BuildCustomTypeLibraryDirectoryPathArgs } from "./buildCustomTypeLibraryDirectoryPath";
83
+
84
+ export { readCustomTypeLibrary } from "./readCustomTypeLibrary";
85
+ export type {
86
+ ReadCustomTypeLibraryArgs,
87
+ ReadCustomTypeLibraryReturnType,
88
+ } from "./readCustomTypeLibrary";
89
+
90
+ export { writeCustomTypeModel } from "./writeCustomTypeModel";
91
+ export type { WriteCustomTypeModelArgs } from "./writeCustomTypeModel";
92
+
93
+ export { readCustomTypeModel } from "./readCustomTypeModel";
94
+ export type {
95
+ ReadCustomTypeModelArgs,
96
+ ReadCustomTypeModelReturnType,
97
+ } from "./readCustomTypeModel";
98
+
99
+ export { renameCustomType } from "./renameCustomType";
100
+ export type { RenameCustomTypeArgs } from "./renameCustomType";
101
+
102
+ export { deleteCustomTypeDirectory } from "./deleteCustomTypeDirectory";
103
+ export type { DeleteCustomTypeDirectoryArgs } from "./deleteCustomTypeDirectory";
@@ -0,0 +1,13 @@
1
+ import { PathLike } from "node:fs";
2
+
3
+ import * as fs from "./fsLimit";
4
+
5
+ export async function checkPathExists(path: PathLike): Promise<boolean> {
6
+ try {
7
+ await fs.access(path);
8
+
9
+ return true;
10
+ } catch {
11
+ return false;
12
+ }
13
+ }