@slicemachine/adapter-next 0.0.3 → 0.0.4

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 (210) hide show
  1. package/dist/SliceSimulator.cjs +14 -41
  2. package/dist/SliceSimulator.cjs.map +1 -1
  3. package/dist/SliceSimulator.js +11 -36
  4. package/dist/SliceSimulator.js.map +1 -1
  5. package/dist/_virtual/_commonjsHelpers.cjs +5 -0
  6. package/dist/_virtual/_commonjsHelpers.cjs.map +1 -0
  7. package/dist/_virtual/_commonjsHelpers.js +5 -0
  8. package/dist/_virtual/_commonjsHelpers.js.map +1 -0
  9. package/dist/_virtual/ponyfill.es2018.cjs +5 -0
  10. package/dist/_virtual/ponyfill.es2018.cjs.map +1 -0
  11. package/dist/_virtual/ponyfill.es2018.js +5 -0
  12. package/dist/_virtual/ponyfill.es2018.js.map +1 -0
  13. package/dist/constants.cjs +5 -0
  14. package/dist/constants.cjs.map +1 -0
  15. package/dist/constants.d.ts +4 -0
  16. package/dist/constants.js +5 -0
  17. package/dist/constants.js.map +1 -0
  18. package/dist/hooks/customType-asset-delete.cjs +3 -5
  19. package/dist/hooks/customType-asset-delete.cjs.map +1 -1
  20. package/dist/hooks/customType-asset-delete.js.map +1 -1
  21. package/dist/hooks/customType-asset-read.cjs +3 -5
  22. package/dist/hooks/customType-asset-read.cjs.map +1 -1
  23. package/dist/hooks/customType-asset-read.js.map +1 -1
  24. package/dist/hooks/customType-asset-update.cjs +4 -6
  25. package/dist/hooks/customType-asset-update.cjs.map +1 -1
  26. package/dist/hooks/customType-asset-update.js.map +1 -1
  27. package/dist/hooks/customType-create.cjs +4 -6
  28. package/dist/hooks/customType-create.cjs.map +1 -1
  29. package/dist/hooks/customType-create.js.map +1 -1
  30. package/dist/hooks/customType-delete.cjs +3 -5
  31. package/dist/hooks/customType-delete.cjs.map +1 -1
  32. package/dist/hooks/customType-delete.js.map +1 -1
  33. package/dist/hooks/customType-read.cjs +1 -1
  34. package/dist/hooks/customType-read.cjs.map +1 -1
  35. package/dist/hooks/customType-read.js.map +1 -1
  36. package/dist/hooks/customType-rename.cjs +1 -1
  37. package/dist/hooks/customType-rename.cjs.map +1 -1
  38. package/dist/hooks/customType-rename.js.map +1 -1
  39. package/dist/hooks/customType-update.cjs +1 -1
  40. package/dist/hooks/customType-update.cjs.map +1 -1
  41. package/dist/hooks/customType-update.js.map +1 -1
  42. package/dist/hooks/customTypeLibrary-read.cjs +4 -6
  43. package/dist/hooks/customTypeLibrary-read.cjs.map +1 -1
  44. package/dist/hooks/customTypeLibrary-read.js.map +1 -1
  45. package/dist/hooks/slice-asset-delete.cjs +3 -5
  46. package/dist/hooks/slice-asset-delete.cjs.map +1 -1
  47. package/dist/hooks/slice-asset-delete.js.map +1 -1
  48. package/dist/hooks/slice-asset-read.cjs +3 -5
  49. package/dist/hooks/slice-asset-read.cjs.map +1 -1
  50. package/dist/hooks/slice-asset-read.js.map +1 -1
  51. package/dist/hooks/slice-asset-update.cjs +4 -6
  52. package/dist/hooks/slice-asset-update.cjs.map +1 -1
  53. package/dist/hooks/slice-asset-update.js.map +1 -1
  54. package/dist/hooks/slice-create.cjs +7 -9
  55. package/dist/hooks/slice-create.cjs.map +1 -1
  56. package/dist/hooks/slice-create.js +3 -3
  57. package/dist/hooks/slice-create.js.map +1 -1
  58. package/dist/hooks/slice-delete.cjs +3 -5
  59. package/dist/hooks/slice-delete.cjs.map +1 -1
  60. package/dist/hooks/slice-delete.js.map +1 -1
  61. package/dist/hooks/slice-read.cjs +4 -6
  62. package/dist/hooks/slice-read.cjs.map +1 -1
  63. package/dist/hooks/slice-read.js.map +1 -1
  64. package/dist/hooks/slice-rename.cjs +3 -5
  65. package/dist/hooks/slice-rename.cjs.map +1 -1
  66. package/dist/hooks/slice-rename.js.map +1 -1
  67. package/dist/hooks/slice-update.cjs +1 -1
  68. package/dist/hooks/slice-update.cjs.map +1 -1
  69. package/dist/hooks/slice-update.js.map +1 -1
  70. package/dist/hooks/sliceLibrary-read.cjs +4 -6
  71. package/dist/hooks/sliceLibrary-read.cjs.map +1 -1
  72. package/dist/hooks/sliceLibrary-read.js.map +1 -1
  73. package/dist/hooks/sliceSimulator-setup-read.cjs +31 -94
  74. package/dist/hooks/sliceSimulator-setup-read.cjs.map +1 -1
  75. package/dist/hooks/sliceSimulator-setup-read.js +31 -75
  76. package/dist/hooks/sliceSimulator-setup-read.js.map +1 -1
  77. package/dist/hooks/snippet-read.cjs +33 -18
  78. package/dist/hooks/snippet-read.cjs.map +1 -1
  79. package/dist/hooks/snippet-read.js +30 -13
  80. package/dist/hooks/snippet-read.js.map +1 -1
  81. package/dist/index.cjs +0 -2
  82. package/dist/index.cjs.map +1 -1
  83. package/dist/index.d.ts +0 -2
  84. package/dist/index.js +0 -2
  85. package/dist/index.js.map +1 -1
  86. package/dist/lib/getJSOrTSXFileExtension.cjs +1 -1
  87. package/dist/lib/getJSOrTSXFileExtension.cjs.map +1 -1
  88. package/dist/lib/getJSOrTSXFileExtension.js.map +1 -1
  89. package/dist/lib/isSharedSliceModel.cjs +3 -5
  90. package/dist/lib/isSharedSliceModel.cjs.map +1 -1
  91. package/dist/lib/isSharedSliceModel.js.map +1 -1
  92. package/dist/lib/pascalCase.cjs +1 -1
  93. package/dist/lib/pascalCase.cjs.map +1 -1
  94. package/dist/lib/pascalCase.js.map +1 -1
  95. package/dist/lib/readJSONFile.cjs +3 -5
  96. package/dist/lib/readJSONFile.cjs.map +1 -1
  97. package/dist/lib/readJSONFile.js.map +1 -1
  98. package/dist/lib/rejectIfNecessary.cjs +1 -1
  99. package/dist/lib/rejectIfNecessary.cjs.map +1 -1
  100. package/dist/lib/rejectIfNecessary.js.map +1 -1
  101. package/dist/lib/updateSliceModelFile.cjs +3 -5
  102. package/dist/lib/updateSliceModelFile.cjs.map +1 -1
  103. package/dist/lib/updateSliceModelFile.js.map +1 -1
  104. package/dist/lib/upsertGlobalContentTypes.cjs +8 -6
  105. package/dist/lib/upsertGlobalContentTypes.cjs.map +1 -1
  106. package/dist/lib/upsertGlobalContentTypes.js +4 -0
  107. package/dist/lib/upsertGlobalContentTypes.js.map +1 -1
  108. package/dist/lib/upsertSliceLibraryIndexFile.cjs +9 -8
  109. package/dist/lib/upsertSliceLibraryIndexFile.cjs.map +1 -1
  110. package/dist/lib/upsertSliceLibraryIndexFile.js +6 -3
  111. package/dist/lib/upsertSliceLibraryIndexFile.js.map +1 -1
  112. package/dist/node_modules/data-uri-to-buffer/dist/index.cjs +41 -0
  113. package/dist/node_modules/data-uri-to-buffer/dist/index.cjs.map +1 -0
  114. package/dist/node_modules/data-uri-to-buffer/dist/index.js +41 -0
  115. package/dist/node_modules/data-uri-to-buffer/dist/index.js.map +1 -0
  116. package/dist/node_modules/fetch-blob/file.cjs +55 -0
  117. package/dist/node_modules/fetch-blob/file.cjs.map +1 -0
  118. package/dist/node_modules/fetch-blob/file.js +55 -0
  119. package/dist/node_modules/fetch-blob/file.js.map +1 -0
  120. package/dist/node_modules/fetch-blob/index.cjs +176 -0
  121. package/dist/node_modules/fetch-blob/index.cjs.map +1 -0
  122. package/dist/node_modules/fetch-blob/index.js +176 -0
  123. package/dist/node_modules/fetch-blob/index.js.map +1 -0
  124. package/dist/node_modules/fetch-blob/streams.cjs +43 -0
  125. package/dist/node_modules/fetch-blob/streams.cjs.map +1 -0
  126. package/dist/node_modules/fetch-blob/streams.js +42 -0
  127. package/dist/node_modules/fetch-blob/streams.js.map +1 -0
  128. package/dist/node_modules/formdata-polyfill/esm.min.cjs +115 -0
  129. package/dist/node_modules/formdata-polyfill/esm.min.cjs.map +1 -0
  130. package/dist/node_modules/formdata-polyfill/esm.min.js +115 -0
  131. package/dist/node_modules/formdata-polyfill/esm.min.js.map +1 -0
  132. package/dist/node_modules/node-domexception/index.cjs +10 -0
  133. package/dist/node_modules/node-domexception/index.cjs.map +1 -0
  134. package/dist/node_modules/node-domexception/index.js +9 -0
  135. package/dist/node_modules/node-domexception/index.js.map +1 -0
  136. package/dist/node_modules/node-fetch/src/body.cjs +237 -0
  137. package/dist/node_modules/node-fetch/src/body.cjs.map +1 -0
  138. package/dist/node_modules/node-fetch/src/body.js +237 -0
  139. package/dist/node_modules/node-fetch/src/body.js.map +1 -0
  140. package/dist/node_modules/node-fetch/src/errors/abort-error.cjs +10 -0
  141. package/dist/node_modules/node-fetch/src/errors/abort-error.cjs.map +1 -0
  142. package/dist/node_modules/node-fetch/src/errors/abort-error.js +10 -0
  143. package/dist/node_modules/node-fetch/src/errors/abort-error.js.map +1 -0
  144. package/dist/node_modules/node-fetch/src/errors/base.cjs +17 -0
  145. package/dist/node_modules/node-fetch/src/errors/base.cjs.map +1 -0
  146. package/dist/node_modules/node-fetch/src/errors/base.js +17 -0
  147. package/dist/node_modules/node-fetch/src/errors/base.js.map +1 -0
  148. package/dist/node_modules/node-fetch/src/errors/fetch-error.cjs +14 -0
  149. package/dist/node_modules/node-fetch/src/errors/fetch-error.cjs.map +1 -0
  150. package/dist/node_modules/node-fetch/src/errors/fetch-error.js +14 -0
  151. package/dist/node_modules/node-fetch/src/errors/fetch-error.js.map +1 -0
  152. package/dist/node_modules/node-fetch/src/headers.cjs +173 -0
  153. package/dist/node_modules/node-fetch/src/headers.cjs.map +1 -0
  154. package/dist/node_modules/node-fetch/src/headers.js +173 -0
  155. package/dist/node_modules/node-fetch/src/headers.js.map +1 -0
  156. package/dist/node_modules/node-fetch/src/index.cjs +292 -0
  157. package/dist/node_modules/node-fetch/src/index.cjs.map +1 -0
  158. package/dist/node_modules/node-fetch/src/index.js +298 -0
  159. package/dist/node_modules/node-fetch/src/index.js.map +1 -0
  160. package/dist/node_modules/node-fetch/src/request.cjs +193 -0
  161. package/dist/node_modules/node-fetch/src/request.cjs.map +1 -0
  162. package/dist/node_modules/node-fetch/src/request.js +193 -0
  163. package/dist/node_modules/node-fetch/src/request.js.map +1 -0
  164. package/dist/node_modules/node-fetch/src/response.cjs +110 -0
  165. package/dist/node_modules/node-fetch/src/response.cjs.map +1 -0
  166. package/dist/node_modules/node-fetch/src/response.js +110 -0
  167. package/dist/node_modules/node-fetch/src/response.js.map +1 -0
  168. package/dist/node_modules/node-fetch/src/utils/get-search.cjs +12 -0
  169. package/dist/node_modules/node-fetch/src/utils/get-search.cjs.map +1 -0
  170. package/dist/node_modules/node-fetch/src/utils/get-search.js +12 -0
  171. package/dist/node_modules/node-fetch/src/utils/get-search.js.map +1 -0
  172. package/dist/node_modules/node-fetch/src/utils/is-redirect.cjs +8 -0
  173. package/dist/node_modules/node-fetch/src/utils/is-redirect.cjs.map +1 -0
  174. package/dist/node_modules/node-fetch/src/utils/is-redirect.js +8 -0
  175. package/dist/node_modules/node-fetch/src/utils/is-redirect.js.map +1 -0
  176. package/dist/node_modules/node-fetch/src/utils/is.cjs +28 -0
  177. package/dist/node_modules/node-fetch/src/utils/is.cjs.map +1 -0
  178. package/dist/node_modules/node-fetch/src/utils/is.js +28 -0
  179. package/dist/node_modules/node-fetch/src/utils/is.js.map +1 -0
  180. package/dist/node_modules/node-fetch/src/utils/multipart-parser.cjs +350 -0
  181. package/dist/node_modules/node-fetch/src/utils/multipart-parser.cjs.map +1 -0
  182. package/dist/node_modules/node-fetch/src/utils/multipart-parser.js +350 -0
  183. package/dist/node_modules/node-fetch/src/utils/multipart-parser.js.map +1 -0
  184. package/dist/node_modules/node-fetch/src/utils/referrer.cjs +149 -0
  185. package/dist/node_modules/node-fetch/src/utils/referrer.cjs.map +1 -0
  186. package/dist/node_modules/node-fetch/src/utils/referrer.js +149 -0
  187. package/dist/node_modules/node-fetch/src/utils/referrer.js.map +1 -0
  188. package/dist/node_modules/web-streams-polyfill/dist/ponyfill.es2018.cjs +3599 -0
  189. package/dist/node_modules/web-streams-polyfill/dist/ponyfill.es2018.cjs.map +1 -0
  190. package/dist/node_modules/web-streams-polyfill/dist/ponyfill.es2018.js +3599 -0
  191. package/dist/node_modules/web-streams-polyfill/dist/ponyfill.es2018.js.map +1 -0
  192. package/dist/package.json.cjs +5 -0
  193. package/dist/package.json.cjs.map +1 -0
  194. package/dist/{package.js → package.json.js} +1 -1
  195. package/dist/package.json.js.map +1 -0
  196. package/dist/plugin.cjs +2 -2
  197. package/dist/plugin.cjs.map +1 -1
  198. package/dist/plugin.js +1 -1
  199. package/dist/plugin.js.map +1 -1
  200. package/package.json +28 -13
  201. package/src/constants.ts +5 -0
  202. package/src/hooks/slice-create.ts +3 -3
  203. package/src/hooks/sliceSimulator-setup-read.ts +37 -76
  204. package/src/hooks/snippet-read.ts +42 -22
  205. package/src/index.ts +0 -6
  206. package/src/lib/upsertGlobalContentTypes.ts +3 -0
  207. package/src/lib/upsertSliceLibraryIndexFile.ts +11 -6
  208. package/dist/package.cjs +0 -5
  209. package/dist/package.cjs.map +0 -1
  210. package/dist/package.js.map +0 -1
