@slicemachine/adapter-next 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/dist/SliceSimulator.cjs +93 -0
  2. package/dist/SliceSimulator.cjs.map +1 -0
  3. package/dist/SliceSimulator.d.ts +29 -0
  4. package/dist/SliceSimulator.js +74 -0
  5. package/dist/SliceSimulator.js.map +1 -0
  6. package/dist/hooks/customType-asset-delete.cjs +28 -0
  7. package/dist/hooks/customType-asset-delete.cjs.map +1 -0
  8. package/dist/hooks/customType-asset-delete.d.ts +3 -0
  9. package/dist/hooks/customType-asset-delete.js +9 -0
  10. package/dist/hooks/customType-asset-delete.js.map +1 -0
  11. package/dist/hooks/customType-asset-read.cjs +31 -0
  12. package/dist/hooks/customType-asset-read.cjs.map +1 -0
  13. package/dist/hooks/customType-asset-read.d.ts +3 -0
  14. package/dist/hooks/customType-asset-read.js +12 -0
  15. package/dist/hooks/customType-asset-read.js.map +1 -0
  16. package/dist/hooks/customType-asset-update.cjs +31 -0
  17. package/dist/hooks/customType-asset-update.cjs.map +1 -0
  18. package/dist/hooks/customType-asset-update.d.ts +3 -0
  19. package/dist/hooks/customType-asset-update.js +11 -0
  20. package/dist/hooks/customType-asset-update.js.map +1 -0
  21. package/dist/hooks/customType-create.cjs +41 -0
  22. package/dist/hooks/customType-create.cjs.map +1 -0
  23. package/dist/hooks/customType-create.d.ts +3 -0
  24. package/dist/hooks/customType-create.js +21 -0
  25. package/dist/hooks/customType-create.js.map +1 -0
  26. package/dist/hooks/customType-delete.cjs +30 -0
  27. package/dist/hooks/customType-delete.cjs.map +1 -0
  28. package/dist/hooks/customType-delete.d.ts +3 -0
  29. package/dist/hooks/customType-delete.js +11 -0
  30. package/dist/hooks/customType-delete.js.map +1 -0
  31. package/dist/hooks/customType-read.cjs +12 -0
  32. package/dist/hooks/customType-read.cjs.map +1 -0
  33. package/dist/hooks/customType-read.d.ts +3 -0
  34. package/dist/hooks/customType-read.js +12 -0
  35. package/dist/hooks/customType-read.js.map +1 -0
  36. package/dist/hooks/customType-update.cjs +6 -0
  37. package/dist/hooks/customType-update.cjs.map +1 -0
  38. package/dist/hooks/customType-update.d.ts +1 -0
  39. package/dist/hooks/customType-update.js +6 -0
  40. package/dist/hooks/customType-update.js.map +1 -0
  41. package/dist/hooks/customTypeLibrary-read.cjs +46 -0
  42. package/dist/hooks/customTypeLibrary-read.cjs.map +1 -0
  43. package/dist/hooks/customTypeLibrary-read.d.ts +3 -0
  44. package/dist/hooks/customTypeLibrary-read.js +26 -0
  45. package/dist/hooks/customTypeLibrary-read.js.map +1 -0
  46. package/dist/hooks/slice-asset-delete.cjs +29 -0
  47. package/dist/hooks/slice-asset-delete.cjs.map +1 -0
  48. package/dist/hooks/slice-asset-delete.d.ts +3 -0
  49. package/dist/hooks/slice-asset-delete.js +10 -0
  50. package/dist/hooks/slice-asset-delete.js.map +1 -0
  51. package/dist/hooks/slice-asset-read.cjs +32 -0
  52. package/dist/hooks/slice-asset-read.cjs.map +1 -0
  53. package/dist/hooks/slice-asset-read.d.ts +3 -0
  54. package/dist/hooks/slice-asset-read.js +13 -0
  55. package/dist/hooks/slice-asset-read.js.map +1 -0
  56. package/dist/hooks/slice-asset-update.cjs +32 -0
  57. package/dist/hooks/slice-asset-update.cjs.map +1 -0
  58. package/dist/hooks/slice-asset-update.d.ts +3 -0
  59. package/dist/hooks/slice-asset-update.js +12 -0
  60. package/dist/hooks/slice-asset-update.js.map +1 -0
  61. package/dist/hooks/slice-create.cjs +119 -0
  62. package/dist/hooks/slice-create.cjs.map +1 -0
  63. package/dist/hooks/slice-create.d.ts +3 -0
  64. package/dist/hooks/slice-create.js +99 -0
  65. package/dist/hooks/slice-create.js.map +1 -0
  66. package/dist/hooks/slice-delete.cjs +49 -0
  67. package/dist/hooks/slice-delete.cjs.map +1 -0
  68. package/dist/hooks/slice-delete.d.ts +3 -0
  69. package/dist/hooks/slice-delete.js +30 -0
  70. package/dist/hooks/slice-delete.js.map +1 -0
  71. package/dist/hooks/slice-read.cjs +13 -0
  72. package/dist/hooks/slice-read.cjs.map +1 -0
  73. package/dist/hooks/slice-read.d.ts +3 -0
  74. package/dist/hooks/slice-read.js +13 -0
  75. package/dist/hooks/slice-read.js.map +1 -0
  76. package/dist/hooks/slice-update.cjs +41 -0
  77. package/dist/hooks/slice-update.cjs.map +1 -0
  78. package/dist/hooks/slice-update.d.ts +3 -0
  79. package/dist/hooks/slice-update.js +21 -0
  80. package/dist/hooks/slice-update.js.map +1 -0
  81. package/dist/hooks/sliceLibrary-read.cjs +52 -0
  82. package/dist/hooks/sliceLibrary-read.cjs.map +1 -0
  83. package/dist/hooks/sliceLibrary-read.d.ts +3 -0
  84. package/dist/hooks/sliceLibrary-read.js +31 -0
  85. package/dist/hooks/sliceLibrary-read.js.map +1 -0
  86. package/dist/hooks/sliceSimulator-setup-read.cjs +217 -0
  87. package/dist/hooks/sliceSimulator-setup-read.cjs.map +1 -0
  88. package/dist/hooks/sliceSimulator-setup-read.d.ts +3 -0
  89. package/dist/hooks/sliceSimulator-setup-read.js +198 -0
  90. package/dist/hooks/sliceSimulator-setup-read.js.map +1 -0
  91. package/dist/hooks/snippet-read.cjs +96 -0
  92. package/dist/hooks/snippet-read.cjs.map +1 -0
  93. package/dist/hooks/snippet-read.d.ts +3 -0
  94. package/dist/hooks/snippet-read.js +77 -0
  95. package/dist/hooks/snippet-read.js.map +1 -0
  96. package/dist/index.cjs +6 -698
  97. package/dist/index.cjs.map +1 -1
  98. package/dist/index.d.ts +5 -45
  99. package/dist/index.js +6 -669
  100. package/dist/index.js.map +1 -1
  101. package/dist/lib/buildSliceLibraryIndexFileContents.cjs +23 -0
  102. package/dist/lib/buildSliceLibraryIndexFileContents.cjs.map +1 -0
  103. package/dist/lib/buildSliceLibraryIndexFileContents.d.ts +10 -0
  104. package/dist/lib/buildSliceLibraryIndexFileContents.js +23 -0
  105. package/dist/lib/buildSliceLibraryIndexFileContents.js.map +1 -0
  106. package/dist/lib/getJSOrTSXFileExtension.cjs +13 -0
  107. package/dist/lib/getJSOrTSXFileExtension.cjs.map +1 -0
  108. package/dist/lib/getJSOrTSXFileExtension.d.ts +2 -0
  109. package/dist/lib/getJSOrTSXFileExtension.js +13 -0
  110. package/dist/lib/getJSOrTSXFileExtension.js.map +1 -0
  111. package/dist/lib/pascalCase.cjs +8 -0
  112. package/dist/lib/pascalCase.cjs.map +1 -0
  113. package/dist/lib/pascalCase.d.ts +8 -0
  114. package/dist/lib/pascalCase.js +8 -0
  115. package/dist/lib/pascalCase.js.map +1 -0
  116. package/dist/lib/readJSONFile.cjs +28 -0
  117. package/dist/lib/readJSONFile.cjs.map +1 -0
  118. package/dist/lib/readJSONFile.d.ts +1 -0
  119. package/dist/lib/readJSONFile.js +9 -0
  120. package/dist/lib/readJSONFile.js.map +1 -0
  121. package/dist/lib/rejectIfNecessary.cjs +10 -0
  122. package/dist/lib/rejectIfNecessary.cjs.map +1 -0
  123. package/dist/lib/rejectIfNecessary.d.ts +1 -0
  124. package/dist/lib/rejectIfNecessary.js +10 -0
  125. package/dist/lib/rejectIfNecessary.js.map +1 -0
  126. package/dist/lib/upsertGlobalContentTypes.cjs +53 -0
  127. package/dist/lib/upsertGlobalContentTypes.cjs.map +1 -0
  128. package/dist/lib/upsertGlobalContentTypes.d.ts +12 -0
  129. package/dist/lib/upsertGlobalContentTypes.js +33 -0
  130. package/dist/lib/upsertGlobalContentTypes.js.map +1 -0
  131. package/dist/package.cjs +5 -0
  132. package/dist/package.cjs.map +1 -0
  133. package/dist/package.js +5 -0
  134. package/dist/package.js.map +1 -0
  135. package/dist/plugin.cjs +52 -0
  136. package/dist/plugin.cjs.map +1 -0
  137. package/dist/plugin.d.ts +2 -0
  138. package/dist/plugin.js +52 -0
  139. package/dist/plugin.js.map +1 -0
  140. package/dist/types.d.ts +9 -0
  141. package/package.json +26 -23
  142. package/src/hooks/customType-asset-delete.ts +16 -0
  143. package/src/hooks/customType-asset-read.ts +20 -0
  144. package/src/hooks/customType-asset-update.ts +18 -0
  145. package/src/hooks/customType-create.ts +5 -20
  146. package/src/hooks/customType-delete.ts +6 -2
  147. package/src/hooks/customType-read.ts +5 -1
  148. package/src/hooks/{customType-library-read.ts → customTypeLibrary-read.ts} +3 -0
  149. package/src/hooks/slice-asset-delete.ts +19 -0
  150. package/src/hooks/slice-asset-read.ts +23 -0
  151. package/src/hooks/slice-asset-update.ts +21 -0
  152. package/src/hooks/slice-create.ts +14 -21
  153. package/src/hooks/slice-delete.ts +9 -1
  154. package/src/hooks/slice-read.ts +5 -1
  155. package/src/hooks/slice-update.ts +4 -19
  156. package/src/hooks/{slice-library-read.ts → sliceLibrary-read.ts} +3 -0
  157. package/src/lib/rejectIfNecessary.ts +16 -0
  158. package/src/lib/upsertGlobalContentTypes.ts +59 -0
  159. package/src/plugin.ts +20 -8
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slice-read.cjs","sources":["../../src/hooks/slice-read.ts"],"sourcesContent":["import type { SliceReadHook } from \"@slicemachine/plugin-kit\";\nimport * as prismicT from \"@prismicio/types\";\n\nimport { readJSONFile } from \"../lib/readJSONFile\";\nimport { pascalCase } from \"../lib/pascalCase\";\n\nimport type { PluginOptions } from \"../types\";\n\nexport const sliceRead: SliceReadHook<PluginOptions> = async (\n\tdata,\n\t{ helpers },\n) => {\n\tconst filePath = helpers.joinPathFromRoot(\n\t\tdata.libraryID,\n\t\tpascalCase(data.sliceID),\n\t\t\"model.json\",\n\t);\n\n\tconst model = await readJSONFile<prismicT.SharedSliceModel>(filePath);\n\n\treturn {\n\t\tmodel,\n\t};\n};\n"],"names":["pascalCase","readJSONFile"],"mappings":";;;;AAQO,MAAM,YAA0C,OACtD,MACA,EAAE,cACC;AACG,QAAA,WAAW,QAAQ,iBACxB,KAAK,WACLA,WAAW,WAAA,KAAK,OAAO,GACvB,YAAY;AAGP,QAAA,QAAQ,MAAMC,0BAAwC,QAAQ;AAE7D,SAAA;AAAA,IACN;AAAA,EAAA;AAEF;;"}
@@ -0,0 +1,3 @@
1
+ import type { SliceReadHook } from "@slicemachine/plugin-kit";
2
+ import type { PluginOptions } from "../types";
3
+ export declare const sliceRead: SliceReadHook<PluginOptions>;
@@ -0,0 +1,13 @@
1
+ import { readJSONFile } from "../lib/readJSONFile.js";
2
+ import { pascalCase } from "../lib/pascalCase.js";
3
+ const sliceRead = async (data, { helpers }) => {
4
+ const filePath = helpers.joinPathFromRoot(data.libraryID, pascalCase(data.sliceID), "model.json");
5
+ const model = await readJSONFile(filePath);
6
+ return {
7
+ model
8
+ };
9
+ };
10
+ export {
11
+ sliceRead
12
+ };
13
+ //# sourceMappingURL=slice-read.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slice-read.js","sources":["../../src/hooks/slice-read.ts"],"sourcesContent":["import type { SliceReadHook } from \"@slicemachine/plugin-kit\";\nimport * as prismicT from \"@prismicio/types\";\n\nimport { readJSONFile } from \"../lib/readJSONFile\";\nimport { pascalCase } from \"../lib/pascalCase\";\n\nimport type { PluginOptions } from \"../types\";\n\nexport const sliceRead: SliceReadHook<PluginOptions> = async (\n\tdata,\n\t{ helpers },\n) => {\n\tconst filePath = helpers.joinPathFromRoot(\n\t\tdata.libraryID,\n\t\tpascalCase(data.sliceID),\n\t\t\"model.json\",\n\t);\n\n\tconst model = await readJSONFile<prismicT.SharedSliceModel>(filePath);\n\n\treturn {\n\t\tmodel,\n\t};\n};\n"],"names":[],"mappings":";;AAQO,MAAM,YAA0C,OACtD,MACA,EAAE,cACC;AACG,QAAA,WAAW,QAAQ,iBACxB,KAAK,WACL,WAAW,KAAK,OAAO,GACvB,YAAY;AAGP,QAAA,QAAQ,MAAM,aAAwC,QAAQ;AAE7D,SAAA;AAAA,IACN;AAAA,EAAA;AAEF;"}
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const fs = require("node:fs/promises");
4
+ const path = require("node:path");
5
+ const pascalCase = require("../lib/pascalCase.cjs");
6
+ const upsertGlobalContentTypes = require("../lib/upsertGlobalContentTypes.cjs");
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule)
9
+ return e;
10
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
11
+ if (e) {
12
+ for (const k in e) {
13
+ if (k !== "default") {
14
+ const d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: () => e[k]
18
+ });
19
+ }
20
+ }
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
25
+ const fs__namespace = /* @__PURE__ */ _interopNamespace(fs);
26
+ const path__namespace = /* @__PURE__ */ _interopNamespace(path);
27
+ const updateModelFile = async ({ dir, data, helpers, options }) => {
28
+ const filePath = path__namespace.join(dir, "model.json");
29
+ let contents = JSON.stringify(data.model);
30
+ if (options.format) {
31
+ contents = await helpers.format(contents, filePath);
32
+ }
33
+ await fs__namespace.writeFile(filePath, contents);
34
+ };
35
+ const sliceUpdate = async (data, context) => {
36
+ const dir = context.helpers.joinPathFromRoot(data.libraryID, pascalCase.pascalCase(data.model.id));
37
+ await updateModelFile({ dir, data, ...context });
38
+ await upsertGlobalContentTypes.upsertGlobalContentTypes(context);
39
+ };
40
+ exports.sliceUpdate = sliceUpdate;
41
+ //# sourceMappingURL=slice-update.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slice-update.cjs","sources":["../../src/hooks/slice-update.ts"],"sourcesContent":["import type {\n\tSliceMachineContext,\n\tSliceUpdateHook,\n\tSliceUpdateHookData,\n} from \"@slicemachine/plugin-kit\";\nimport * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\n\nimport { pascalCase } from \"../lib/pascalCase\";\nimport { upsertGlobalContentTypes } from \"../lib/upsertGlobalContentTypes\";\n\nimport type { PluginOptions } from \"../types\";\n\ntype Args = {\n\tdir: string;\n\tdata: SliceUpdateHookData;\n} & SliceMachineContext<PluginOptions>;\n\nconst updateModelFile = async ({ dir, data, helpers, options }: Args) => {\n\tconst filePath = path.join(dir, \"model.json\");\n\n\tlet contents = JSON.stringify(data.model);\n\n\tif (options.format) {\n\t\tcontents = await helpers.format(contents, filePath);\n\t}\n\n\tawait fs.writeFile(filePath, contents);\n};\n\nexport const sliceUpdate: SliceUpdateHook<PluginOptions> = async (\n\tdata,\n\tcontext,\n) => {\n\tconst dir = context.helpers.joinPathFromRoot(\n\t\tdata.libraryID,\n\t\tpascalCase(data.model.id),\n\t);\n\n\tawait updateModelFile({ dir, data, ...context });\n\n\tawait upsertGlobalContentTypes(context);\n};\n"],"names":["path","fs","pascalCase","upsertGlobalContentTypes"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA,MAAM,kBAAkB,OAAO,EAAE,KAAK,MAAM,SAAS,cAAmB;AACvE,QAAM,WAAWA,gBAAK,KAAK,KAAK,YAAY;AAE5C,MAAI,WAAW,KAAK,UAAU,KAAK,KAAK;AAExC,MAAI,QAAQ,QAAQ;AACnB,eAAW,MAAM,QAAQ,OAAO,UAAU,QAAQ;AAAA,EAClD;AAEK,QAAAC,cAAG,UAAU,UAAU,QAAQ;AACtC;AAEa,MAAA,cAA8C,OAC1D,MACA,YACG;AACG,QAAA,MAAM,QAAQ,QAAQ,iBAC3B,KAAK,WACLC,WAAAA,WAAW,KAAK,MAAM,EAAE,CAAC;AAG1B,QAAM,gBAAgB,EAAE,KAAK,MAAM,GAAG,QAAS,CAAA;AAE/C,QAAMC,yBAAAA,yBAAyB,OAAO;AACvC;;"}
@@ -0,0 +1,3 @@
1
+ import type { SliceUpdateHook } from "@slicemachine/plugin-kit";
2
+ import type { PluginOptions } from "../types";
3
+ export declare const sliceUpdate: SliceUpdateHook<PluginOptions>;
@@ -0,0 +1,21 @@
1
+ import * as fs from "node:fs/promises";
2
+ import * as path from "node:path";
3
+ import { pascalCase } from "../lib/pascalCase.js";
4
+ import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes.js";
5
+ const updateModelFile = async ({ dir, data, helpers, options }) => {
6
+ const filePath = path.join(dir, "model.json");
7
+ let contents = JSON.stringify(data.model);
8
+ if (options.format) {
9
+ contents = await helpers.format(contents, filePath);
10
+ }
11
+ await fs.writeFile(filePath, contents);
12
+ };
13
+ const sliceUpdate = async (data, context) => {
14
+ const dir = context.helpers.joinPathFromRoot(data.libraryID, pascalCase(data.model.id));
15
+ await updateModelFile({ dir, data, ...context });
16
+ await upsertGlobalContentTypes(context);
17
+ };
18
+ export {
19
+ sliceUpdate
20
+ };
21
+ //# sourceMappingURL=slice-update.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slice-update.js","sources":["../../src/hooks/slice-update.ts"],"sourcesContent":["import type {\n\tSliceMachineContext,\n\tSliceUpdateHook,\n\tSliceUpdateHookData,\n} from \"@slicemachine/plugin-kit\";\nimport * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\n\nimport { pascalCase } from \"../lib/pascalCase\";\nimport { upsertGlobalContentTypes } from \"../lib/upsertGlobalContentTypes\";\n\nimport type { PluginOptions } from \"../types\";\n\ntype Args = {\n\tdir: string;\n\tdata: SliceUpdateHookData;\n} & SliceMachineContext<PluginOptions>;\n\nconst updateModelFile = async ({ dir, data, helpers, options }: Args) => {\n\tconst filePath = path.join(dir, \"model.json\");\n\n\tlet contents = JSON.stringify(data.model);\n\n\tif (options.format) {\n\t\tcontents = await helpers.format(contents, filePath);\n\t}\n\n\tawait fs.writeFile(filePath, contents);\n};\n\nexport const sliceUpdate: SliceUpdateHook<PluginOptions> = async (\n\tdata,\n\tcontext,\n) => {\n\tconst dir = context.helpers.joinPathFromRoot(\n\t\tdata.libraryID,\n\t\tpascalCase(data.model.id),\n\t);\n\n\tawait updateModelFile({ dir, data, ...context });\n\n\tawait upsertGlobalContentTypes(context);\n};\n"],"names":[],"mappings":";;;;AAkBA,MAAM,kBAAkB,OAAO,EAAE,KAAK,MAAM,SAAS,cAAmB;AACvE,QAAM,WAAW,KAAK,KAAK,KAAK,YAAY;AAE5C,MAAI,WAAW,KAAK,UAAU,KAAK,KAAK;AAExC,MAAI,QAAQ,QAAQ;AACnB,eAAW,MAAM,QAAQ,OAAO,UAAU,QAAQ;AAAA,EAClD;AAEK,QAAA,GAAG,UAAU,UAAU,QAAQ;AACtC;AAEa,MAAA,cAA8C,OAC1D,MACA,YACG;AACG,QAAA,MAAM,QAAQ,QAAQ,iBAC3B,KAAK,WACL,WAAW,KAAK,MAAM,EAAE,CAAC;AAG1B,QAAM,gBAAgB,EAAE,KAAK,MAAM,GAAG,QAAS,CAAA;AAE/C,QAAM,yBAAyB,OAAO;AACvC;"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const prismicT = require("@prismicio/types");
4
+ const fs = require("node:fs/promises");
5
+ const path = require("node:path");
6
+ const readJSONFile = require("../lib/readJSONFile.cjs");
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule)
9
+ return e;
10
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
11
+ if (e) {
12
+ for (const k in e) {
13
+ if (k !== "default") {
14
+ const d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: () => e[k]
18
+ });
19
+ }
20
+ }
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
25
+ const prismicT__namespace = /* @__PURE__ */ _interopNamespace(prismicT);
26
+ const fs__namespace = /* @__PURE__ */ _interopNamespace(fs);
27
+ const path__namespace = /* @__PURE__ */ _interopNamespace(path);
28
+ const isSharedSliceModel = (input) => {
29
+ return typeof input === "object" && input !== null && "type" in input && input.type === prismicT__namespace.CustomTypeModelSliceType.SharedSlice;
30
+ };
31
+ const sliceLibraryRead = async (data, { helpers }) => {
32
+ const dirPath = helpers.joinPathFromRoot(data.libraryID);
33
+ await fs__namespace.mkdir(dirPath, { recursive: true });
34
+ const childDirs = await fs__namespace.readdir(dirPath);
35
+ const sliceIDs = [];
36
+ await Promise.all(childDirs.map(async (childDir) => {
37
+ const modelPath = path__namespace.join(dirPath, childDir, "model.json");
38
+ try {
39
+ const modelContents = await readJSONFile.readJSONFile(modelPath);
40
+ if (isSharedSliceModel(modelContents)) {
41
+ sliceIDs.push(modelContents.id);
42
+ }
43
+ } catch {
44
+ }
45
+ }));
46
+ return {
47
+ id: data.libraryID,
48
+ sliceIDs: sliceIDs.sort()
49
+ };
50
+ };
51
+ exports.sliceLibraryRead = sliceLibraryRead;
52
+ //# sourceMappingURL=sliceLibrary-read.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sliceLibrary-read.cjs","sources":["../../src/hooks/sliceLibrary-read.ts"],"sourcesContent":["import type { SliceLibraryReadHook } from \"@slicemachine/plugin-kit\";\nimport * as prismicT from \"@prismicio/types\";\nimport * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\n\nimport { readJSONFile } from \"../lib/readJSONFile\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst isSharedSliceModel = (\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tinput: any,\n): input is prismicT.SharedSliceModel => {\n\treturn (\n\t\ttypeof input === \"object\" &&\n\t\tinput !== null &&\n\t\t\"type\" in input &&\n\t\tinput.type === prismicT.CustomTypeModelSliceType.SharedSlice\n\t);\n};\n\nexport const sliceLibraryRead: SliceLibraryReadHook<PluginOptions> = async (\n\tdata,\n\t{ helpers },\n) => {\n\tconst dirPath = helpers.joinPathFromRoot(data.libraryID);\n\n\t// Ensure the directory exists.\n\tawait fs.mkdir(dirPath, { recursive: true });\n\n\tconst childDirs = await fs.readdir(dirPath);\n\n\tconst sliceIDs: string[] = [];\n\tawait Promise.all(\n\t\tchildDirs.map(async (childDir) => {\n\t\t\tconst modelPath = path.join(dirPath, childDir, \"model.json\");\n\n\t\t\ttry {\n\t\t\t\tconst modelContents = await readJSONFile(modelPath);\n\n\t\t\t\tif (isSharedSliceModel(modelContents)) {\n\t\t\t\t\tsliceIDs.push(modelContents.id);\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// noop\n\t\t\t}\n\t\t}),\n\t);\n\n\treturn {\n\t\tid: data.libraryID,\n\t\tsliceIDs: sliceIDs.sort(),\n\t};\n};\n"],"names":["prismicT","fs","path","readJSONFile"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,MAAM,qBAAqB,CAE1B,UACuC;AAEtC,SAAA,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAASA,oBAAS,yBAAyB;AAEnD;AAEO,MAAM,mBAAwD,OACpE,MACA,EAAE,cACC;AACH,QAAM,UAAU,QAAQ,iBAAiB,KAAK,SAAS;AAGvD,QAAMC,cAAG,MAAM,SAAS,EAAE,WAAW,MAAM;AAE3C,QAAM,YAAY,MAAMA,cAAG,QAAQ,OAAO;AAE1C,QAAM,WAAqB,CAAA;AAC3B,QAAM,QAAQ,IACb,UAAU,IAAI,OAAO,aAAY;AAChC,UAAM,YAAYC,gBAAK,KAAK,SAAS,UAAU,YAAY;AAEvD,QAAA;AACG,YAAA,gBAAgB,MAAMC,0BAAa,SAAS;AAE9C,UAAA,mBAAmB,aAAa,GAAG;AAC7B,iBAAA,KAAK,cAAc,EAAE;AAAA,MAC9B;AAAA,IAAA,QACA;AAAA,IAED;AAAA,EACD,CAAA,CAAC;AAGI,SAAA;AAAA,IACN,IAAI,KAAK;AAAA,IACT,UAAU,SAAS,KAAM;AAAA,EAAA;AAE3B;;"}
@@ -0,0 +1,3 @@
1
+ import type { SliceLibraryReadHook } from "@slicemachine/plugin-kit";
2
+ import type { PluginOptions } from "../types";
3
+ export declare const sliceLibraryRead: SliceLibraryReadHook<PluginOptions>;
@@ -0,0 +1,31 @@
1
+ import * as prismicT from "@prismicio/types";
2
+ import * as fs from "node:fs/promises";
3
+ import * as path from "node:path";
4
+ import { readJSONFile } from "../lib/readJSONFile.js";
5
+ const isSharedSliceModel = (input) => {
6
+ return typeof input === "object" && input !== null && "type" in input && input.type === prismicT.CustomTypeModelSliceType.SharedSlice;
7
+ };
8
+ const sliceLibraryRead = async (data, { helpers }) => {
9
+ const dirPath = helpers.joinPathFromRoot(data.libraryID);
10
+ await fs.mkdir(dirPath, { recursive: true });
11
+ const childDirs = await fs.readdir(dirPath);
12
+ const sliceIDs = [];
13
+ await Promise.all(childDirs.map(async (childDir) => {
14
+ const modelPath = path.join(dirPath, childDir, "model.json");
15
+ try {
16
+ const modelContents = await readJSONFile(modelPath);
17
+ if (isSharedSliceModel(modelContents)) {
18
+ sliceIDs.push(modelContents.id);
19
+ }
20
+ } catch {
21
+ }
22
+ }));
23
+ return {
24
+ id: data.libraryID,
25
+ sliceIDs: sliceIDs.sort()
26
+ };
27
+ };
28
+ export {
29
+ sliceLibraryRead
30
+ };
31
+ //# sourceMappingURL=sliceLibrary-read.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sliceLibrary-read.js","sources":["../../src/hooks/sliceLibrary-read.ts"],"sourcesContent":["import type { SliceLibraryReadHook } from \"@slicemachine/plugin-kit\";\nimport * as prismicT from \"@prismicio/types\";\nimport * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\n\nimport { readJSONFile } from \"../lib/readJSONFile\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst isSharedSliceModel = (\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tinput: any,\n): input is prismicT.SharedSliceModel => {\n\treturn (\n\t\ttypeof input === \"object\" &&\n\t\tinput !== null &&\n\t\t\"type\" in input &&\n\t\tinput.type === prismicT.CustomTypeModelSliceType.SharedSlice\n\t);\n};\n\nexport const sliceLibraryRead: SliceLibraryReadHook<PluginOptions> = async (\n\tdata,\n\t{ helpers },\n) => {\n\tconst dirPath = helpers.joinPathFromRoot(data.libraryID);\n\n\t// Ensure the directory exists.\n\tawait fs.mkdir(dirPath, { recursive: true });\n\n\tconst childDirs = await fs.readdir(dirPath);\n\n\tconst sliceIDs: string[] = [];\n\tawait Promise.all(\n\t\tchildDirs.map(async (childDir) => {\n\t\t\tconst modelPath = path.join(dirPath, childDir, \"model.json\");\n\n\t\t\ttry {\n\t\t\t\tconst modelContents = await readJSONFile(modelPath);\n\n\t\t\t\tif (isSharedSliceModel(modelContents)) {\n\t\t\t\t\tsliceIDs.push(modelContents.id);\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// noop\n\t\t\t}\n\t\t}),\n\t);\n\n\treturn {\n\t\tid: data.libraryID,\n\t\tsliceIDs: sliceIDs.sort(),\n\t};\n};\n"],"names":[],"mappings":";;;;AASA,MAAM,qBAAqB,CAE1B,UACuC;AAEtC,SAAA,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAAS,SAAS,yBAAyB;AAEnD;AAEO,MAAM,mBAAwD,OACpE,MACA,EAAE,cACC;AACH,QAAM,UAAU,QAAQ,iBAAiB,KAAK,SAAS;AAGvD,QAAM,GAAG,MAAM,SAAS,EAAE,WAAW,MAAM;AAE3C,QAAM,YAAY,MAAM,GAAG,QAAQ,OAAO;AAE1C,QAAM,WAAqB,CAAA;AAC3B,QAAM,QAAQ,IACb,UAAU,IAAI,OAAO,aAAY;AAChC,UAAM,YAAY,KAAK,KAAK,SAAS,UAAU,YAAY;AAEvD,QAAA;AACG,YAAA,gBAAgB,MAAM,aAAa,SAAS;AAE9C,UAAA,mBAAmB,aAAa,GAAG;AAC7B,iBAAA,KAAK,cAAc,EAAE;AAAA,MAC9B;AAAA,IAAA,QACA;AAAA,IAED;AAAA,EACD,CAAA,CAAC;AAGI,SAAA;AAAA,IACN,IAAI,KAAK;AAAA,IACT,UAAU,SAAS,KAAM;AAAA,EAAA;AAE3B;"}
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const pluginKit = require("@slicemachine/plugin-kit");
4
+ const commonTags = require("common-tags");
5
+ const node_module = require("node:module");
6
+ const http = require("node:http");
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
+ const REQUIRED_DEPENDENCIES = [
28
+ "@prismicio/react",
29
+ "@prismicio/slice-simulator-react",
30
+ "@prismicio/client@latest",
31
+ "@prismicio/helpers"
32
+ ];
33
+ const createStep1 = async ({ project }) => {
34
+ const require2 = node_module.createRequire(project.root);
35
+ return {
36
+ title: "Install packages",
37
+ body: commonTags.stripIndent`
38
+ The simulator requires extra dependencies. Run the following command to install them.
39
+
40
+ ~~~sh
41
+ npm install --save @prismicio/react @prismicio/slice-simulator-react @prismicio/client@latest @prismicio/helpers
42
+ ~~~
43
+ `,
44
+ validate: async () => {
45
+ const missingDependencies = [];
46
+ for (const dependency of REQUIRED_DEPENDENCIES) {
47
+ try {
48
+ require2.resolve(dependency);
49
+ } catch {
50
+ missingDependencies.push(dependency);
51
+ }
52
+ }
53
+ if (missingDependencies.length >= REQUIRED_DEPENDENCIES.length) {
54
+ return {
55
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
56
+ title: "Missing all dependencies",
57
+ message: commonTags.stripIndent`
58
+ Install the required dependencies to continue.
59
+ `
60
+ };
61
+ }
62
+ if (missingDependencies.length > 0) {
63
+ const formattedMissingDependencies = missingDependencies.map((missingDependency) => `\`${missingDependency}\``).join(", ");
64
+ return {
65
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
66
+ title: "Missing some dependencies",
67
+ message: commonTags.stripIndent`
68
+ The following dependencies are missing: ${formattedMissingDependencies}
69
+ `
70
+ };
71
+ }
72
+ }
73
+ };
74
+ };
75
+ const createStep2 = async ({ helpers, options }) => {
76
+ const fileName = `slice-simulator.${getJSOrTSXFileExtension.getJSOrTSXFileExtension}`;
77
+ 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`
108
+ import { SliceSimulator } from "@prismicio/slice-simulator-react";
109
+ import { SliceZone } from "@prismicio/react";
110
+
111
+ import state from "../.slicemachine/libraries-state.json";
112
+ import { components } from "../slices";
113
+
114
+ const SliceSimulatorPage = () => {
115
+ return (
116
+ <SliceSimulator
117
+ sliceZone={(props) => <SliceZone {...props} components={components} />}
118
+ state={state}
119
+ />
120
+ );
121
+ };
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);
134
+ return {
135
+ title: "Create a page for the simulator",
136
+ body: commonTags.stripIndent`
137
+ 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
+
139
+ ~~~tsx
140
+ ${fileContents}
141
+ ~~~
142
+ `
143
+ };
144
+ };
145
+ const createStep3 = async ({ helpers }) => {
146
+ const filePath = helpers.joinPathFromRoot("sm.json");
147
+ const fileContents = await helpers.format(`
148
+ {
149
+ "localSliceSimulatorURL": "http://localhost:3000/slice-simulator"
150
+ }
151
+ `, filePath);
152
+ return {
153
+ title: "Update `sm.json`",
154
+ body: commonTags.stripIndent`
155
+ Update your \`sm.json\` file with a \`localSliceSimulatorURL\` property pointing to your \`slice-simulator\` page.
156
+
157
+ ~~~json
158
+ ${fileContents}
159
+ ~~~
160
+ `,
161
+ validate: async () => {
162
+ const project = await helpers.getProject();
163
+ if (!("localSliceSimulatorURL" in project.config)) {
164
+ return {
165
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
166
+ title: "Missing `localSliceSimulatorURL` property",
167
+ message: commonTags.stripIndent`
168
+ A \`localSliceSimulatorURL\` property was not found in your \`sm.json\` file.
169
+ `
170
+ };
171
+ }
172
+ try {
173
+ if (project.config.localSliceSimulatorURL) {
174
+ new URL(project.config.localSliceSimulatorURL);
175
+ } else {
176
+ throw new Error("Undefined Slice Simulator URL");
177
+ }
178
+ } catch {
179
+ return {
180
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
181
+ title: "An invalid URL was provided",
182
+ message: commonTags.stripIndent`
183
+ The \`localSliceSimulatorURL\` property should be of the shape \`http://localhost:PORT/PATH\`. See the codeblock for an example.
184
+ `
185
+ };
186
+ }
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) {
198
+ return {
199
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
200
+ title: "Unable to connect to simulator page",
201
+ message: commonTags.stripIndent`
202
+ 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
+ `
204
+ };
205
+ }
206
+ }
207
+ };
208
+ };
209
+ const sliceSimulatorSetupRead = async (_data, context) => {
210
+ return Promise.all([
211
+ createStep1(context),
212
+ createStep2(context),
213
+ createStep3(context)
214
+ ]);
215
+ };
216
+ exports.sliceSimulatorSetupRead = sliceSimulatorSetupRead;
217
+ //# sourceMappingURL=sliceSimulator-setup-read.cjs.map
@@ -0,0 +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":"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"}
@@ -0,0 +1,3 @@
1
+ import type { SliceSimulatorSetupReadHook } from "@slicemachine/plugin-kit";
2
+ import type { PluginOptions } from "../types";
3
+ export declare const sliceSimulatorSetupRead: SliceSimulatorSetupReadHook<PluginOptions>;
@@ -0,0 +1,198 @@
1
+ import { SliceSimulatorSetupStepValidationMessageType } from "@slicemachine/plugin-kit";
2
+ import { stripIndent } from "common-tags";
3
+ import { createRequire } from "node:module";
4
+ import * as http from "node:http";
5
+ import { getJSOrTSXFileExtension } from "../lib/getJSOrTSXFileExtension.js";
6
+ const REQUIRED_DEPENDENCIES = [
7
+ "@prismicio/react",
8
+ "@prismicio/slice-simulator-react",
9
+ "@prismicio/client@latest",
10
+ "@prismicio/helpers"
11
+ ];
12
+ const createStep1 = async ({ project }) => {
13
+ const require2 = createRequire(project.root);
14
+ return {
15
+ title: "Install packages",
16
+ body: stripIndent`
17
+ The simulator requires extra dependencies. Run the following command to install them.
18
+
19
+ ~~~sh
20
+ npm install --save @prismicio/react @prismicio/slice-simulator-react @prismicio/client@latest @prismicio/helpers
21
+ ~~~
22
+ `,
23
+ validate: async () => {
24
+ const missingDependencies = [];
25
+ for (const dependency of REQUIRED_DEPENDENCIES) {
26
+ try {
27
+ require2.resolve(dependency);
28
+ } catch {
29
+ missingDependencies.push(dependency);
30
+ }
31
+ }
32
+ if (missingDependencies.length >= REQUIRED_DEPENDENCIES.length) {
33
+ return {
34
+ type: SliceSimulatorSetupStepValidationMessageType.Error,
35
+ title: "Missing all dependencies",
36
+ message: stripIndent`
37
+ Install the required dependencies to continue.
38
+ `
39
+ };
40
+ }
41
+ if (missingDependencies.length > 0) {
42
+ const formattedMissingDependencies = missingDependencies.map((missingDependency) => `\`${missingDependency}\``).join(", ");
43
+ return {
44
+ type: SliceSimulatorSetupStepValidationMessageType.Warning,
45
+ title: "Missing some dependencies",
46
+ message: stripIndent`
47
+ The following dependencies are missing: ${formattedMissingDependencies}
48
+ `
49
+ };
50
+ }
51
+ }
52
+ };
53
+ };
54
+ const createStep2 = async ({ helpers, options }) => {
55
+ const fileName = `slice-simulator.${getJSOrTSXFileExtension}`;
56
+ 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`
87
+ import { SliceSimulator } from "@prismicio/slice-simulator-react";
88
+ import { SliceZone } from "@prismicio/react";
89
+
90
+ import state from "../.slicemachine/libraries-state.json";
91
+ import { components } from "../slices";
92
+
93
+ const SliceSimulatorPage = () => {
94
+ return (
95
+ <SliceSimulator
96
+ sliceZone={(props) => <SliceZone {...props} components={components} />}
97
+ state={state}
98
+ />
99
+ );
100
+ };
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);
113
+ return {
114
+ title: "Create a page for the simulator",
115
+ body: stripIndent`
116
+ 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
+
118
+ ~~~tsx
119
+ ${fileContents}
120
+ ~~~
121
+ `
122
+ };
123
+ };
124
+ const createStep3 = async ({ helpers }) => {
125
+ const filePath = helpers.joinPathFromRoot("sm.json");
126
+ const fileContents = await helpers.format(`
127
+ {
128
+ "localSliceSimulatorURL": "http://localhost:3000/slice-simulator"
129
+ }
130
+ `, filePath);
131
+ return {
132
+ title: "Update `sm.json`",
133
+ body: stripIndent`
134
+ Update your \`sm.json\` file with a \`localSliceSimulatorURL\` property pointing to your \`slice-simulator\` page.
135
+
136
+ ~~~json
137
+ ${fileContents}
138
+ ~~~
139
+ `,
140
+ validate: async () => {
141
+ const project = await helpers.getProject();
142
+ if (!("localSliceSimulatorURL" in project.config)) {
143
+ return {
144
+ type: SliceSimulatorSetupStepValidationMessageType.Error,
145
+ title: "Missing `localSliceSimulatorURL` property",
146
+ message: stripIndent`
147
+ A \`localSliceSimulatorURL\` property was not found in your \`sm.json\` file.
148
+ `
149
+ };
150
+ }
151
+ try {
152
+ if (project.config.localSliceSimulatorURL) {
153
+ new URL(project.config.localSliceSimulatorURL);
154
+ } else {
155
+ throw new Error("Undefined Slice Simulator URL");
156
+ }
157
+ } catch {
158
+ return {
159
+ type: SliceSimulatorSetupStepValidationMessageType.Warning,
160
+ title: "An invalid URL was provided",
161
+ message: stripIndent`
162
+ The \`localSliceSimulatorURL\` property should be of the shape \`http://localhost:PORT/PATH\`. See the codeblock for an example.
163
+ `
164
+ };
165
+ }
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) {
177
+ return {
178
+ type: SliceSimulatorSetupStepValidationMessageType.Warning,
179
+ title: "Unable to connect to simulator page",
180
+ message: stripIndent`
181
+ 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
+ `
183
+ };
184
+ }
185
+ }
186
+ };
187
+ };
188
+ const sliceSimulatorSetupRead = async (_data, context) => {
189
+ return Promise.all([
190
+ createStep1(context),
191
+ createStep2(context),
192
+ createStep3(context)
193
+ ]);
194
+ };
195
+ export {
196
+ sliceSimulatorSetupRead
197
+ };
198
+ //# sourceMappingURL=sliceSimulator-setup-read.js.map