@slicemachine/adapter-sveltekit 0.0.0 → 0.3.10-dev-adapter-svelte-kit.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/_node_modules/data-uri-to-buffer/dist/index.cjs +41 -0
  4. package/dist/_node_modules/data-uri-to-buffer/dist/index.cjs.map +1 -0
  5. package/dist/_node_modules/data-uri-to-buffer/dist/index.js +41 -0
  6. package/dist/_node_modules/data-uri-to-buffer/dist/index.js.map +1 -0
  7. package/dist/_node_modules/fetch-blob/file.cjs +61 -0
  8. package/dist/_node_modules/fetch-blob/file.cjs.map +1 -0
  9. package/dist/_node_modules/fetch-blob/file.js +61 -0
  10. package/dist/_node_modules/fetch-blob/file.js.map +1 -0
  11. package/dist/_node_modules/fetch-blob/index.cjs +223 -0
  12. package/dist/_node_modules/fetch-blob/index.cjs.map +1 -0
  13. package/dist/_node_modules/fetch-blob/index.js +223 -0
  14. package/dist/_node_modules/fetch-blob/index.js.map +1 -0
  15. package/dist/_node_modules/fetch-blob/streams.cjs +43 -0
  16. package/dist/_node_modules/fetch-blob/streams.cjs.map +1 -0
  17. package/dist/_node_modules/fetch-blob/streams.js +42 -0
  18. package/dist/_node_modules/fetch-blob/streams.js.map +1 -0
  19. package/dist/_node_modules/formdata-polyfill/esm.min.cjs +116 -0
  20. package/dist/_node_modules/formdata-polyfill/esm.min.cjs.map +1 -0
  21. package/dist/_node_modules/formdata-polyfill/esm.min.js +116 -0
  22. package/dist/_node_modules/formdata-polyfill/esm.min.js.map +1 -0
  23. package/dist/_node_modules/node-domexception/index.cjs +11 -0
  24. package/dist/_node_modules/node-domexception/index.cjs.map +1 -0
  25. package/dist/_node_modules/node-domexception/index.js +10 -0
  26. package/dist/_node_modules/node-domexception/index.js.map +1 -0
  27. package/dist/_node_modules/node-fetch/src/body.cjs +262 -0
  28. package/dist/_node_modules/node-fetch/src/body.cjs.map +1 -0
  29. package/dist/_node_modules/node-fetch/src/body.js +262 -0
  30. package/dist/_node_modules/node-fetch/src/body.js.map +1 -0
  31. package/dist/_node_modules/node-fetch/src/errors/abort-error.cjs +10 -0
  32. package/dist/_node_modules/node-fetch/src/errors/abort-error.cjs.map +1 -0
  33. package/dist/_node_modules/node-fetch/src/errors/abort-error.js +10 -0
  34. package/dist/_node_modules/node-fetch/src/errors/abort-error.js.map +1 -0
  35. package/dist/_node_modules/node-fetch/src/errors/base.cjs +17 -0
  36. package/dist/_node_modules/node-fetch/src/errors/base.cjs.map +1 -0
  37. package/dist/_node_modules/node-fetch/src/errors/base.js +17 -0
  38. package/dist/_node_modules/node-fetch/src/errors/base.js.map +1 -0
  39. package/dist/_node_modules/node-fetch/src/errors/fetch-error.cjs +19 -0
  40. package/dist/_node_modules/node-fetch/src/errors/fetch-error.cjs.map +1 -0
  41. package/dist/_node_modules/node-fetch/src/errors/fetch-error.js +19 -0
  42. package/dist/_node_modules/node-fetch/src/errors/fetch-error.js.map +1 -0
  43. package/dist/_node_modules/node-fetch/src/headers.cjs +190 -0
  44. package/dist/_node_modules/node-fetch/src/headers.cjs.map +1 -0
  45. package/dist/_node_modules/node-fetch/src/headers.js +190 -0
  46. package/dist/_node_modules/node-fetch/src/headers.js.map +1 -0
  47. package/dist/_node_modules/node-fetch/src/index.cjs +292 -0
  48. package/dist/_node_modules/node-fetch/src/index.cjs.map +1 -0
  49. package/dist/_node_modules/node-fetch/src/index.js +292 -0
  50. package/dist/_node_modules/node-fetch/src/index.js.map +1 -0
  51. package/dist/_node_modules/node-fetch/src/request.cjs +206 -0
  52. package/dist/_node_modules/node-fetch/src/request.cjs.map +1 -0
  53. package/dist/_node_modules/node-fetch/src/request.js +206 -0
  54. package/dist/_node_modules/node-fetch/src/request.js.map +1 -0
  55. package/dist/_node_modules/node-fetch/src/response.cjs +122 -0
  56. package/dist/_node_modules/node-fetch/src/response.cjs.map +1 -0
  57. package/dist/_node_modules/node-fetch/src/response.js +123 -0
  58. package/dist/_node_modules/node-fetch/src/response.js.map +1 -0
  59. package/dist/_node_modules/node-fetch/src/utils/get-search.cjs +12 -0
  60. package/dist/_node_modules/node-fetch/src/utils/get-search.cjs.map +1 -0
  61. package/dist/_node_modules/node-fetch/src/utils/get-search.js +12 -0
  62. package/dist/_node_modules/node-fetch/src/utils/get-search.js.map +1 -0
  63. package/dist/_node_modules/node-fetch/src/utils/is-redirect.cjs +8 -0
  64. package/dist/_node_modules/node-fetch/src/utils/is-redirect.cjs.map +1 -0
  65. package/dist/_node_modules/node-fetch/src/utils/is-redirect.js +8 -0
  66. package/dist/_node_modules/node-fetch/src/utils/is-redirect.js.map +1 -0
  67. package/dist/_node_modules/node-fetch/src/utils/is.cjs +28 -0
  68. package/dist/_node_modules/node-fetch/src/utils/is.cjs.map +1 -0
  69. package/dist/_node_modules/node-fetch/src/utils/is.js +28 -0
  70. package/dist/_node_modules/node-fetch/src/utils/is.js.map +1 -0
  71. package/dist/_node_modules/node-fetch/src/utils/multipart-parser.cjs +356 -0
  72. package/dist/_node_modules/node-fetch/src/utils/multipart-parser.cjs.map +1 -0
  73. package/dist/_node_modules/node-fetch/src/utils/multipart-parser.js +356 -0
  74. package/dist/_node_modules/node-fetch/src/utils/multipart-parser.js.map +1 -0
  75. package/dist/_node_modules/node-fetch/src/utils/referrer.cjs +149 -0
  76. package/dist/_node_modules/node-fetch/src/utils/referrer.cjs.map +1 -0
  77. package/dist/_node_modules/node-fetch/src/utils/referrer.js +149 -0
  78. package/dist/_node_modules/node-fetch/src/utils/referrer.js.map +1 -0
  79. package/dist/_node_modules/web-streams-polyfill/dist/ponyfill.es2018.cjs +3822 -0
  80. package/dist/_node_modules/web-streams-polyfill/dist/ponyfill.es2018.cjs.map +1 -0
  81. package/dist/_node_modules/web-streams-polyfill/dist/ponyfill.es2018.js +3822 -0
  82. package/dist/_node_modules/web-streams-polyfill/dist/ponyfill.es2018.js.map +1 -0
  83. package/dist/_virtual/_commonjsHelpers.cjs +5 -0
  84. package/dist/_virtual/_commonjsHelpers.cjs.map +1 -0
  85. package/dist/_virtual/_commonjsHelpers.js +5 -0
  86. package/dist/_virtual/_commonjsHelpers.js.map +1 -0
  87. package/dist/_virtual/ponyfill.es2018.cjs +5 -0
  88. package/dist/_virtual/ponyfill.es2018.cjs.map +1 -0
  89. package/dist/_virtual/ponyfill.es2018.js +5 -0
  90. package/dist/_virtual/ponyfill.es2018.js.map +1 -0
  91. package/dist/constants.cjs +5 -0
  92. package/dist/constants.cjs.map +1 -0
  93. package/dist/constants.d.ts +4 -0
  94. package/dist/constants.js +5 -0
  95. package/dist/constants.js.map +1 -0
  96. package/dist/hooks/project-init.cjs +211 -0
  97. package/dist/hooks/project-init.cjs.map +1 -0
  98. package/dist/hooks/project-init.d.ts +3 -0
  99. package/dist/hooks/project-init.js +194 -0
  100. package/dist/hooks/project-init.js.map +1 -0
  101. package/dist/hooks/slice-create.cjs +69 -0
  102. package/dist/hooks/slice-create.cjs.map +1 -0
  103. package/dist/hooks/slice-create.d.ts +3 -0
  104. package/dist/hooks/slice-create.js +69 -0
  105. package/dist/hooks/slice-create.js.map +1 -0
  106. package/dist/hooks/sliceSimulator-setup-read.cjs +144 -0
  107. package/dist/hooks/sliceSimulator-setup-read.cjs.map +1 -0
  108. package/dist/hooks/sliceSimulator-setup-read.d.ts +3 -0
  109. package/dist/hooks/sliceSimulator-setup-read.js +144 -0
  110. package/dist/hooks/sliceSimulator-setup-read.js.map +1 -0
  111. package/dist/hooks/snippet-read.cjs +114 -0
  112. package/dist/hooks/snippet-read.cjs.map +1 -0
  113. package/dist/hooks/snippet-read.d.ts +3 -0
  114. package/dist/hooks/snippet-read.js +114 -0
  115. package/dist/hooks/snippet-read.js.map +1 -0
  116. package/dist/index.cjs +4 -0
  117. package/dist/index.cjs.map +1 -0
  118. package/dist/index.d.ts +2 -0
  119. package/dist/index.js +5 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/lib/checkIsTypeScriptProject.cjs +8 -0
  122. package/dist/lib/checkIsTypeScriptProject.cjs.map +1 -0
  123. package/dist/lib/checkIsTypeScriptProject.d.ts +8 -0
  124. package/dist/lib/checkIsTypeScriptProject.js +8 -0
  125. package/dist/lib/checkIsTypeScriptProject.js.map +1 -0
  126. package/dist/lib/getJSFileExtension.cjs +12 -0
  127. package/dist/lib/getJSFileExtension.cjs.map +1 -0
  128. package/dist/lib/getJSFileExtension.d.ts +5 -0
  129. package/dist/lib/getJSFileExtension.js +12 -0
  130. package/dist/lib/getJSFileExtension.js.map +1 -0
  131. package/dist/lib/pascalCase.cjs +8 -0
  132. package/dist/lib/pascalCase.cjs.map +1 -0
  133. package/dist/lib/pascalCase.d.ts +8 -0
  134. package/dist/lib/pascalCase.js +8 -0
  135. package/dist/lib/pascalCase.js.map +1 -0
  136. package/dist/lib/rejectIfNecessary.cjs +10 -0
  137. package/dist/lib/rejectIfNecessary.cjs.map +1 -0
  138. package/dist/lib/rejectIfNecessary.d.ts +1 -0
  139. package/dist/lib/rejectIfNecessary.js +10 -0
  140. package/dist/lib/rejectIfNecessary.js.map +1 -0
  141. package/dist/lib/upsertSliceLibraryIndexFile.cjs +67 -0
  142. package/dist/lib/upsertSliceLibraryIndexFile.cjs.map +1 -0
  143. package/dist/lib/upsertSliceLibraryIndexFile.d.ts +7 -0
  144. package/dist/lib/upsertSliceLibraryIndexFile.js +50 -0
  145. package/dist/lib/upsertSliceLibraryIndexFile.js.map +1 -0
  146. package/dist/packages/adapter-sveltekit/package.json.cjs +5 -0
  147. package/dist/packages/adapter-sveltekit/package.json.cjs.map +1 -0
  148. package/dist/packages/adapter-sveltekit/package.json.js +5 -0
  149. package/dist/packages/adapter-sveltekit/package.json.js.map +1 -0
  150. package/dist/plugin.cjs +204 -0
  151. package/dist/plugin.cjs.map +1 -0
  152. package/dist/plugin.d.ts +2 -0
  153. package/dist/plugin.js +204 -0
  154. package/dist/plugin.js.map +1 -0
  155. package/dist/simulator/SliceSimulator.svelte +65 -0
  156. package/dist/simulator/SliceSimulator.svelte.d.ts +35 -0
  157. package/dist/simulator/index.d.ts +1 -0
  158. package/dist/simulator/index.js +1 -0
  159. package/dist/types.d.ts +21 -0
  160. package/package.json +111 -2
  161. package/src/constants.ts +5 -0
  162. package/src/hooks/project-init.ts +265 -0
  163. package/src/hooks/slice-create.ts +120 -0
  164. package/src/hooks/sliceSimulator-setup-read.ts +203 -0
  165. package/src/hooks/snippet-read.ts +163 -0
  166. package/src/index.ts +3 -0
  167. package/src/lib/checkIsTypeScriptProject.ts +18 -0
  168. package/src/lib/getJSFileExtension.ts +22 -0
  169. package/src/lib/pascalCase.ts +12 -0
  170. package/src/lib/rejectIfNecessary.ts +16 -0
  171. package/src/lib/upsertSliceLibraryIndexFile.ts +77 -0
  172. package/src/plugin.ts +275 -0
  173. package/src/simulator/SliceSimulator.svelte +65 -0
  174. package/src/simulator/index.ts +1 -0
  175. package/src/types.ts +23 -0