@@ -1,51 +1,33 @@
1
1
  "use strict";
2
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const pluginKit = require("@slicemachine/plugin-kit");
4
4
  const commonTags = require("common-tags");
5
5
  const node_module = require("node:module");
6
- const http = require("node:http");
6
+ const index = require("../node_modules/node-fetch/src/index.cjs");
7
7
  const getJSOrTSXFileExtension = require("../lib/getJSOrTSXFileExtension.cjs");
8
- function _interopNamespace(e) {
9
- if (e && e.__esModule)
10
- return e;
11
- const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
12
- if (e) {
13
- for (const k in e) {
14
- if (k !== "default") {
15
- const d = Object.getOwnPropertyDescriptor(e, k);
16
- Object.defineProperty(n, k, d.get ? d : {
17
- enumerable: true,
18
- get: () => e[k]
19
- });
20
- }
21
- }
22
- }
23
- n.default = e;
24
- return Object.freeze(n);
25
- }
26
- const http__namespace = /* @__PURE__ */ _interopNamespace(http);
27
8
  const REQUIRED_DEPENDENCIES = [
28
9
  "@prismicio/react",
29
- "@prismicio/slice-simulator-react",
30
- "@prismicio/client@latest",
10
+ "@prismicio/client",
31
11
  "@prismicio/helpers"
32
12
  ];
33
- const createStep1 = async ({ project }) => {
13
+ const createStep1 = async ({ project, helpers }) => {
34
14
  const require2 = node_module.createRequire(project.root);
35
15
  return {
36
16
  title: "Install packages",
37
- body: commonTags.stripIndent`
17
+ body: commonTags.source`
38
18
  The simulator requires extra dependencies. Run the following command to install them.
39
19
 
40
20
  ~~~sh
41
- npm install --save @prismicio/react @prismicio/slice-simulator-react @prismicio/client@latest @prismicio/helpers
21
+ npm install --save ${REQUIRED_DEPENDENCIES.join(" ")}
42
22
  ~~~
43
23
  `,
44
24
  validate: async () => {
45
25
  const missingDependencies = [];
46
26
  for (const dependency of REQUIRED_DEPENDENCIES) {
47
27
  try {
48
- require2.resolve(dependency);
28
+ require2.resolve(dependency, {
29
+ paths: [helpers.joinPathFromRoot("node_modules")]
30
+ });
49
31
  } catch {
50
32
  missingDependencies.push(dependency);
51
33
  }
@@ -54,7 +36,7 @@ const createStep1 = async ({ project }) => {
54
36
  return {
55
37
  type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
56
38
  title: "Missing all dependencies",
57
- message: commonTags.stripIndent`
39
+ message: commonTags.source`
58
40
  Install the required dependencies to continue.
59
41
  `
60
42
  };
@@ -62,9 +44,9 @@ const createStep1 = async ({ project }) => {
62
44
  if (missingDependencies.length > 0) {
63
45
  const formattedMissingDependencies = missingDependencies.map((missingDependency) => `\`${missingDependency}\``).join(", ");
64
46
  return {
65
- type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
47
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
66
48
  title: "Missing some dependencies",
67
- message: commonTags.stripIndent`
49
+ message: commonTags.source`
68
50
  The following dependencies are missing: ${formattedMissingDependencies}
69
51
  `
70
52
  };
@@ -73,45 +55,16 @@ const createStep1 = async ({ project }) => {
73
55
  };
74
56
  };
75
57
  const createStep2 = async ({ helpers, options }) => {
76
- const fileName = `slice-simulator.${getJSOrTSXFileExtension.getJSOrTSXFileExtension}`;
58
+ const fileName = `slice-simulator.${getJSOrTSXFileExtension.getJSOrTSXFileExtension(options)}`;
77
59
  const filePath = helpers.joinPathFromRoot("pages", fileName);
78
- let fileContents;
79
- if (options.typescript) {
80
- fileContents = commonTags.stripIndent`
81
- import { GetStaticProps } from "next/types";
82
- import { SliceSimulator } from "@prismicio/slice-simulator-react";
83
- import { SliceZone } from "@prismicio/react";
84
-
85
- import state from "../.slicemachine/libraries-state.json";
86
- import { components } from "../slices";
87
-
88
- const SliceSimulatorPage = () => {
89
- return (
90
- <SliceSimulator
91
- sliceZone={(props) => <SliceZone {...props} components={components} />}
92
- state={state}
93
- />
94
- );
95
- };
96
-
97
- export default SliceSimulatorPage;
98
-
99
- export const getStaticProps: GetStaticProps = () => {
100
- return {
101
- // Exclude this page from production builds.
102
- notFound: process.env.NODE_ENV === "production",
103
- };
104
- };
105
- `;
106
- } else {
107
- fileContents = commonTags.stripIndent`
60
+ const fileContents = await helpers.format(commonTags.source`
108
61
  import { SliceSimulator } from "@prismicio/slice-simulator-react";
109
62
  import { SliceZone } from "@prismicio/react";
110
63
 
111
64
  import state from "../.slicemachine/libraries-state.json";
112
65
  import { components } from "../slices";
113
66
 
114
- const SliceSimulatorPage = () => {
67
+ export default function SliceSimulatorPage() {
115
68
  return (
116
69
  <SliceSimulator
117
70
  sliceZone={(props) => <SliceZone {...props} components={components} />}
@@ -119,21 +72,12 @@ const createStep2 = async ({ helpers, options }) => {
119
72
  />
120
73
  );
121
74
  };
122
-
123
- export default SliceSimulatorPage;
124
-
125
- export const getStaticProps= () => {
126
- return {
127
- // Exclude this page from production builds.
128
- notFound: process.env.NODE_ENV === "production",
129
- };
130
- };
131
- `;
132
- }
133
- fileContents = await helpers.format(fileContents, filePath);
75
+ `, filePath, {
76
+ includeNewlineAtEnd: false
77
+ });
134
78
  return {
135
79
  title: "Create a page for the simulator",
136
- body: commonTags.stripIndent`
80
+ body: commonTags.source`
137
81
  In your \`pages\` directory, create a file called \`${fileName}\` and add the following code. This route will be used to simulate and develop your components.
138
82
 
139
83
  ~~~tsx
@@ -144,14 +88,16 @@ const createStep2 = async ({ helpers, options }) => {
144
88
  };
145
89
  const createStep3 = async ({ helpers }) => {
146
90
  const filePath = helpers.joinPathFromRoot("sm.json");
147
- const fileContents = await helpers.format(`
91
+ const fileContents = await helpers.format(commonTags.source`
148
92
  {
149
93
  "localSliceSimulatorURL": "http://localhost:3000/slice-simulator"
150
94
  }
151
- `, filePath);
95
+ `, filePath, {
96
+ includeNewlineAtEnd: false
97
+ });
152
98
  return {
153
99
  title: "Update `sm.json`",
154
- body: commonTags.stripIndent`
100
+ body: commonTags.source`
155
101
  Update your \`sm.json\` file with a \`localSliceSimulatorURL\` property pointing to your \`slice-simulator\` page.
156
102
 
157
103
  ~~~json
@@ -164,7 +110,7 @@ const createStep3 = async ({ helpers }) => {
164
110
  return {
165
111
  type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
166
112
  title: "Missing `localSliceSimulatorURL` property",
167
- message: commonTags.stripIndent`
113
+ message: commonTags.source`
168
114
  A \`localSliceSimulatorURL\` property was not found in your \`sm.json\` file.
169
115
  `
170
116
  };
@@ -177,28 +123,19 @@ const createStep3 = async ({ helpers }) => {
177
123
  }
178
124
  } catch {
179
125
  return {
180
- type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
126
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
181
127
  title: "An invalid URL was provided",
182
- message: commonTags.stripIndent`
128
+ message: commonTags.source`
183
129
  The \`localSliceSimulatorURL\` property should be of the shape \`http://localhost:PORT/PATH\`. See the codeblock for an example.
184
130
  `
185
131
  };
186
132
  }
187
- const ok = await new Promise((resolve) => {
188
- if (project.config.localSliceSimulatorURL) {
189
- http__namespace.get(project.config.localSliceSimulatorURL, (res) => {
190
- if (res.statusCode) {
191
- resolve(res.statusCode >= 200 && res.statusCode < 300);
192
- }
193
- });
194
- }
195
- resolve(false);
196
- });
197
- if (!ok) {
133
+ const res = await index.default(project.config.localSliceSimulatorURL);
134
+ if (!res.ok) {
198
135
  return {
199
- type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
136
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
200
137
  title: "Unable to connect to simulator page",
201
- message: commonTags.stripIndent`
138
+ message: commonTags.source`
202
139
  Check that the \`localSliceSimulatorURL\` property in \`sm.json\` is correct and try again. See the [troubleshooting page](https://prismic.io/docs/technologies/setup-slice-simulator-nextjs) for more details.
203
140
  `
204
141
  };
@@ -1 +1 @@
1
- {"version":3,"file":"sliceSimulator-setup-read.cjs","sources":["../../src/hooks/sliceSimulator-setup-read.ts"],"sourcesContent":["import type {\n\tSliceMachineContext,\n\tSliceSimulatorSetupReadHook,\n\tSliceSimulatorSetupStep,\n} from \"@slicemachine/plugin-kit\";\nimport { SliceSimulatorSetupStepValidationMessageType } from \"@slicemachine/plugin-kit\";\nimport { stripIndent } from \"common-tags\";\nimport { createRequire } from \"node:module\";\nimport * as http from \"node:http\";\n\nimport { getJSOrTSXFileExtension } from \"../lib/getJSOrTSXFileExtension\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst REQUIRED_DEPENDENCIES = [\n\t\"@prismicio/react\",\n\t\"@prismicio/slice-simulator-react\",\n\t\"@prismicio/client@latest\",\n\t\"@prismicio/helpers\",\n];\n\ntype Args = SliceMachineContext<PluginOptions>;\n\nconst createStep1 = async ({\n\tproject,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst require = createRequire(project.root);\n\n\treturn {\n\t\ttitle: \"Install packages\",\n\t\tbody: stripIndent`\n\t\t\tThe simulator requires extra dependencies. Run the following command to install them.\n\n\t\t\t~~~sh\n\t\t\tnpm install --save @prismicio/react @prismicio/slice-simulator-react @prismicio/client@latest @prismicio/helpers\n\t\t\t~~~\n\t\t`,\n\t\tvalidate: async () => {\n\t\t\tconst missingDependencies: string[] = [];\n\n\t\t\tfor (const dependency of REQUIRED_DEPENDENCIES) {\n\t\t\t\ttry {\n\t\t\t\t\t// `require.resolve()` is preferred\n\t\t\t\t\t// over `import()` because we don't\n\t\t\t\t\t// want to load the module. Loading a\n\t\t\t\t\t// module could introduce side-effects.\n\t\t\t\t\trequire.resolve(dependency);\n\t\t\t\t} catch {\n\t\t\t\t\tmissingDependencies.push(dependency);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (missingDependencies.length >= REQUIRED_DEPENDENCIES.length) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing all dependencies\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tInstall the required dependencies to continue.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (missingDependencies.length > 0) {\n\t\t\t\tconst formattedMissingDependencies = missingDependencies\n\t\t\t\t\t.map((missingDependency) => `\\`${missingDependency}\\``)\n\t\t\t\t\t.join(\", \");\n\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Warning,\n\t\t\t\t\ttitle: \"Missing some dependencies\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tThe following dependencies are missing: ${formattedMissingDependencies}\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n};\n\nconst createStep2 = async ({\n\thelpers,\n\toptions,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst fileName = `slice-simulator.${getJSOrTSXFileExtension}`;\n\tconst filePath = helpers.joinPathFromRoot(\"pages\", fileName);\n\n\tlet fileContents: string;\n\n\tif (options.typescript) {\n\t\tfileContents = stripIndent`\n\t\t\timport { GetStaticProps } from \"next/types\";\n\t\t\timport { SliceSimulator } from \"@prismicio/slice-simulator-react\";\n\t\t\timport { SliceZone } from \"@prismicio/react\";\n\n\t\t\timport state from \"../.slicemachine/libraries-state.json\";\n\t\t\timport { components } from \"../slices\";\n\n\t\t\tconst SliceSimulatorPage = () => {\n\t\t\t\treturn (\n\t\t\t\t\t<SliceSimulator\n\t\t\t\t\t\tsliceZone={(props) => <SliceZone {...props} components={components} />}\n\t\t\t\t\t\tstate={state}\n\t\t\t\t\t/>\n\t\t\t\t);\n\t\t\t};\n\n\t\t\texport default SliceSimulatorPage;\n\n\t\t\texport const getStaticProps: GetStaticProps = () => {\n\t\t\t\treturn {\n\t\t\t\t\t// Exclude this page from production builds.\n\t\t\t\t\tnotFound: process.env.NODE_ENV === \"production\",\n\t\t\t\t};\n\t\t\t};\n\t\t`;\n\t} else {\n\t\tfileContents = stripIndent`\n\t\t\timport { SliceSimulator } from \"@prismicio/slice-simulator-react\";\n\t\t\timport { SliceZone } from \"@prismicio/react\";\n\n\t\t\timport state from \"../.slicemachine/libraries-state.json\";\n\t\t\timport { components } from \"../slices\";\n\n\t\t\tconst SliceSimulatorPage = () => {\n\t\t\t\treturn (\n\t\t\t\t\t<SliceSimulator\n\t\t\t\t\t\tsliceZone={(props) => <SliceZone {...props} components={components} />}\n\t\t\t\t\t\tstate={state}\n\t\t\t\t\t/>\n\t\t\t\t);\n\t\t\t};\n\n\t\t\texport default SliceSimulatorPage;\n\n\t\t\texport const getStaticProps= () => {\n\t\t\t\treturn {\n\t\t\t\t\t// Exclude this page from production builds.\n\t\t\t\t\tnotFound: process.env.NODE_ENV === \"production\",\n\t\t\t\t};\n\t\t\t};\n\t\t`;\n\t}\n\n\tfileContents = await helpers.format(fileContents, filePath);\n\n\treturn {\n\t\ttitle: \"Create a page for the simulator\",\n\t\tbody: stripIndent`\n\t\t\tIn your \\`pages\\` directory, create a file called \\`${fileName}\\` and add the following code. This route will be used to simulate and develop your components.\n\n\t\t\t~~~tsx\n\t\t\t${fileContents}\n\t\t\t~~~\n\t\t`,\n\t};\n};\n\nconst createStep3 = async ({\n\thelpers,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst filePath = helpers.joinPathFromRoot(\"sm.json\");\n\tconst fileContents = await helpers.format(\n\t\t`\n\t\t\t{\n\t\t\t\t\"localSliceSimulatorURL\": \"http://localhost:3000/slice-simulator\"\n\t\t\t}\n\t\t`,\n\t\tfilePath,\n\t);\n\n\treturn {\n\t\ttitle: \"Update `sm.json`\",\n\t\tbody: stripIndent`\n\t\t\tUpdate your \\`sm.json\\` file with a \\`localSliceSimulatorURL\\` property pointing to your \\`slice-simulator\\` page.\n\n\t\t\t~~~json\n\t\t\t${fileContents}\n\t\t\t~~~\n\t\t`,\n\t\tvalidate: async () => {\n\t\t\tconst project = await helpers.getProject();\n\n\t\t\tif (!(\"localSliceSimulatorURL\" in project.config)) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing `localSliceSimulatorURL` property\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tA \\`localSliceSimulatorURL\\` property was not found in your \\`sm.json\\` file.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Test if the URL is valid.\n\t\t\ttry {\n\t\t\t\tif (project.config.localSliceSimulatorURL) {\n\t\t\t\t\tnew URL(project.config.localSliceSimulatorURL);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new Error(\"Undefined Slice Simulator URL\");\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Warning,\n\t\t\t\t\ttitle: \"An invalid URL was provided\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tThe \\`localSliceSimulatorURL\\` property should be of the shape \\`http://localhost:PORT/PATH\\`. See the codeblock for an example.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Check if the URL is accessible.\n\t\t\tconst ok = await new Promise<boolean>((resolve) => {\n\t\t\t\tif (project.config.localSliceSimulatorURL) {\n\t\t\t\t\thttp.get(project.config.localSliceSimulatorURL, (res) => {\n\t\t\t\t\t\tif (res.statusCode) {\n\t\t\t\t\t\t\tresolve(res.statusCode >= 200 && res.statusCode < 300);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tresolve(false);\n\t\t\t});\n\n\t\t\tif (!ok) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Warning,\n\t\t\t\t\ttitle: \"Unable to connect to simulator page\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tCheck that the \\`localSliceSimulatorURL\\` property in \\`sm.json\\` is correct and try again. See the [troubleshooting page](https://prismic.io/docs/technologies/setup-slice-simulator-nextjs) for more details.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n};\n\nexport const sliceSimulatorSetupRead: SliceSimulatorSetupReadHook<\n\tPluginOptions\n> = async (_data, context) => {\n\treturn Promise.all([\n\t\tcreateStep1(context),\n\t\tcreateStep2(context),\n\t\tcreateStep3(context),\n\t]);\n};\n"],"names":["require","createRequire","stripIndent","SliceSimulatorSetupStepValidationMessageType","getJSOrTSXFileExtension","http"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,MAAM,wBAAwB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;;AAKD,MAAM,cAAc,OAAO,EAC1B,cAC4C;AACtC,QAAAA,WAAUC,YAAAA,cAAc,QAAQ,IAAI;AAEnC,SAAA;AAAA,IACN,OAAO;AAAA,IACP,MAAMC,WAAAA;AAAAA;AAAAA;AAAAA;AAAAA;AAAAA;AAAAA;AAAAA,IAON,UAAU,YAAW;AACpB,YAAM,sBAAgC,CAAA;AAEtC,iBAAW,cAAc,uBAAuB;AAC3C,YAAA;AAKH,UAAAF,SAAQ,QAAQ,UAAU;AAAA,QAAA,QACzB;AACD,8BAAoB,KAAK,UAAU;AAAA,QACnC;AAAA,MACD;AAEG,UAAA,oBAAoB,UAAU,sBAAsB,QAAQ;AACxD,eAAA;AAAA,UACN,MAAMG,UAA6C,6CAAA;A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OAAO;AAAA,IACnB,YAAY,OAAO;AAAA,EAAA,CACnB;AACF;;"}
1
+ {"version":3,"file":"sliceSimulator-setup-read.cjs","sources":["../../../src/hooks/sliceSimulator-setup-read.ts"],"sourcesContent":["import type {\n\tSliceMachineContext,\n\tSliceSimulatorSetupReadHook,\n\tSliceSimulatorSetupStep,\n} from \"@slicemachine/plugin-kit\";\nimport { SliceSimulatorSetupStepValidationMessageType } from \"@slicemachine/plugin-kit\";\nimport { source } from \"common-tags\";\nimport { createRequire } from \"node:module\";\nimport fetch from \"node-fetch\";\n\nimport { getJSOrTSXFileExtension } from \"../lib/getJSOrTSXFileExtension\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst REQUIRED_DEPENDENCIES = [\n\t\"@prismicio/react\",\n\t\"@prismicio/client\",\n\t\"@prismicio/helpers\",\n];\n\ntype Args = SliceMachineContext<PluginOptions>;\n\nconst createStep1 = async ({\n\tproject,\n\thelpers,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst require = createRequire(project.root);\n\n\treturn {\n\t\ttitle: \"Install packages\",\n\t\t// TODO: Create a plugin runner helper to provide the correct\n\t\t// package manager\n\t\tbody: source`\n\t\t\tThe simulator requires extra dependencies. Run the following command to install them.\n\n\t\t\t~~~sh\n\t\t\tnpm install --save ${REQUIRED_DEPENDENCIES.join(\" \")}\n\t\t\t~~~\n\t\t`,\n\t\tvalidate: async () => {\n\t\t\tconst missingDependencies: string[] = [];\n\n\t\t\tfor (const dependency of REQUIRED_DEPENDENCIES) {\n\t\t\t\ttry {\n\t\t\t\t\t// `require.resolve()` is preferred\n\t\t\t\t\t// over `import()` because we don't\n\t\t\t\t\t// want to load the module. Loading a\n\t\t\t\t\t// module could introduce side-effects.\n\t\t\t\t\trequire.resolve(dependency, {\n\t\t\t\t\t\tpaths: [helpers.joinPathFromRoot(\"node_modules\")],\n\t\t\t\t\t});\n\t\t\t\t} catch {\n\t\t\t\t\tmissingDependencies.push(dependency);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (missingDependencies.length >= REQUIRED_DEPENDENCIES.length) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing all dependencies\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tInstall the required dependencies to continue.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (missingDependencies.length > 0) {\n\t\t\t\tconst formattedMissingDependencies = missingDependencies\n\t\t\t\t\t.map((missingDependency) => `\\`${missingDependency}\\``)\n\t\t\t\t\t.join(\", \");\n\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing some dependencies\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tThe following dependencies are missing: ${formattedMissingDependencies}\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n};\n\nconst createStep2 = async ({\n\thelpers,\n\toptions,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst fileName = `slice-simulator.${getJSOrTSXFileExtension(options)}`;\n\tconst filePath = helpers.joinPathFromRoot(\"pages\", fileName);\n\n\tconst fileContents = await helpers.format(\n\t\tsource`\n\t\t\timport { SliceSimulator } from \"@prismicio/slice-simulator-react\";\n\t\t\timport { SliceZone } from \"@prismicio/react\";\n\n\t\t\timport state from \"../.slicemachine/libraries-state.json\";\n\t\t\timport { components } from \"../slices\";\n\n\t\t\texport default function SliceSimulatorPage() {\n\t\t\t\treturn (\n\t\t\t\t\t<SliceSimulator\n\t\t\t\t\t\tsliceZone={(props) => <SliceZone {...props} components={components} />}\n\t\t\t\t\t\tstate={state}\n\t\t\t\t\t/>\n\t\t\t\t);\n\t\t\t};\n\t\t`,\n\t\tfilePath,\n\t\t{\n\t\t\tincludeNewlineAtEnd: false,\n\t\t},\n\t);\n\n\treturn {\n\t\ttitle: \"Create a page for the simulator\",\n\t\tbody: source`\n\t\t\tIn your \\`pages\\` directory, create a file called \\`${fileName}\\` and add the following code. This route will be used to simulate and develop your components.\n\n\t\t\t~~~tsx\n\t\t\t${fileContents}\n\t\t\t~~~\n\t\t`,\n\t};\n};\n\nconst createStep3 = async ({\n\thelpers,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst filePath = helpers.joinPathFromRoot(\"sm.json\");\n\tconst fileContents = await helpers.format(\n\t\tsource`\n\t\t\t{\n\t\t\t\t\"localSliceSimulatorURL\": \"http://localhost:3000/slice-simulator\"\n\t\t\t}\n\t\t`,\n\t\tfilePath,\n\t\t{\n\t\t\tincludeNewlineAtEnd: false,\n\t\t},\n\t);\n\n\treturn {\n\t\ttitle: \"Update `sm.json`\",\n\t\tbody: source`\n\t\t\tUpdate your \\`sm.json\\` file with a \\`localSliceSimulatorURL\\` property pointing to your \\`slice-simulator\\` page.\n\n\t\t\t~~~json\n\t\t\t${fileContents}\n\t\t\t~~~\n\t\t`,\n\t\tvalidate: async () => {\n\t\t\tconst project = await helpers.getProject();\n\n\t\t\tif (!(\"localSliceSimulatorURL\" in project.config)) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing `localSliceSimulatorURL` property\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tA \\`localSliceSimulatorURL\\` property was not found in your \\`sm.json\\` file.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Test if the URL is valid.\n\t\t\ttry {\n\t\t\t\tif (project.config.localSliceSimulatorURL) {\n\t\t\t\t\tnew URL(project.config.localSliceSimulatorURL);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new Error(\"Undefined Slice Simulator URL\");\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"An invalid URL was provided\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tThe \\`localSliceSimulatorURL\\` property should be of the shape \\`http://localhost:PORT/PATH\\`. See the codeblock for an example.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Check if the URL is accessible.\n\t\t\tconst res = await fetch(project.config.localSliceSimulatorURL);\n\n\t\t\tif (!res.ok) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Unable to connect to simulator page\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tCheck that the \\`localSliceSimulatorURL\\` property in \\`sm.json\\` is correct and try again. See the [troubleshooting page](https://prismic.io/docs/technologies/setup-slice-simulator-nextjs) for more details.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n};\n\nexport const sliceSimulatorSetupRead: SliceSimulatorSetupReadHook<\n\tPluginOptions\n> = async (_data, context) => {\n\treturn Promise.all([\n\t\tcreateStep1(context),\n\t\tcreateStep2(context),\n\t\tcreateStep3(context),\n\t]);\n};\n"],"names":["require","createRequire","source","SliceSimulatorSetupStepValidationMessageType","getJSOrTSXFileExtension","fetch"],"mappings":";;;;;;;AAcA,MAAM,wBAAwB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;;AAKD,MAAM,cAAc,OAAO,EAC1B,SACA,cAC4C;AACtC,QAAAA,WAAUC,YAAAA,cAAc,QAAQ,IAAI;AAEnC,SAAA;AAAA,IACN,OAAO;AAAA,IAGP,MAAMC,WAAAA;AAAAA;AAAAA;AAAAA;AAAAA,wBAIgB,sBAAsB,KAAK,GAAG;AAAA;AAAA;AAAA,IAGpD,UAAU,YAAW;AACpB,YAAM,sBAAgC,CAAA;AAEtC,iBAAW,cAAc,uBAAuB;AAC3C,YAAA;AAKH,UAAAF,SAAQ,QAAQ,YAAY;AAAA,YAC3B,OAAO,CAAC,QAAQ,iBAAiB,cAAc,CAAC;AAAA,UAAA,CAChD;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;AAAAA;AAAAA;AAAAA;AAAAA,KAIH;AAAA;AAAA;AAAA,IAGH,UAAU,YAAW;AACd,YAAA,UAAU,MAAM,QAAQ;AAE1B,UAAA,EAAE,4BAA4B,QAAQ,SAAS;AAC3C,eAAA;AAAA,UACN,MAAMC,UAA6C,6CAAA;AAAA,UACnD,OAAO;AAAA,UACP,SAASD,WAAAA;AAAAA;AAAAA;AAAAA,QAAA;AAAA,MAIV;AAGG,UAAA;AACC,YAAA,QAAQ,OAAO,wBAAwB;AACtC,cAAA,IAAI,QAAQ,OAAO,sBAAsB;AAAA,QAAA,OACvC;AACA,gBAAA,IAAI,MAAM,+BAA+B;AAAA,QAC/C;AAAA,MAAA,QACA;AACM,eAAA;AAAA,UACN,MAAMC,UAA6C,6CAAA;AAAA,UACnD,OAAO;AAAA,UACP,SAASD,WAAAA;AAAAA;AAAAA;AAAAA,QAAA;AAAA,MAIV;AAGD,YAAM,MAAM,MAAMG,MAAM,QAAA,QAAQ,OAAO,sBAAsB;AAEzD,UAAA,CAAC,IAAI,IAAI;AACL,eAAA;AAAA,UACN,MAAMF,UAA6C,6CAAA;AAAA,UACnD,OAAO;AAAA,UACP,SAASD,WAAAA;AAAAA;AAAAA;AAAAA,QAAA;AAAA,MAIV;AAAA,IACF;AAAA,EAAA;AAEF;AAEa,MAAA,0BAET,OAAO,OAAO,YAAW;AAC5B,SAAO,QAAQ,IAAI;AAAA,IAClB,YAAY,OAAO;AAAA,IACnB,YAAY,OAAO;AAAA,IACnB,YAAY,OAAO;AAAA,EAAA,CACnB;AACF;;"}
@@ -1,30 +1,31 @@
1
1
  import { SliceSimulatorSetupStepValidationMessageType } from "@slicemachine/plugin-kit";
2
- import { stripIndent } from "common-tags";
2
+ import { source } from "common-tags";
3
3
  import { createRequire } from "node:module";
4
- import * as http from "node:http";
4
+ import fetch from "../node_modules/node-fetch/src/index.js";
5
5
  import { getJSOrTSXFileExtension } from "../lib/getJSOrTSXFileExtension.js";
6
6
  const REQUIRED_DEPENDENCIES = [
7
7
  "@prismicio/react",
8
- "@prismicio/slice-simulator-react",
9
- "@prismicio/client@latest",
8
+ "@prismicio/client",
10
9
  "@prismicio/helpers"
11
10
  ];
12
- const createStep1 = async ({ project }) => {
11
+ const createStep1 = async ({ project, helpers }) => {
13
12
  const require2 = createRequire(project.root);
14
13
  return {
15
14
  title: "Install packages",
16
- body: stripIndent`
15
+ body: source`
17
16
  The simulator requires extra dependencies. Run the following command to install them.
18
17
 
19
18
  ~~~sh
20
- npm install --save @prismicio/react @prismicio/slice-simulator-react @prismicio/client@latest @prismicio/helpers
19
+ npm install --save ${REQUIRED_DEPENDENCIES.join(" ")}
21
20
  ~~~
22
21
  `,
23
22
  validate: async () => {
24
23
  const missingDependencies = [];
25
24
  for (const dependency of REQUIRED_DEPENDENCIES) {
26
25
  try {
27
- require2.resolve(dependency);
26
+ require2.resolve(dependency, {
27
+ paths: [helpers.joinPathFromRoot("node_modules")]
28
+ });
28
29
  } catch {
29
30
  missingDependencies.push(dependency);
30
31
  }
@@ -33,7 +34,7 @@ const createStep1 = async ({ project }) => {
33
34
  return {
34
35
  type: SliceSimulatorSetupStepValidationMessageType.Error,
35
36
  title: "Missing all dependencies",
36
- message: stripIndent`
37
+ message: source`
37
38
  Install the required dependencies to continue.
38
39
  `
39
40
  };
@@ -41,9 +42,9 @@ const createStep1 = async ({ project }) => {
41
42
  if (missingDependencies.length > 0) {
42
43
  const formattedMissingDependencies = missingDependencies.map((missingDependency) => `\`${missingDependency}\``).join(", ");
43
44
  return {
44
- type: SliceSimulatorSetupStepValidationMessageType.Warning,
45
+ type: SliceSimulatorSetupStepValidationMessageType.Error,
45
46
  title: "Missing some dependencies",
46
- message: stripIndent`
47
+ message: source`
47
48
  The following dependencies are missing: ${formattedMissingDependencies}
48
49
  `
49
50
  };
@@ -52,45 +53,16 @@ const createStep1 = async ({ project }) => {
52
53
  };
53
54
  };
54
55
  const createStep2 = async ({ helpers, options }) => {
55
- const fileName = `slice-simulator.${getJSOrTSXFileExtension}`;
56
+ const fileName = `slice-simulator.${getJSOrTSXFileExtension(options)}`;
56
57
  const filePath = helpers.joinPathFromRoot("pages", fileName);
57
- let fileContents;
58
- if (options.typescript) {
59
- fileContents = stripIndent`
60
- import { GetStaticProps } from "next/types";
61
- import { SliceSimulator } from "@prismicio/slice-simulator-react";
62
- import { SliceZone } from "@prismicio/react";
63
-
64
- import state from "../.slicemachine/libraries-state.json";
65
- import { components } from "../slices";
66
-
67
- const SliceSimulatorPage = () => {
68
- return (
69
- <SliceSimulator
70
- sliceZone={(props) => <SliceZone {...props} components={components} />}
71
- state={state}
72
- />
73
- );
74
- };
75
-
76
- export default SliceSimulatorPage;
77
-
78
- export const getStaticProps: GetStaticProps = () => {
79
- return {
80
- // Exclude this page from production builds.
81
- notFound: process.env.NODE_ENV === "production",
82
- };
83
- };
84
- `;
85
- } else {
86
- fileContents = stripIndent`
58
+ const fileContents = await helpers.format(source`
87
59
  import { SliceSimulator } from "@prismicio/slice-simulator-react";
88
60
  import { SliceZone } from "@prismicio/react";
89
61
 
90
62
  import state from "../.slicemachine/libraries-state.json";
91
63
  import { components } from "../slices";
92
64
 
93
- const SliceSimulatorPage = () => {
65
+ export default function SliceSimulatorPage() {
94
66
  return (
95
67
  <SliceSimulator
96
68
  sliceZone={(props) => <SliceZone {...props} components={components} />}
@@ -98,21 +70,12 @@ const createStep2 = async ({ helpers, options }) => {
98
70
  />
99
71
  );
100
72
  };
101
-
102
- export default SliceSimulatorPage;
103
-
104
- export const getStaticProps= () => {
105
- return {
106
- // Exclude this page from production builds.
107
- notFound: process.env.NODE_ENV === "production",
108
- };
109
- };
110
- `;
111
- }
112
- fileContents = await helpers.format(fileContents, filePath);
73
+ `, filePath, {
74
+ includeNewlineAtEnd: false
75
+ });
113
76
  return {
114
77
  title: "Create a page for the simulator",
115
- body: stripIndent`
78
+ body: source`
116
79
  In your \`pages\` directory, create a file called \`${fileName}\` and add the following code. This route will be used to simulate and develop your components.
117
80
 
118
81
  ~~~tsx
@@ -123,14 +86,16 @@ const createStep2 = async ({ helpers, options }) => {
123
86
  };
124
87
  const createStep3 = async ({ helpers }) => {
125
88
  const filePath = helpers.joinPathFromRoot("sm.json");
126
- const fileContents = await helpers.format(`
89
+ const fileContents = await helpers.format(source`
127
90
  {
128
91
  "localSliceSimulatorURL": "http://localhost:3000/slice-simulator"
129
92
  }
130
- `, filePath);
93
+ `, filePath, {
94
+ includeNewlineAtEnd: false
95
+ });
131
96
  return {
132
97
  title: "Update `sm.json`",
133
- body: stripIndent`
98
+ body: source`
134
99
  Update your \`sm.json\` file with a \`localSliceSimulatorURL\` property pointing to your \`slice-simulator\` page.
135
100
 
136
101
  ~~~json
@@ -143,7 +108,7 @@ const createStep3 = async ({ helpers }) => {
143
108
  return {
144
109
  type: SliceSimulatorSetupStepValidationMessageType.Error,
145
110
  title: "Missing `localSliceSimulatorURL` property",
146
- message: stripIndent`
111
+ message: source`
147
112
  A \`localSliceSimulatorURL\` property was not found in your \`sm.json\` file.
148
113
  `
149
114
  };
@@ -156,28 +121,19 @@ const createStep3 = async ({ helpers }) => {
156
121
  }
157
122
  } catch {
158
123
  return {
159
- type: SliceSimulatorSetupStepValidationMessageType.Warning,
124
+ type: SliceSimulatorSetupStepValidationMessageType.Error,
160
125
  title: "An invalid URL was provided",
161
- message: stripIndent`
126
+ message: source`
162
127
  The \`localSliceSimulatorURL\` property should be of the shape \`http://localhost:PORT/PATH\`. See the codeblock for an example.
163
128
  `
164
129
  };
165
130
  }
166
- const ok = await new Promise((resolve) => {
167
- if (project.config.localSliceSimulatorURL) {
168
- http.get(project.config.localSliceSimulatorURL, (res) => {
169
- if (res.statusCode) {
170
- resolve(res.statusCode >= 200 && res.statusCode < 300);
171
- }
172
- });
173
- }
174
- resolve(false);
175
- });
176
- if (!ok) {
131
+ const res = await fetch(project.config.localSliceSimulatorURL);
132
+ if (!res.ok) {
177
133
  return {
178
- type: SliceSimulatorSetupStepValidationMessageType.Warning,
134
+ type: SliceSimulatorSetupStepValidationMessageType.Error,
179
135
  title: "Unable to connect to simulator page",
180
- message: stripIndent`
136
+ message: source`
181
137
  Check that the \`localSliceSimulatorURL\` property in \`sm.json\` is correct and try again. See the [troubleshooting page](https://prismic.io/docs/technologies/setup-slice-simulator-nextjs) for more details.
182
138
  `
183
139
  };
@@ -1 +1 @@
1
- {"version":3,"file":"sliceSimulator-setup-read.js","sources":["../../src/hooks/sliceSimulator-setup-read.ts"],"sourcesContent":["import type {\n\tSliceMachineContext,\n\tSliceSimulatorSetupReadHook,\n\tSliceSimulatorSetupStep,\n} from \"@slicemachine/plugin-kit\";\nimport { SliceSimulatorSetupStepValidationMessageType } from \"@slicemachine/plugin-kit\";\nimport { stripIndent } from \"common-tags\";\nimport { createRequire } from \"node:module\";\nimport * as http from \"node:http\";\n\nimport { getJSOrTSXFileExtension } from \"../lib/getJSOrTSXFileExtension\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst REQUIRED_DEPENDENCIES = [\n\t\"@prismicio/react\",\n\t\"@prismicio/slice-simulator-react\",\n\t\"@prismicio/client@latest\",\n\t\"@prismicio/helpers\",\n];\n\ntype Args = SliceMachineContext<PluginOptions>;\n\nconst createStep1 = async ({\n\tproject,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst require = createRequire(project.root);\n\n\treturn {\n\t\ttitle: \"Install packages\",\n\t\tbody: stripIndent`\n\t\t\tThe simulator requires extra dependencies. Run the following command to install them.\n\n\t\t\t~~~sh\n\t\t\tnpm install --save @prismicio/react @prismicio/slice-simulator-react @prismicio/client@latest @prismicio/helpers\n\t\t\t~~~\n\t\t`,\n\t\tvalidate: async () => {\n\t\t\tconst missingDependencies: string[] = [];\n\n\t\t\tfor (const dependency of REQUIRED_DEPENDENCIES) {\n\t\t\t\ttry {\n\t\t\t\t\t// `require.resolve()` is preferred\n\t\t\t\t\t// over `import()` because we don't\n\t\t\t\t\t// want to load the module. Loading a\n\t\t\t\t\t// module could introduce side-effects.\n\t\t\t\t\trequire.resolve(dependency);\n\t\t\t\t} catch {\n\t\t\t\t\tmissingDependencies.push(dependency);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (missingDependencies.length >= REQUIRED_DEPENDENCIES.length) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing all dependencies\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tInstall the required dependencies to continue.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (missingDependencies.length > 0) {\n\t\t\t\tconst formattedMissingDependencies = missingDependencies\n\t\t\t\t\t.map((missingDependency) => `\\`${missingDependency}\\``)\n\t\t\t\t\t.join(\", \");\n\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Warning,\n\t\t\t\t\ttitle: \"Missing some dependencies\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tThe following dependencies are missing: ${formattedMissingDependencies}\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n};\n\nconst createStep2 = async ({\n\thelpers,\n\toptions,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst fileName = `slice-simulator.${getJSOrTSXFileExtension}`;\n\tconst filePath = helpers.joinPathFromRoot(\"pages\", fileName);\n\n\tlet fileContents: string;\n\n\tif (options.typescript) {\n\t\tfileContents = stripIndent`\n\t\t\timport { GetStaticProps } from \"next/types\";\n\t\t\timport { SliceSimulator } from \"@prismicio/slice-simulator-react\";\n\t\t\timport { SliceZone } from \"@prismicio/react\";\n\n\t\t\timport state from \"../.slicemachine/libraries-state.json\";\n\t\t\timport { components } from \"../slices\";\n\n\t\t\tconst SliceSimulatorPage = () => {\n\t\t\t\treturn (\n\t\t\t\t\t<SliceSimulator\n\t\t\t\t\t\tsliceZone={(props) => <SliceZone {...props} components={components} />}\n\t\t\t\t\t\tstate={state}\n\t\t\t\t\t/>\n\t\t\t\t);\n\t\t\t};\n\n\t\t\texport default SliceSimulatorPage;\n\n\t\t\texport const getStaticProps: GetStaticProps = () => {\n\t\t\t\treturn {\n\t\t\t\t\t// Exclude this page from production builds.\n\t\t\t\t\tnotFound: process.env.NODE_ENV === \"production\",\n\t\t\t\t};\n\t\t\t};\n\t\t`;\n\t} else {\n\t\tfileContents = stripIndent`\n\t\t\timport { SliceSimulator } from \"@prismicio/slice-simulator-react\";\n\t\t\timport { SliceZone } from \"@prismicio/react\";\n\n\t\t\timport state from \"../.slicemachine/libraries-state.json\";\n\t\t\timport { components } from \"../slices\";\n\n\t\t\tconst SliceSimulatorPage = () => {\n\t\t\t\treturn (\n\t\t\t\t\t<SliceSimulator\n\t\t\t\t\t\tsliceZone={(props) => <SliceZone {...props} components={components} />}\n\t\t\t\t\t\tstate={state}\n\t\t\t\t\t/>\n\t\t\t\t);\n\t\t\t};\n\n\t\t\texport default SliceSimulatorPage;\n\n\t\t\texport const getStaticProps= () => {\n\t\t\t\treturn {\n\t\t\t\t\t// Exclude this page from production builds.\n\t\t\t\t\tnotFound: process.env.NODE_ENV === \"production\",\n\t\t\t\t};\n\t\t\t};\n\t\t`;\n\t}\n\n\tfileContents = await helpers.format(fileContents, filePath);\n\n\treturn {\n\t\ttitle: \"Create a page for the simulator\",\n\t\tbody: stripIndent`\n\t\t\tIn your \\`pages\\` directory, create a file called \\`${fileName}\\` and add the following code. This route will be used to simulate and develop your components.\n\n\t\t\t~~~tsx\n\t\t\t${fileContents}\n\t\t\t~~~\n\t\t`,\n\t};\n};\n\nconst createStep3 = async ({\n\thelpers,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst filePath = helpers.joinPathFromRoot(\"sm.json\");\n\tconst fileContents = await helpers.format(\n\t\t`\n\t\t\t{\n\t\t\t\t\"localSliceSimulatorURL\": \"http://localhost:3000/slice-simulator\"\n\t\t\t}\n\t\t`,\n\t\tfilePath,\n\t);\n\n\treturn {\n\t\ttitle: \"Update `sm.json`\",\n\t\tbody: stripIndent`\n\t\t\tUpdate your \\`sm.json\\` file with a \\`localSliceSimulatorURL\\` property pointing to your \\`slice-simulator\\` page.\n\n\t\t\t~~~json\n\t\t\t${fileContents}\n\t\t\t~~~\n\t\t`,\n\t\tvalidate: async () => {\n\t\t\tconst project = await helpers.getProject();\n\n\t\t\tif (!(\"localSliceSimulatorURL\" in project.config)) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing `localSliceSimulatorURL` property\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tA \\`localSliceSimulatorURL\\` property was not found in your \\`sm.json\\` file.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Test if the URL is valid.\n\t\t\ttry {\n\t\t\t\tif (project.config.localSliceSimulatorURL) {\n\t\t\t\t\tnew URL(project.config.localSliceSimulatorURL);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new Error(\"Undefined Slice Simulator URL\");\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Warning,\n\t\t\t\t\ttitle: \"An invalid URL was provided\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tThe \\`localSliceSimulatorURL\\` property should be of the shape \\`http://localhost:PORT/PATH\\`. See the codeblock for an example.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Check if the URL is accessible.\n\t\t\tconst ok = await new Promise<boolean>((resolve) => {\n\t\t\t\tif (project.config.localSliceSimulatorURL) {\n\t\t\t\t\thttp.get(project.config.localSliceSimulatorURL, (res) => {\n\t\t\t\t\t\tif (res.statusCode) {\n\t\t\t\t\t\t\tresolve(res.statusCode >= 200 && res.statusCode < 300);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tresolve(false);\n\t\t\t});\n\n\t\t\tif (!ok) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Warning,\n\t\t\t\t\ttitle: \"Unable to connect to simulator page\",\n\t\t\t\t\tmessage: stripIndent`\n\t\t\t\t\t\tCheck that the \\`localSliceSimulatorURL\\` property in \\`sm.json\\` is correct and try again. See the [troubleshooting page](https://prismic.io/docs/technologies/setup-slice-simulator-nextjs) for more details.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n};\n\nexport const sliceSimulatorSetupRead: SliceSimulatorSetupReadHook<\n\tPluginOptions\n> = async (_data, context) => {\n\treturn Promise.all([\n\t\tcreateStep1(context),\n\t\tcreateStep2(context),\n\t\tcreateStep3(context),\n\t]);\n};\n"],"names":["require"],"mappings":";;;;;AAcA,MAAM,wBAAwB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;;AAKD,MAAM,cAAc,OAAO,EAC1B,cAC4C;AACtC,QAAAA,WAAU,cAAc,QAAQ,IAAI;AAEnC,SAAA;AAAA,IACN,OAAO;AAAA,IACP,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAON,UAAU,YAAW;AACpB,YAAM,sBAAgC,CAAA;AAEtC,iBAAW,cAAc,uBAAuB;AAC3C,YAAA;AAKH,UAAAA,SAAQ,QAAQ,UAAU;AAAA,QAAA,QACzB;AACD,8BAAoB,KAAK,UAAU;AAAA,QACnC;AAAA,MACD;AAEG,UAAA,oBAAoB,UAAU,sBAAsB,QAAQ;AACxD,eAAA;AAAA,UACN,MAAM,6CAA6C;AAAA,UACnD,OAAO;AAAA,UACP,SAAS;AAAA;AAAA;AAAA,QAAA;AAAA,MAIV;AAEG,UAAA,oBAAoB,SAAS,GAAG;AAC7B,cAAA,+BAA+B,oBACnC,IAAI,CAAC,sBAAsB,KAAK,qBAAqB,EACrD,KAAK,IAAI;AAEJ,eAAA;AAAA,UACN,MAAM,6CAA6C;AAAA,UACnD,OAAO;AA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,OAAO,wBAAwB;AACtC,cAAA,IAAI,QAAQ,OAAO,sBAAsB;AAAA,QAAA,OACvC;AACA,gBAAA,IAAI,MAAM,+BAA+B;AAAA,QAC/C;AAAA,MAAA,QACA;AACM,eAAA;AAAA,UACN,MAAM,6CAA6C;AAAA,UACnD,OAAO;AAAA,UACP,SAAS;AAAA;AAAA;AAAA,QAAA;AAAA,MAIV;AAGD,YAAM,KAAK,MAAM,IAAI,QAAiB,CAAC,YAAW;AAC7C,YAAA,QAAQ,OAAO,wBAAwB;AAC1C,eAAK,IAAI,QAAQ,OAAO,wBAAwB,CAAC,QAAO;AACvD,gBAAI,IAAI,YAAY;AACnB,sBAAQ,IAAI,cAAc,OAAO,IAAI,aAAa,GAAG;AAAA,YACrD;AAAA,UAAA,CACD;AAAA,QACD;AAED,gBAAQ,KAAK;AAAA,MAAA,CACb;AAED,UAAI,CAAC,IAAI;AACD,eAAA;AAAA,UACN,MAAM,6CAA6C;AAAA,UACnD,OAAO;AAAA,UACP,SAAS;AAAA;AAAA;AAAA,QAAA;AAAA,MAIV;AAAA,IACF;AAAA,EAAA;AAEF;AAEa,MAAA,0BAET,OAAO,OAAO,YAAW;AAC5B,SAAO,QAAQ,IAAI;AAAA,IAClB,YAAY,OAAO;AAAA,IACnB,YAAY,OAAO;AAAA,IACnB,YAAY,OAAO;AAAA,EAAA,CACnB;AACF;"}
1
+ {"version":3,"file":"sliceSimulator-setup-read.js","sources":["../../../src/hooks/sliceSimulator-setup-read.ts"],"sourcesContent":["import type {\n\tSliceMachineContext,\n\tSliceSimulatorSetupReadHook,\n\tSliceSimulatorSetupStep,\n} from \"@slicemachine/plugin-kit\";\nimport { SliceSimulatorSetupStepValidationMessageType } from \"@slicemachine/plugin-kit\";\nimport { source } from \"common-tags\";\nimport { createRequire } from \"node:module\";\nimport fetch from \"node-fetch\";\n\nimport { getJSOrTSXFileExtension } from \"../lib/getJSOrTSXFileExtension\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst REQUIRED_DEPENDENCIES = [\n\t\"@prismicio/react\",\n\t\"@prismicio/client\",\n\t\"@prismicio/helpers\",\n];\n\ntype Args = SliceMachineContext<PluginOptions>;\n\nconst createStep1 = async ({\n\tproject,\n\thelpers,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst require = createRequire(project.root);\n\n\treturn {\n\t\ttitle: \"Install packages\",\n\t\t// TODO: Create a plugin runner helper to provide the correct\n\t\t// package manager\n\t\tbody: source`\n\t\t\tThe simulator requires extra dependencies. Run the following command to install them.\n\n\t\t\t~~~sh\n\t\t\tnpm install --save ${REQUIRED_DEPENDENCIES.join(\" \")}\n\t\t\t~~~\n\t\t`,\n\t\tvalidate: async () => {\n\t\t\tconst missingDependencies: string[] = [];\n\n\t\t\tfor (const dependency of REQUIRED_DEPENDENCIES) {\n\t\t\t\ttry {\n\t\t\t\t\t// `require.resolve()` is preferred\n\t\t\t\t\t// over `import()` because we don't\n\t\t\t\t\t// want to load the module. Loading a\n\t\t\t\t\t// module could introduce side-effects.\n\t\t\t\t\trequire.resolve(dependency, {\n\t\t\t\t\t\tpaths: [helpers.joinPathFromRoot(\"node_modules\")],\n\t\t\t\t\t});\n\t\t\t\t} catch {\n\t\t\t\t\tmissingDependencies.push(dependency);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (missingDependencies.length >= REQUIRED_DEPENDENCIES.length) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing all dependencies\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tInstall the required dependencies to continue.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (missingDependencies.length > 0) {\n\t\t\t\tconst formattedMissingDependencies = missingDependencies\n\t\t\t\t\t.map((missingDependency) => `\\`${missingDependency}\\``)\n\t\t\t\t\t.join(\", \");\n\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing some dependencies\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tThe following dependencies are missing: ${formattedMissingDependencies}\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n};\n\nconst createStep2 = async ({\n\thelpers,\n\toptions,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst fileName = `slice-simulator.${getJSOrTSXFileExtension(options)}`;\n\tconst filePath = helpers.joinPathFromRoot(\"pages\", fileName);\n\n\tconst fileContents = await helpers.format(\n\t\tsource`\n\t\t\timport { SliceSimulator } from \"@prismicio/slice-simulator-react\";\n\t\t\timport { SliceZone } from \"@prismicio/react\";\n\n\t\t\timport state from \"../.slicemachine/libraries-state.json\";\n\t\t\timport { components } from \"../slices\";\n\n\t\t\texport default function SliceSimulatorPage() {\n\t\t\t\treturn (\n\t\t\t\t\t<SliceSimulator\n\t\t\t\t\t\tsliceZone={(props) => <SliceZone {...props} components={components} />}\n\t\t\t\t\t\tstate={state}\n\t\t\t\t\t/>\n\t\t\t\t);\n\t\t\t};\n\t\t`,\n\t\tfilePath,\n\t\t{\n\t\t\tincludeNewlineAtEnd: false,\n\t\t},\n\t);\n\n\treturn {\n\t\ttitle: \"Create a page for the simulator\",\n\t\tbody: source`\n\t\t\tIn your \\`pages\\` directory, create a file called \\`${fileName}\\` and add the following code. This route will be used to simulate and develop your components.\n\n\t\t\t~~~tsx\n\t\t\t${fileContents}\n\t\t\t~~~\n\t\t`,\n\t};\n};\n\nconst createStep3 = async ({\n\thelpers,\n}: Args): Promise<SliceSimulatorSetupStep> => {\n\tconst filePath = helpers.joinPathFromRoot(\"sm.json\");\n\tconst fileContents = await helpers.format(\n\t\tsource`\n\t\t\t{\n\t\t\t\t\"localSliceSimulatorURL\": \"http://localhost:3000/slice-simulator\"\n\t\t\t}\n\t\t`,\n\t\tfilePath,\n\t\t{\n\t\t\tincludeNewlineAtEnd: false,\n\t\t},\n\t);\n\n\treturn {\n\t\ttitle: \"Update `sm.json`\",\n\t\tbody: source`\n\t\t\tUpdate your \\`sm.json\\` file with a \\`localSliceSimulatorURL\\` property pointing to your \\`slice-simulator\\` page.\n\n\t\t\t~~~json\n\t\t\t${fileContents}\n\t\t\t~~~\n\t\t`,\n\t\tvalidate: async () => {\n\t\t\tconst project = await helpers.getProject();\n\n\t\t\tif (!(\"localSliceSimulatorURL\" in project.config)) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Missing `localSliceSimulatorURL` property\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tA \\`localSliceSimulatorURL\\` property was not found in your \\`sm.json\\` file.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Test if the URL is valid.\n\t\t\ttry {\n\t\t\t\tif (project.config.localSliceSimulatorURL) {\n\t\t\t\t\tnew URL(project.config.localSliceSimulatorURL);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new Error(\"Undefined Slice Simulator URL\");\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"An invalid URL was provided\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tThe \\`localSliceSimulatorURL\\` property should be of the shape \\`http://localhost:PORT/PATH\\`. See the codeblock for an example.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Check if the URL is accessible.\n\t\t\tconst res = await fetch(project.config.localSliceSimulatorURL);\n\n\t\t\tif (!res.ok) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: SliceSimulatorSetupStepValidationMessageType.Error,\n\t\t\t\t\ttitle: \"Unable to connect to simulator page\",\n\t\t\t\t\tmessage: source`\n\t\t\t\t\t\tCheck that the \\`localSliceSimulatorURL\\` property in \\`sm.json\\` is correct and try again. See the [troubleshooting page](https://prismic.io/docs/technologies/setup-slice-simulator-nextjs) for more details.\n\t\t\t\t\t`,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n};\n\nexport const sliceSimulatorSetupRead: SliceSimulatorSetupReadHook<\n\tPluginOptions\n> = async (_data, context) => {\n\treturn Promise.all([\n\t\tcreateStep1(context),\n\t\tcreateStep2(context),\n\t\tcreateStep3(context),\n\t]);\n};\n"],"names":["require"],"mappings":";;;;;AAcA,MAAM,wBAAwB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;;AAKD,MAAM,cAAc,OAAO,EAC1B,SACA,cAC4C;AACtC,QAAAA,WAAU,cAAc,QAAQ,IAAI;AAEnC,SAAA;AAAA,IACN,OAAO;AAAA,IAGP,MAAM;AAAA;AAAA;AAAA;AAAA,wBAIgB,sBAAsB,KAAK,GAAG;AAAA;AAAA;AAAA,IAGpD,UAAU,YAAW;AACpB,YAAM,sBAAgC,CAAA;AAEtC,iBAAW,cAAc,uBAAuB;AAC3C,YAAA;AAKH,UAAAA,SAAQ,QAAQ,YAAY;AAAA,YAC3B,OAAO,CAAC,QAAQ,iBAAiB,cAAc,CAAC;AAAA,UAAA,CAChD;AAAA,QAAA,QACA;AACD,8BAAoB,KAAK,UAAU;AAAA,QACnC;AAAA,MACD;AAEG,UAAA,oBAAoB,UAAU,sBAAsB,QAAQ;AACxD,eAAA;AAAA,UACN,MAAM,6CAA6C;AAAA,UACnD,OAAO;AAAA,UACP,SAAS;AAAA;AAAA;AAAA,QAAA;AAAA,MAIV;AAEG,UAAA,oBAAoB,SAAS,GAAG;AAC7B,cAAA,+BAA+B,oBACnC,IAAI,CAAC,sBAAsB,KAAK,qBAAqB,EACrD,KAAK,IAAI;AAEJ,eAAA;AAAA,UACN,MAAM,6CAA6C;AAAA,UACnD,OAAO;AAAA,UACP,SAAS;AAAA,gDACkC;AAAA;AAAA,QAAA;AAAA,MAG5C;AAAA,IACF;AAAA,EAAA;AAEF;AAEA,MAAM,cAAc,OAAO,EAC1B,SACA,cAC4C;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UACN,MAAM,6CAA6C;AAAA,UACnD,OAAO;AAAA,UACP,SAAS;AAAA;AAAA;AAAA,QAAA;AAAA,MAIV;AAAA,IACF;AAAA,EAAA;AAEF;AAEa,MAAA,0BAET,OAAO,OAAO,YAAW;AAC5B,SAAO,QAAQ,IAAI;AAAA,IAClB,YAAY,OAAO;AAAA,IACnB,YAAY,OAAO;AAAA,IACnB,YAAY,OAAO;AAAA,EAAA,CACnB;AACF;"}
@@ -1,10 +1,8 @@
1
1
  "use strict";
2
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const prismicT = require("@prismicio/types");
4
4
  const commonTags = require("common-tags");
5
- function _interopNamespace(e) {
6
- if (e && e.__esModule)
7
- return e;
5
+ function _interopNamespaceDefault(e) {
8
6
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
9
7
  if (e) {
10
8
  for (const k in e) {
@@ -20,22 +18,39 @@ function _interopNamespace(e) {
20
18
  n.default = e;
21
19
  return Object.freeze(n);
22
20
  }
23
- const prismicT__namespace = /* @__PURE__ */ _interopNamespace(prismicT);
24
- const prettierOptions = { parser: "typescript" };
21
+ const prismicT__namespace = /* @__PURE__ */ _interopNamespaceDefault(prismicT);
25
22
  const dotPath = (segments) => {
26
23
  return segments.join(".");
27
24
  };
25
+ const format = async (input, helpers) => {
26
+ const formattedInput = await helpers.format(input, void 0, {
27
+ includeNewlineAtEnd: false,
28
+ prettier: {
29
+ parser: "typescript"
30
+ }
31
+ });
32
+ return formattedInput.endsWith(";") ? formattedInput.substring(0, formattedInput.length - 1) : formattedInput;
33
+ };
28
34
  const snippetRead = async (data, { helpers }) => {
29
35
  const { fieldPath } = data;
30
36
  const label = "React";
31
37
  switch (data.model.type) {
38
+ case prismicT__namespace.CustomTypeModelFieldType.StructuredText: {
39
+ return {
40
+ label,
41
+ language: "tsx",
42
+ code: await format(commonTags.stripIndent`
43
+ <PrismicRichText field={${dotPath(fieldPath)}} />
44
+ `, helpers)
45
+ };
46
+ }
32
47
  case prismicT__namespace.CustomTypeModelFieldType.Link: {
33
48
  return {
34
49
  label,
35
50
  language: "tsx",
36
- code: await helpers.format(commonTags.stripIndent`
51
+ code: await format(commonTags.stripIndent`
37
52
  <PrismicLink field={${dotPath(fieldPath)}}>Link</PrismicLink>
38
- `, void 0, { prettier: prettierOptions })
53
+ `, helpers)
39
54
  };
40
55
  }
41
56
  case prismicT__namespace.CustomTypeModelFieldType.Image: {
@@ -43,25 +58,25 @@ const snippetRead = async (data, { helpers }) => {
43
58
  {
44
59
  label: `${label} (next/image)`,
45
60
  language: "tsx",
46
- code: await helpers.format(commonTags.stripIndent`
61
+ code: await format(commonTags.stripIndent`
47
62
  <PrismicNextImage field={${dotPath(fieldPath)}} />
48
- `, void 0, { prettier: prettierOptions })
63
+ `, helpers)
49
64
  },
50
65
  {
51
66
  label,
52
67
  language: "tsx",
53
- code: await helpers.format(commonTags.stripIndent`
68
+ code: await format(commonTags.stripIndent`
54
69
  <PrismicImage field={${dotPath(fieldPath)}} />
55
- `, void 0, { prettier: prettierOptions })
70
+ `, helpers)
56
71
  }
57
72
  ];
58
73
  }
59
74
  case prismicT__namespace.CustomTypeModelFieldType.Group: {
60
- const code = await helpers.format(commonTags.stripIndent`
75
+ const code = await format(commonTags.stripIndent`
61
76
  <>{${dotPath(fieldPath)}.map(item => (
62
77
  <>{/* Render content for item */}</>
63
78
  ))}</>
64
- `, void 0, { prettier: prettierOptions });
79
+ `, helpers);
65
80
  return {
66
81
  label,
67
82
  language: "tsx",
@@ -69,12 +84,12 @@ const snippetRead = async (data, { helpers }) => {
69
84
  };
70
85
  }
71
86
  case prismicT__namespace.CustomTypeModelFieldType.Slices: {
72
- const code = await helpers.format(commonTags.stripIndent`
87
+ const code = await format(commonTags.stripIndent`
73
88
  <SliceZone
74
89
  slices={${dotPath(fieldPath)}}
75
90
  components={components}
76
91
  />
77
- `, void 0, { prettier: prettierOptions });
92
+ `, helpers);
78
93
  return {
79
94
  label,
80
95
  language: "tsx",
@@ -85,9 +100,9 @@ const snippetRead = async (data, { helpers }) => {
85
100
  return {
86
101
  label,
87
102
  language: "tsx",
88
- code: await helpers.format(commonTags.stripIndent`
103
+ code: await format(commonTags.stripIndent`
89
104
  <>{${dotPath(fieldPath)}}</>
90
- `, void 0, { prettier: prettierOptions })
105
+ `, helpers)
91
106
  };
92
107
  }
93
108
  }
@@ -1 +1 @@
1
- {"version":3,"file":"snippet-read.cjs","sources":["../../src/hooks/snippet-read.ts"],"sourcesContent":["import type { SnippetReadHook } from \"@slicemachine/plugin-kit\";\nimport * as prismicT from \"@prismicio/types\";\nimport { stripIndent } from \"common-tags\";\nimport type { Config as PrettierConfig } from \"prettier\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst prettierOptions: PrettierConfig = { parser: \"typescript\" };\n\nconst dotPath = (segments: string[]): string => {\n\treturn segments.join(\".\");\n};\n\nexport const snippetRead: SnippetReadHook<PluginOptions> = async (\n\tdata,\n\t{ helpers },\n) => {\n\tconst { fieldPath } = data;\n\n\tconst label = \"React\";\n\n\tswitch (data.model.type) {\n\t\tcase prismicT.CustomTypeModelFieldType.Link: {\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode: await helpers.format(\n\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t<PrismicLink field={${dotPath(fieldPath)}}>Link</PrismicLink>\n\t\t\t\t\t`,\n\t\t\t\t\tundefined,\n\t\t\t\t\t{ prettier: prettierOptions },\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\n\t\tcase prismicT.CustomTypeModelFieldType.Image: {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tlabel: `${label} (next/image)`,\n\t\t\t\t\tlanguage: \"tsx\",\n\t\t\t\t\tcode: await helpers.format(\n\t\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t\t<PrismicNextImage field={${dotPath(fieldPath)}} />\n\t\t\t\t\t\t`,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t{ prettier: prettierOptions },\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tlabel,\n\t\t\t\t\tlanguage: \"tsx\",\n\t\t\t\t\tcode: await helpers.format(\n\t\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t\t<PrismicImage field={${dotPath(fieldPath)}} />\n\t\t\t\t\t\t`,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t{ prettier: prettierOptions },\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t];\n\t\t}\n\n\t\tcase prismicT.CustomTypeModelFieldType.Group: {\n\t\t\tconst code = await helpers.format(\n\t\t\t\tstripIndent`\n\t\t\t\t\t<>{${dotPath(fieldPath)}.map(item => (\n\t\t\t\t\t <>{/* Render content for item */}</>\n\t\t\t\t\t))}</>\n\t\t\t\t`,\n\t\t\t\tundefined,\n\t\t\t\t{ prettier: prettierOptions },\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode,\n\t\t\t};\n\t\t}\n\n\t\tcase prismicT.CustomTypeModelFieldType.Slices: {\n\t\t\tconst code = await helpers.format(\n\t\t\t\tstripIndent`\n\t\t\t\t\t<SliceZone\n\t\t\t\t\t slices={${dotPath(fieldPath)}}\n\t\t\t\t\t components={components}\n\t\t\t\t\t/>\n\t\t\t\t`,\n\t\t\t\tundefined,\n\t\t\t\t{ prettier: prettierOptions },\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode,\n\t\t\t};\n\t\t}\n\n\t\tdefault: {\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode: await helpers.format(\n\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t<>{${dotPath(fieldPath)}}</>\n\t\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AA,IAED,KAAKD,oBAAS,yBAAyB,QAAQ;AACxC,YAAA,OAAO,MAAM,QAAQ,OAC1BC,WAAAA;AAAAA;AAAAA,iBAEa,QAAQ,SAAS;AAAA;AAAA;AAAA,OAI9B,QACA,EAAE,UAAU,gBAAiB,CAAA;AAGvB,aAAA;AAAA,QACN;AAAA,QACA,UAAU;AAAA,QACV;AAAA,MAAA;AAAA,IAED;AAAA,IAED,SAAS;AACD,aAAA;AAAA,QACN;AAAA,QACA,UAAU;AAAA,QACV,MAAM,MAAM,QAAQ,OACnBA;WACM,QAAQ,SAAS;AAAA,QAEvB,QACA,EAAE,UAAU,gBAAA,CAAiB;AAAA,MAAA;AAAA,IAG/B;AAAA,EACD;AACF;;"}
1
+ {"version":3,"file":"snippet-read.cjs","sources":["../../../src/hooks/snippet-read.ts"],"sourcesContent":["import type {\n\tSliceMachineHelpers,\n\tSnippetReadHook,\n} from \"@slicemachine/plugin-kit\";\nimport * as prismicT from \"@prismicio/types\";\nimport { stripIndent } from \"common-tags\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst dotPath = (segments: string[]): string => {\n\treturn segments.join(\".\");\n};\n\nconst format = async (input: string, helpers: SliceMachineHelpers) => {\n\tconst formattedInput = await helpers.format(input, undefined, {\n\t\tincludeNewlineAtEnd: false,\n\t\tprettier: {\n\t\t\tparser: \"typescript\",\n\t\t},\n\t});\n\n\treturn formattedInput.endsWith(\";\")\n\t\t? formattedInput.substring(0, formattedInput.length - 1)\n\t\t: formattedInput;\n};\n\nexport const snippetRead: SnippetReadHook<PluginOptions> = async (\n\tdata,\n\t{ helpers },\n) => {\n\tconst { fieldPath } = data;\n\n\tconst label = \"React\";\n\n\tswitch (data.model.type) {\n\t\tcase prismicT.CustomTypeModelFieldType.StructuredText: {\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode: await format(\n\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t<PrismicRichText field={${dotPath(fieldPath)}} />\n\t\t\t\t\t`,\n\t\t\t\t\thelpers,\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\n\t\tcase prismicT.CustomTypeModelFieldType.Link: {\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode: await format(\n\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t<PrismicLink field={${dotPath(fieldPath)}}>Link</PrismicLink>\n\t\t\t\t\t`,\n\t\t\t\t\thelpers,\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\n\t\tcase prismicT.CustomTypeModelFieldType.Image: {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tlabel: `${label} (next/image)`,\n\t\t\t\t\tlanguage: \"tsx\",\n\t\t\t\t\tcode: await format(\n\t\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t\t<PrismicNextImage field={${dotPath(fieldPath)}} />\n\t\t\t\t\t\t`,\n\t\t\t\t\t\thelpers,\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tlabel,\n\t\t\t\t\tlanguage: \"tsx\",\n\t\t\t\t\tcode: await format(\n\t\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t\t<PrismicImage field={${dotPath(fieldPath)}} />\n\t\t\t\t\t\t`,\n\t\t\t\t\t\thelpers,\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t];\n\t\t}\n\n\t\tcase prismicT.CustomTypeModelFieldType.Group: {\n\t\t\tconst code = await format(\n\t\t\t\tstripIndent`\n\t\t\t\t\t<>{${dotPath(fieldPath)}.map(item => (\n\t\t\t\t\t <>{/* Render content for item */}</>\n\t\t\t\t\t))}</>\n\t\t\t\t`,\n\t\t\t\thelpers,\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode,\n\t\t\t};\n\t\t}\n\n\t\tcase prismicT.CustomTypeModelFieldType.Slices: {\n\t\t\tconst code = await format(\n\t\t\t\tstripIndent`\n\t\t\t\t\t<SliceZone\n\t\t\t\t\t slices={${dotPath(fieldPath)}}\n\t\t\t\t\t components={components}\n\t\t\t\t\t/>\n\t\t\t\t`,\n\t\t\t\thelpers,\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode,\n\t\t\t};\n\t\t}\n\n\t\tdefault: {\n\t\t\treturn {\n\t\t\t\tlabel,\n\t\t\t\tlanguage: \"tsx\",\n\t\t\t\tcode: await format(\n\t\t\t\t\tstripIndent`\n\t\t\t\t\t\t<>{${dotPath(fieldPath)}}</>\n\t\t\t\t\t`,\n\t\t\t\t\thelpers,\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\t}\n};\n"],"names":["prismicT","stripIndent"],"mappings":";;;;;;;;;;;;;;;;;;;;;AASA,MAAM,UAAU,CAAC,aAA8B;AACvC,SAAA,SAAS,KAAK,GAAG;AACzB;AAEA,MAAM,SAAS,OAAO,OAAe,YAAgC;AACpE,QAAM,iBAAiB,MAAM,QAAQ,OAAO,OAAO,QAAW;AAAA,IAC7D,qBAAqB;AAAA,IACrB,UAAU;AAAA,MACT,QAAQ;AAAA,IACR;AAAA,EAAA,CACD;AAEM,SAAA,eAAe,SAAS,GAAG,IAC/B,eAAe,UAAU,GAAG,eAAe,SAAS,CAAC,IACrD;AACJ;AAEO,MAAM,cAA8C,OAC1D,MACA,EAAE,cACC;AACG,QAAA,EAAE,UAAc,IAAA;AAEtB,QAAM,QAAQ;AAEN,UAAA,KAAK,MAAM,MAAM;AAAA,IACxB,KAAKA,oBAAS,yBAAyB,gBAAgB;AAC/C,aAAA;AAAA,QACN;AAAA,QACA,UAAU;AAAA,QACV,MAAM,MAAM,OACXC;gCAC2B,QAAQ,SAAS;AAAA,QAE5C,OAAO;AAAA,MAAA;AAAA,IAGT;AAAA,IAED,KAAKD,oBAAS,yBAAyB,MAAM;AACrC,aAAA;AAAA,QACN;AAAA,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