@@ -0,0 +1,265 @@
1
+ import * as path from "node:path";
2
+ import type {
3
+ ProjectInitHook,
4
+ ProjectInitHookData,
5
+ SliceMachineContext,
6
+ } from "@slicemachine/plugin-kit";
7
+ import {
8
+ checkHasProjectFile,
9
+ writeProjectFile,
10
+ } from "@slicemachine/plugin-kit/fs";
11
+ import { source } from "common-tags";
12
+
13
+ import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
14
+ import { getJSFileExtension } from "../lib/getJSFileExtension";
15
+ import { rejectIfNecessary } from "../lib/rejectIfNecessary";
16
+
17
+ import type { PluginOptions } from "../types";
18
+ import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
19
+
20
+ type InstallDependenciesArgs = {
21
+ installDependencies: ProjectInitHookData["installDependencies"];
22
+ };
23
+
24
+ const installDependencies = async ({
25
+ installDependencies,
26
+ }: InstallDependenciesArgs) => {
27
+ await installDependencies({
28
+ dependencies: {
29
+ "@prismicio/client": "latest",
30
+ "@prismicio/svelte": "latest",
31
+ },
32
+ });
33
+ };
34
+
35
+ type CreatePrismicIOFileArgs = SliceMachineContext<PluginOptions>;
36
+
37
+ const createPrismicIOFile = async ({
38
+ helpers,
39
+ options,
40
+ }: CreatePrismicIOFileArgs) => {
41
+ const isTypeScriptProject = await checkIsTypeScriptProject({
42
+ helpers,
43
+ options,
44
+ });
45
+ const extension = await getJSFileExtension({ helpers, options });
46
+ const filename = path.join(`src/lib/prismicio.${extension}`);
47
+
48
+ if (await checkHasProjectFile({ filename, helpers })) {
49
+ return;
50
+ }
51
+
52
+ let createClientContents: string;
53
+
54
+ if (isTypeScriptProject) {
55
+ createClientContents = source`
56
+ /**
57
+ * Creates a Prismic client for the project's repository. The client is used to
58
+ * query content from the Prismic API.
59
+ *
60
+ * @param config - Configuration for the Prismic client.
61
+ */
62
+ export const createClient = (config: prismic.ClientConfig = {}) => {
63
+ const client = prismic.createClient(repositoryName, {
64
+ routes,
65
+ ...config,
66
+ });
67
+
68
+ return client;
69
+ };
70
+ `;
71
+ } else {
72
+ createClientContents = source`
73
+ /**
74
+ * Creates a Prismic client for the project's repository. The client is used to
75
+ * query content from the Prismic API.
76
+ *
77
+ * @param {prismic.ClientConfig} config - Configuration for the Prismic client.
78
+ */
79
+ export const createClient = (config = {}) => {
80
+ const client = prismic.createClient(repositoryName, {
81
+ routes,
82
+ ...config,
83
+ });
84
+
85
+ return client;
86
+ };
87
+ `;
88
+ }
89
+
90
+ let contents: string;
91
+
92
+ if (isTypeScriptProject) {
93
+ contents = source`
94
+ import * as prismic from "@prismicio/client";
95
+ import config from "../../slicemachine.config.json";
96
+
97
+ /**
98
+ * The project's Prismic repository name.
99
+ */
100
+ export const repositoryName = config.repositoryName;
101
+
102
+ /**
103
+ * A list of Route Resolver objects that define how a document's \`url\` field is resolved.
104
+ *
105
+ * {@link https://prismic.io/docs/route-resolver#route-resolver}
106
+ */
107
+ // TODO: Update the routes array to match your project's route structure.
108
+ const routes: prismic.ClientConfig["routes"] = [
109
+ {
110
+ type: "homepage",
111
+ path: "/",
112
+ },
113
+ {
114
+ type: "page",
115
+ path: "/:uid",
116
+ },
117
+ ];
118
+
119
+ ${createClientContents}
120
+ `;
121
+ } else {
122
+ contents = source`
123
+ import * as prismic from "@prismicio/client";
124
+ import config from "../../slicemachine.config.json";
125
+
126
+ /**
127
+ * The project's Prismic repository name.
128
+ */
129
+ export const repositoryName = config.repositoryName;
130
+
131
+ /**
132
+ * A list of Route Resolver objects that define how a document's \`url\` field is resolved.
133
+ *
134
+ * {@link https://prismic.io/docs/route-resolver#route-resolver}
135
+ *
136
+ * @type {prismic.ClientConfig["routes"]}
137
+ */
138
+ // TODO: Update the routes array to match your project's route structure.
139
+ const routes = [
140
+ {
141
+ type: "homepage",
142
+ path: "/",
143
+ },
144
+ {
145
+ type: "page",
146
+ path: "/:uid",
147
+ },
148
+ ];
149
+
150
+ ${createClientContents}
151
+ `;
152
+ }
153
+
154
+ await writeProjectFile({
155
+ filename,
156
+ contents,
157
+ format: options.format,
158
+ helpers,
159
+ });
160
+ };
161
+
162
+ const createSliceSimulatorPage = async ({
163
+ helpers,
164
+ options,
165
+ }: SliceMachineContext<PluginOptions>) => {
166
+ const filename = path.join(
167
+ "src",
168
+ "routes",
169
+ "slice-simulator",
170
+ "+page.svelte",
171
+ );
172
+
173
+ if (await checkHasProjectFile({ filename, helpers })) {
174
+ return;
175
+ }
176
+
177
+ const contents = source`
178
+ <script>
179
+ import { SliceSimulator } from '@slicemachine/adapter-sveltekit/simulator';
180
+ import { SliceZone } from '@prismicio/svelte';
181
+ import { components } from '$lib/slices';
182
+ </script>
183
+
184
+ <SliceSimulator let:slices>
185
+ <SliceZone {slices} {components} />
186
+ </SliceSimulator>
187
+ `;
188
+
189
+ await writeProjectFile({
190
+ filename,
191
+ contents,
192
+ format: options.format,
193
+ formatOptions: {
194
+ prettier: {
195
+ plugins: ["prettier-plugin-svelte"],
196
+ parser: "svelte",
197
+ },
198
+ },
199
+ helpers,
200
+ });
201
+ };
202
+
203
+ const modifySliceMachineConfig = async ({
204
+ helpers,
205
+ options,
206
+ actions,
207
+ }: SliceMachineContext<PluginOptions>) => {
208
+ const project = await helpers.getProject();
209
+
210
+ // Add Slice Simulator URL.
211
+ project.config.localSliceSimulatorURL ||=
212
+ "http://localhost:3000/slice-simulator";
213
+
214
+ // Nest the default Slice Library in the src directory if it exists and
215
+ // is empty.
216
+ if (
217
+ (await checkHasProjectFile({
218
+ filename: "./src/lib",
219
+ helpers,
220
+ })) &&
221
+ project.config.libraries &&
222
+ JSON.stringify(project.config.libraries) === JSON.stringify(["./slices"])
223
+ ) {
224
+ const sliceLibrary = await actions.readSliceLibrary({
225
+ libraryID: project.config.libraries[0],
226
+ });
227
+
228
+ if (sliceLibrary.sliceIDs.length < 1) {
229
+ project.config.libraries = ["./src/lib/slices"];
230
+ }
231
+ }
232
+
233
+ await helpers.updateSliceMachineConfig(project.config, {
234
+ format: options.format,
235
+ });
236
+ };
237
+
238
+ const upsertSliceLibraryIndexFiles = async (
239
+ context: SliceMachineContext<PluginOptions>,
240
+ ) => {
241
+ if (!context.project.config.libraries) {
242
+ return;
243
+ }
244
+
245
+ await Promise.all(
246
+ context.project.config.libraries?.map(async (libraryID) => {
247
+ await upsertSliceLibraryIndexFile({ libraryID, ...context });
248
+ }),
249
+ );
250
+ };
251
+
252
+ export const projectInit: ProjectInitHook<PluginOptions> = async (
253
+ { installDependencies: _installDependencies },
254
+ context,
255
+ ) => {
256
+ rejectIfNecessary(
257
+ await Promise.allSettled([
258
+ installDependencies({ installDependencies: _installDependencies }),
259
+ modifySliceMachineConfig(context),
260
+ createPrismicIOFile(context),
261
+ createSliceSimulatorPage(context),
262
+ upsertSliceLibraryIndexFiles(context),
263
+ ]),
264
+ );
265
+ };
@@ -0,0 +1,120 @@
1
+ import type {
2
+ SliceCreateHook,
3
+ SliceCreateHookData,
4
+ SliceMachineContext,
5
+ } from "@slicemachine/plugin-kit";
6
+ import {
7
+ upsertGlobalTypeScriptTypes,
8
+ writeSliceFile,
9
+ writeSliceModel,
10
+ } from "@slicemachine/plugin-kit/fs";
11
+ import { source } from "common-tags";
12
+
13
+ import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
14
+ import { pascalCase } from "../lib/pascalCase";
15
+ import { rejectIfNecessary } from "../lib/rejectIfNecessary";
16
+ import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
17
+
18
+ import type { PluginOptions } from "../types";
19
+
20
+ type Args = {
21
+ data: SliceCreateHookData;
22
+ } & SliceMachineContext<PluginOptions>;
23
+
24
+ const createComponentFile = async ({
25
+ data,
26
+ helpers,
27
+ actions,
28
+ options,
29
+ }: Args) => {
30
+ const pascalName = pascalCase(data.model.name);
31
+
32
+ let contents: string;
33
+
34
+ const isTypeScriptProject = await checkIsTypeScriptProject({
35
+ helpers,
36
+ options,
37
+ });
38
+
39
+ if (isTypeScriptProject) {
40
+ contents = source`
41
+ <script lang="ts">
42
+ import type { Content, SliceZone } from '@prismicio/client';
43
+
44
+ export let slice: Content.${pascalName}Slice;
45
+ export let slices: SliceZone;
46
+ export let index: number;
47
+ export let context: unknown;
48
+ </script>
49
+
50
+ <section data-slice-type={slice.slice_type} data-slice-variation={slice.variation}>
51
+ Placeholder component for {slice.slice_type} (variation: {slice.variation}) Slices
52
+ </section>
53
+ `;
54
+ } else {
55
+ contents = source`
56
+ <script>
57
+ /** @type {import("@prismicio/client").Content.${pascalName}Slice} */
58
+ export let slice;
59
+ /** @type {import("@prismicio/client").SliceZone} */
60
+ export let slices;
61
+ /** @type {number} */
62
+ export let index;
63
+ /** @type {unknown} */
64
+ export let context;
65
+ </script>
66
+
67
+ <section data-slice-type={slice.slice_type} data-slice-variation={slice.variation}>
68
+ Placeholder component for {slice.slice_type} (variation: {slice.variation}) Slices
69
+ </section>
70
+ `;
71
+ }
72
+
73
+ await writeSliceFile({
74
+ libraryID: data.libraryID,
75
+ model: data.model,
76
+ filename: "index.svelte",
77
+ contents,
78
+ format: options.format,
79
+ actions,
80
+ helpers,
81
+ formatOptions: {
82
+ prettier: {
83
+ plugins: ["prettier-plugin-svelte"],
84
+ parser: "svelte",
85
+ },
86
+ },
87
+ });
88
+ };
89
+
90
+ export const sliceCreate: SliceCreateHook<PluginOptions> = async (
91
+ data,
92
+ context,
93
+ ) => {
94
+ rejectIfNecessary(
95
+ await Promise.allSettled([
96
+ writeSliceModel({
97
+ libraryID: data.libraryID,
98
+ model: data.model,
99
+ format: context.options.format,
100
+ helpers: context.helpers,
101
+ }),
102
+ createComponentFile({ data, ...context }),
103
+ ]),
104
+ );
105
+
106
+ rejectIfNecessary(
107
+ await Promise.allSettled([
108
+ upsertSliceLibraryIndexFile({
109
+ libraryID: data.libraryID,
110
+ ...context,
111
+ }),
112
+ upsertGlobalTypeScriptTypes({
113
+ filename: context.options.generatedTypesFilePath,
114
+ format: context.options.format,
115
+ helpers: context.helpers,
116
+ actions: context.actions,
117
+ }),
118
+ ]),
119
+ );
120
+ };
@@ -0,0 +1,203 @@
1
+ import type {
2
+ SliceMachineContext,
3
+ SliceSimulatorSetupReadHook,
4
+ SliceSimulatorSetupStep,
5
+ } from "@slicemachine/plugin-kit";
6
+ import { source } from "common-tags";
7
+ import { createRequire } from "node:module";
8
+ import fetch, { Response } from "node-fetch";
9
+
10
+ import type { PluginOptions } from "../types";
11
+
12
+ const REQUIRED_DEPENDENCIES = ["@prismicio/svelte"];
13
+
14
+ type Args = SliceMachineContext<PluginOptions>;
15
+
16
+ const createStep1 = async ({
17
+ project,
18
+ helpers,
19
+ }: Args): Promise<SliceSimulatorSetupStep> => {
20
+ const require = createRequire(project.root);
21
+
22
+ return {
23
+ title: "Install packages",
24
+ description: "The simulator requires some dependencies.",
25
+ // TODO: Create a plugin runner helper to provide the correct
26
+ // package manager
27
+ body: source`
28
+ The simulator requires extra dependencies. Run the following command to install them.
29
+
30
+ ~~~sh
31
+ npm install --save ${REQUIRED_DEPENDENCIES.join(" ")}
32
+ ~~~
33
+ `,
34
+ validate: async () => {
35
+ const missingDependencies: string[] = [];
36
+
37
+ for (const dependency of REQUIRED_DEPENDENCIES) {
38
+ try {
39
+ // `require.resolve()` is preferred
40
+ // over `import()` because we don't
41
+ // want to load the module. Loading a
42
+ // module could introduce side-effects.
43
+ require.resolve(dependency, {
44
+ paths: [helpers.joinPathFromRoot("node_modules")],
45
+ });
46
+ } catch {
47
+ missingDependencies.push(dependency);
48
+ }
49
+ }
50
+
51
+ if (missingDependencies.length >= REQUIRED_DEPENDENCIES.length) {
52
+ return {
53
+ title: "Missing all dependencies",
54
+ message: source`
55
+ Install the required dependencies to continue.
56
+ `,
57
+ };
58
+ }
59
+
60
+ if (missingDependencies.length > 0) {
61
+ const formattedMissingDependencies = missingDependencies
62
+ .map((missingDependency) => `\`${missingDependency}\``)
63
+ .join(", ");
64
+
65
+ return {
66
+ title: "Missing some dependencies",
67
+ message: source`
68
+ The following dependencies are missing: ${formattedMissingDependencies}
69
+ `,
70
+ };
71
+ }
72
+ },
73
+ };
74
+ };
75
+
76
+ const createStep2 = async ({
77
+ helpers,
78
+ }: Args): Promise<SliceSimulatorSetupStep> => {
79
+ const filePath = helpers.joinPathFromRoot(
80
+ "src",
81
+ "routes",
82
+ "slice-simulator",
83
+ "+page.svelte",
84
+ );
85
+
86
+ const fileContents = await helpers.format(
87
+ source`
88
+ <script>
89
+ import { SliceSimulator } from '@slicemachine/adapter-sveltekit/simulator';
90
+ import { SliceZone } from '@prismicio/svelte';
91
+ import { components } from '$lib/slices';
92
+ </script>
93
+
94
+ <SliceSimulator let:slices>
95
+ <SliceZone {slices} {components} />
96
+ </SliceSimulator>
97
+ `,
98
+ filePath,
99
+ {
100
+ includeNewlineAtEnd: false,
101
+ prettier: {
102
+ plugins: ["prettier-plugin-svelte"],
103
+ parser: "svelte",
104
+ },
105
+ },
106
+ );
107
+
108
+ return {
109
+ title: "Create a page for the simulator",
110
+ description: `Create a file at \`src/routes/slice-simulator/+page.svelte\` containing this code.`,
111
+ body: source`
112
+ Create a file at \`src/routes/slice-simulator/+page.svelte\` containing the following code. This route will be used to simulate and develop your components.
113
+
114
+ ~~~svelte
115
+ ${fileContents}
116
+ ~~~
117
+ `,
118
+ };
119
+ };
120
+
121
+ const createStep3 = async ({
122
+ helpers,
123
+ }: Args): Promise<SliceSimulatorSetupStep> => {
124
+ const filePath = helpers.joinPathFromRoot("slicemachine.config.json");
125
+ const fileContents = await helpers.format(
126
+ source`
127
+ {
128
+ "localSliceSimulatorURL": "http://localhost:5173/slice-simulator"
129
+ }
130
+ `,
131
+ filePath,
132
+ {
133
+ includeNewlineAtEnd: false,
134
+ },
135
+ );
136
+
137
+ return {
138
+ title: "Update `slicemachine.config.json`",
139
+ description: `Update your \`slicemachine.config.json\` file with a \`localSliceSimulatorURL\` property pointing to your \`/slice-simulator\` page.`,
140
+ body: source`
141
+ Update your \`slicemachine.config.json\` file with a \`localSliceSimulatorURL\` property pointing to your \`/slice-simulator\` page.
142
+
143
+ ~~~json
144
+ ${fileContents}
145
+ ~~~
146
+ `,
147
+ validate: async () => {
148
+ const project = await helpers.getProject();
149
+
150
+ if (!("localSliceSimulatorURL" in project.config)) {
151
+ return {
152
+ title: "Missing `localSliceSimulatorURL` property",
153
+ message: source`
154
+ A \`localSliceSimulatorURL\` property was not found in your \`slicemachine.config.json\` file.
155
+ `,
156
+ };
157
+ }
158
+
159
+ // Test if the URL is valid.
160
+ try {
161
+ if (project.config.localSliceSimulatorURL) {
162
+ new URL(project.config.localSliceSimulatorURL);
163
+ } else {
164
+ throw new Error("Undefined Slice Simulator URL");
165
+ }
166
+ } catch {
167
+ return {
168
+ title: "An invalid URL was provided",
169
+ message: source`
170
+ The \`localSliceSimulatorURL\` property should be of the shape \`http://localhost:PORT/PATH\`. See the codeblock for an example.
171
+ `,
172
+ };
173
+ }
174
+
175
+ // Check if the URL is accessible.
176
+ let res: Response | undefined = undefined;
177
+ try {
178
+ res = await fetch(project.config.localSliceSimulatorURL);
179
+ } catch (error) {
180
+ // Noop, we return if `res` is not defined
181
+ }
182
+
183
+ if (!res || !res.ok) {
184
+ return {
185
+ title: "Unable to connect to simulator page",
186
+ message: source`
187
+ Check that the \`localSliceSimulatorURL\` property in \`slicemachine.config.json\` is correct and try again. See the [troubleshooting page](https://prismic.io/docs/setup-nextjs) for more details.
188
+ `,
189
+ };
190
+ }
191
+ },
192
+ };
193
+ };
194
+
195
+ export const sliceSimulatorSetupRead: SliceSimulatorSetupReadHook<
196
+ PluginOptions
197
+ > = async (_data, context) => {
198
+ return await Promise.all([
199
+ createStep1(context),
200
+ createStep2(context),
201
+ createStep3(context),
202
+ ]);
203
+ };