@slicemachine/adapter-next 0.3.10 → 0.3.11-dev-next-release.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/dist/hooks/documentation-read.cjs +7 -6
  2. package/dist/hooks/documentation-read.cjs.map +1 -1
  3. package/dist/hooks/documentation-read.js +7 -6
  4. package/dist/hooks/documentation-read.js.map +1 -1
  5. package/dist/hooks/project-init.cjs +51 -58
  6. package/dist/hooks/project-init.cjs.map +1 -1
  7. package/dist/hooks/project-init.js +51 -57
  8. package/dist/hooks/project-init.js.map +1 -1
  9. package/dist/hooks/slice-create.cjs +29 -44
  10. package/dist/hooks/slice-create.cjs.map +1 -1
  11. package/dist/hooks/slice-create.js +29 -26
  12. package/dist/hooks/slice-create.js.map +1 -1
  13. package/dist/lib/checkHasAppRouter.cjs +23 -2
  14. package/dist/lib/checkHasAppRouter.cjs.map +1 -1
  15. package/dist/lib/checkHasAppRouter.js +6 -2
  16. package/dist/lib/checkHasAppRouter.js.map +1 -1
  17. package/dist/lib/checkHasSrcDirectory.cjs +5 -2
  18. package/dist/lib/checkHasSrcDirectory.cjs.map +1 -1
  19. package/dist/lib/checkHasSrcDirectory.js +5 -2
  20. package/dist/lib/checkHasSrcDirectory.js.map +1 -1
  21. package/dist/lib/checkIsTypeScriptProject.cjs +2 -3
  22. package/dist/lib/checkIsTypeScriptProject.cjs.map +1 -1
  23. package/dist/lib/checkIsTypeScriptProject.js +2 -3
  24. package/dist/lib/checkIsTypeScriptProject.js.map +1 -1
  25. package/dist/lib/upsertSliceLibraryIndexFile.cjs +29 -21
  26. package/dist/lib/upsertSliceLibraryIndexFile.cjs.map +1 -1
  27. package/dist/lib/upsertSliceLibraryIndexFile.js +29 -20
  28. package/dist/lib/upsertSliceLibraryIndexFile.js.map +1 -1
  29. package/dist/plugin.cjs +178 -36
  30. package/dist/plugin.cjs.map +1 -1
  31. package/dist/plugin.js +178 -36
  32. package/dist/plugin.js.map +1 -1
  33. package/package.json +5 -7
  34. package/src/hooks/documentation-read.ts +9 -7
  35. package/src/hooks/project-init.ts +58 -69
  36. package/src/hooks/slice-create.ts +39 -31
  37. package/src/lib/checkHasAppRouter.ts +6 -6
  38. package/src/lib/checkHasSrcDirectory.ts +5 -2
  39. package/src/lib/checkIsTypeScriptProject.ts +5 -4
  40. package/src/lib/upsertSliceLibraryIndexFile.ts +53 -33
  41. package/src/plugin.ts +245 -37
  42. package/dist/hooks/customType-asset-delete.cjs +0 -31
  43. package/dist/hooks/customType-asset-delete.cjs.map +0 -1
  44. package/dist/hooks/customType-asset-delete.d.ts +0 -3
  45. package/dist/hooks/customType-asset-delete.js +0 -14
  46. package/dist/hooks/customType-asset-delete.js.map +0 -1
  47. package/dist/hooks/customType-asset-read.cjs +0 -34
  48. package/dist/hooks/customType-asset-read.cjs.map +0 -1
  49. package/dist/hooks/customType-asset-read.d.ts +0 -3
  50. package/dist/hooks/customType-asset-read.js +0 -17
  51. package/dist/hooks/customType-asset-read.js.map +0 -1
  52. package/dist/hooks/customType-asset-update.cjs +0 -34
  53. package/dist/hooks/customType-asset-update.cjs.map +0 -1
  54. package/dist/hooks/customType-asset-update.d.ts +0 -3
  55. package/dist/hooks/customType-asset-update.js +0 -16
  56. package/dist/hooks/customType-asset-update.js.map +0 -1
  57. package/dist/hooks/customType-create.cjs +0 -43
  58. package/dist/hooks/customType-create.cjs.map +0 -1
  59. package/dist/hooks/customType-create.d.ts +0 -3
  60. package/dist/hooks/customType-create.js +0 -25
  61. package/dist/hooks/customType-create.js.map +0 -1
  62. package/dist/hooks/customType-delete.cjs +0 -32
  63. package/dist/hooks/customType-delete.cjs.map +0 -1
  64. package/dist/hooks/customType-delete.d.ts +0 -3
  65. package/dist/hooks/customType-delete.js +0 -15
  66. package/dist/hooks/customType-delete.js.map +0 -1
  67. package/dist/hooks/customType-read.cjs +0 -31
  68. package/dist/hooks/customType-read.cjs.map +0 -1
  69. package/dist/hooks/customType-read.d.ts +0 -3
  70. package/dist/hooks/customType-read.js +0 -14
  71. package/dist/hooks/customType-read.js.map +0 -1
  72. package/dist/hooks/customType-rename.cjs +0 -6
  73. package/dist/hooks/customType-rename.cjs.map +0 -1
  74. package/dist/hooks/customType-rename.d.ts +0 -1
  75. package/dist/hooks/customType-rename.js +0 -6
  76. package/dist/hooks/customType-rename.js.map +0 -1
  77. package/dist/hooks/customType-update.cjs +0 -6
  78. package/dist/hooks/customType-update.cjs.map +0 -1
  79. package/dist/hooks/customType-update.d.ts +0 -1
  80. package/dist/hooks/customType-update.js +0 -6
  81. package/dist/hooks/customType-update.js.map +0 -1
  82. package/dist/hooks/customTypeLibrary-read.cjs +0 -55
  83. package/dist/hooks/customTypeLibrary-read.cjs.map +0 -1
  84. package/dist/hooks/customTypeLibrary-read.d.ts +0 -3
  85. package/dist/hooks/customTypeLibrary-read.js +0 -37
  86. package/dist/hooks/customTypeLibrary-read.js.map +0 -1
  87. package/dist/hooks/slice-asset-delete.cjs +0 -33
  88. package/dist/hooks/slice-asset-delete.cjs.map +0 -1
  89. package/dist/hooks/slice-asset-delete.d.ts +0 -3
  90. package/dist/hooks/slice-asset-delete.js +0 -16
  91. package/dist/hooks/slice-asset-delete.js.map +0 -1
  92. package/dist/hooks/slice-asset-read.cjs +0 -36
  93. package/dist/hooks/slice-asset-read.cjs.map +0 -1
  94. package/dist/hooks/slice-asset-read.d.ts +0 -3
  95. package/dist/hooks/slice-asset-read.js +0 -19
  96. package/dist/hooks/slice-asset-read.js.map +0 -1
  97. package/dist/hooks/slice-asset-update.cjs +0 -36
  98. package/dist/hooks/slice-asset-update.cjs.map +0 -1
  99. package/dist/hooks/slice-asset-update.d.ts +0 -3
  100. package/dist/hooks/slice-asset-update.js +0 -18
  101. package/dist/hooks/slice-asset-update.js.map +0 -1
  102. package/dist/hooks/slice-delete.cjs +0 -41
  103. package/dist/hooks/slice-delete.cjs.map +0 -1
  104. package/dist/hooks/slice-delete.d.ts +0 -3
  105. package/dist/hooks/slice-delete.js +0 -24
  106. package/dist/hooks/slice-delete.js.map +0 -1
  107. package/dist/hooks/slice-read.cjs +0 -62
  108. package/dist/hooks/slice-read.cjs.map +0 -1
  109. package/dist/hooks/slice-read.d.ts +0 -3
  110. package/dist/hooks/slice-read.js +0 -44
  111. package/dist/hooks/slice-read.js.map +0 -1
  112. package/dist/hooks/slice-rename.cjs +0 -51
  113. package/dist/hooks/slice-rename.cjs.map +0 -1
  114. package/dist/hooks/slice-rename.d.ts +0 -3
  115. package/dist/hooks/slice-rename.js +0 -34
  116. package/dist/hooks/slice-rename.js.map +0 -1
  117. package/dist/hooks/slice-update.cjs +0 -14
  118. package/dist/hooks/slice-update.cjs.map +0 -1
  119. package/dist/hooks/slice-update.d.ts +0 -3
  120. package/dist/hooks/slice-update.js +0 -14
  121. package/dist/hooks/slice-update.js.map +0 -1
  122. package/dist/hooks/sliceLibrary-read.cjs +0 -52
  123. package/dist/hooks/sliceLibrary-read.cjs.map +0 -1
  124. package/dist/hooks/sliceLibrary-read.d.ts +0 -3
  125. package/dist/hooks/sliceLibrary-read.js +0 -34
  126. package/dist/hooks/sliceLibrary-read.js.map +0 -1
  127. package/dist/lib/buildCustomTypeAssetPath.cjs +0 -29
  128. package/dist/lib/buildCustomTypeAssetPath.cjs.map +0 -1
  129. package/dist/lib/buildCustomTypeAssetPath.d.ts +0 -9
  130. package/dist/lib/buildCustomTypeAssetPath.js +0 -12
  131. package/dist/lib/buildCustomTypeAssetPath.js.map +0 -1
  132. package/dist/lib/buildCustomTypeDirectoryPath.cjs +0 -26
  133. package/dist/lib/buildCustomTypeDirectoryPath.cjs.map +0 -1
  134. package/dist/lib/buildCustomTypeDirectoryPath.d.ts +0 -8
  135. package/dist/lib/buildCustomTypeDirectoryPath.js +0 -9
  136. package/dist/lib/buildCustomTypeDirectoryPath.js.map +0 -1
  137. package/dist/lib/buildCustomTypeLibraryDirectoryPath.cjs +0 -7
  138. package/dist/lib/buildCustomTypeLibraryDirectoryPath.cjs.map +0 -1
  139. package/dist/lib/buildCustomTypeLibraryDirectoryPath.d.ts +0 -7
  140. package/dist/lib/buildCustomTypeLibraryDirectoryPath.js +0 -7
  141. package/dist/lib/buildCustomTypeLibraryDirectoryPath.js.map +0 -1
  142. package/dist/lib/buildSliceAssetPath.cjs +0 -34
  143. package/dist/lib/buildSliceAssetPath.cjs.map +0 -1
  144. package/dist/lib/buildSliceAssetPath.d.ts +0 -11
  145. package/dist/lib/buildSliceAssetPath.js +0 -17
  146. package/dist/lib/buildSliceAssetPath.js.map +0 -1
  147. package/dist/lib/buildSliceDirectoryPath.cjs +0 -30
  148. package/dist/lib/buildSliceDirectoryPath.cjs.map +0 -1
  149. package/dist/lib/buildSliceDirectoryPath.d.ts +0 -10
  150. package/dist/lib/buildSliceDirectoryPath.js +0 -13
  151. package/dist/lib/buildSliceDirectoryPath.js.map +0 -1
  152. package/dist/lib/buildSliceLibraryDirectoryPath.cjs +0 -7
  153. package/dist/lib/buildSliceLibraryDirectoryPath.cjs.map +0 -1
  154. package/dist/lib/buildSliceLibraryDirectoryPath.d.ts +0 -8
  155. package/dist/lib/buildSliceLibraryDirectoryPath.js +0 -7
  156. package/dist/lib/buildSliceLibraryDirectoryPath.js.map +0 -1
  157. package/dist/lib/checkPathExists.cjs +0 -30
  158. package/dist/lib/checkPathExists.cjs.map +0 -1
  159. package/dist/lib/checkPathExists.d.ts +0 -2
  160. package/dist/lib/checkPathExists.js +0 -13
  161. package/dist/lib/checkPathExists.js.map +0 -1
  162. package/dist/lib/isSharedSliceModel.cjs +0 -7
  163. package/dist/lib/isSharedSliceModel.cjs.map +0 -1
  164. package/dist/lib/isSharedSliceModel.d.ts +0 -335
  165. package/dist/lib/isSharedSliceModel.js +0 -7
  166. package/dist/lib/isSharedSliceModel.js.map +0 -1
  167. package/dist/lib/readJSONFile.cjs +0 -26
  168. package/dist/lib/readJSONFile.cjs.map +0 -1
  169. package/dist/lib/readJSONFile.d.ts +0 -1
  170. package/dist/lib/readJSONFile.js +0 -9
  171. package/dist/lib/readJSONFile.js.map +0 -1
  172. package/dist/lib/updateSliceModelFile.cjs +0 -33
  173. package/dist/lib/updateSliceModelFile.cjs.map +0 -1
  174. package/dist/lib/updateSliceModelFile.d.ts +0 -9
  175. package/dist/lib/updateSliceModelFile.js +0 -15
  176. package/dist/lib/updateSliceModelFile.js.map +0 -1
  177. package/dist/lib/upsertGlobalContentTypes.cjs +0 -56
  178. package/dist/lib/upsertGlobalContentTypes.cjs.map +0 -1
  179. package/dist/lib/upsertGlobalContentTypes.d.ts +0 -12
  180. package/dist/lib/upsertGlobalContentTypes.js +0 -38
  181. package/dist/lib/upsertGlobalContentTypes.js.map +0 -1
  182. package/src/hooks/customType-asset-delete.ts +0 -18
  183. package/src/hooks/customType-asset-read.ts +0 -22
  184. package/src/hooks/customType-asset-update.ts +0 -20
  185. package/src/hooks/customType-create.ts +0 -45
  186. package/src/hooks/customType-delete.ts +0 -21
  187. package/src/hooks/customType-read.ts +0 -24
  188. package/src/hooks/customType-rename.ts +0 -3
  189. package/src/hooks/customType-update.ts +0 -3
  190. package/src/hooks/customTypeLibrary-read.ts +0 -58
  191. package/src/hooks/slice-asset-delete.ts +0 -21
  192. package/src/hooks/slice-asset-read.ts +0 -25
  193. package/src/hooks/slice-asset-update.ts +0 -23
  194. package/src/hooks/slice-delete.ts +0 -41
  195. package/src/hooks/slice-read.ts +0 -69
  196. package/src/hooks/slice-rename.ts +0 -46
  197. package/src/hooks/slice-update.ts +0 -19
  198. package/src/hooks/sliceLibrary-read.ts +0 -48
  199. package/src/lib/buildCustomTypeAssetPath.ts +0 -24
  200. package/src/lib/buildCustomTypeDirectoryPath.ts +0 -20
  201. package/src/lib/buildCustomTypeLibraryDirectoryPath.ts +0 -13
  202. package/src/lib/buildSliceAssetPath.ts +0 -32
  203. package/src/lib/buildSliceDirectoryPath.ts +0 -26
  204. package/src/lib/buildSliceLibraryDirectoryPath.ts +0 -14
  205. package/src/lib/checkPathExists.ts +0 -12
  206. package/src/lib/isSharedSliceModel.ts +0 -14
  207. package/src/lib/readJSONFile.ts +0 -7
  208. package/src/lib/updateSliceModelFile.ts +0 -33
  209. package/src/lib/upsertGlobalContentTypes.ts +0 -63
@@ -1,16 +1,18 @@
1
- import * as fs from "node:fs/promises";
2
1
  import * as path from "node:path";
3
2
  import type {
4
3
  ProjectInitHook,
5
4
  ProjectInitHookData,
6
5
  SliceMachineContext,
7
6
  } from "@slicemachine/plugin-kit";
7
+ import {
8
+ checkHasProjectFile,
9
+ writeProjectFile,
10
+ } from "@slicemachine/plugin-kit/fs";
8
11
  import { source } from "common-tags";
9
12
 
10
13
  import { checkHasAppRouter } from "../lib/checkHasAppRouter";
11
14
  import { checkHasSrcDirectory } from "../lib/checkHasSrcDirectory";
12
15
  import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
13
- import { checkPathExists } from "../lib/checkPathExists";
14
16
  import { getJSFileExtension } from "../lib/getJSFileExtension";
15
17
  import { rejectIfNecessary } from "../lib/rejectIfNecessary";
16
18
 
@@ -46,12 +48,13 @@ const createPrismicIOFile = async ({
46
48
  const hasAppRouter = await checkHasAppRouter({ helpers });
47
49
 
48
50
  const extension = await getJSFileExtension({ helpers, options });
49
- const filename = `prismicio.${extension}`;
50
- const filePath = hasSrcDirectory
51
- ? helpers.joinPathFromRoot("src", filename)
52
- : helpers.joinPathFromRoot(filename);
51
+ const filename = path.join(
52
+ ...[hasSrcDirectory ? "src" : undefined, `prismicio.${extension}`].filter(
53
+ (segment): segment is NonNullable<typeof segment> => Boolean(segment),
54
+ ),
55
+ );
53
56
 
54
- if (await checkPathExists(filePath)) {
57
+ if (await checkHasProjectFile({ filename, helpers })) {
55
58
  return;
56
59
  }
57
60
 
@@ -229,11 +232,12 @@ const createPrismicIOFile = async ({
229
232
  `;
230
233
  }
231
234
 
232
- if (options.format) {
233
- contents = await helpers.format(contents, filePath);
234
- }
235
-
236
- await fs.writeFile(filePath, contents);
235
+ await writeProjectFile({
236
+ filename,
237
+ contents,
238
+ format: options.format,
239
+ helpers,
240
+ });
237
241
  };
238
242
 
239
243
  type CreateSliceSimulatorPageArgs = SliceMachineContext<PluginOptions>;
@@ -246,7 +250,7 @@ const createSliceSimulatorPage = async ({
246
250
  const hasAppRouter = await checkHasAppRouter({ helpers });
247
251
 
248
252
  const extension = await getJSFileExtension({ helpers, options, jsx: true });
249
- const filePath = helpers.joinPathFromRoot(
253
+ const filename = path.join(
250
254
  ...[
251
255
  hasSrcDirectory ? "src" : undefined,
252
256
  hasAppRouter
@@ -257,13 +261,11 @@ const createSliceSimulatorPage = async ({
257
261
  ),
258
262
  );
259
263
 
260
- if (await checkPathExists(filePath)) {
264
+ if (await checkHasProjectFile({ filename, helpers })) {
261
265
  return;
262
266
  }
263
267
 
264
- await fs.mkdir(path.dirname(filePath), { recursive: true });
265
-
266
- let contents = source`
268
+ const contents = source`
267
269
  "use client"
268
270
 
269
271
  import { SliceSimulator } from "@slicemachine/adapter-next/simulator";
@@ -280,11 +282,12 @@ const createSliceSimulatorPage = async ({
280
282
  }
281
283
  `;
282
284
 
283
- if (options.format) {
284
- contents = await helpers.format(contents, filePath);
285
- }
286
-
287
- await fs.writeFile(filePath, contents);
285
+ await writeProjectFile({
286
+ filename,
287
+ contents,
288
+ format: options.format,
289
+ helpers,
290
+ });
288
291
  };
289
292
 
290
293
  const createPreviewRoute = async ({
@@ -299,7 +302,7 @@ const createPreviewRoute = async ({
299
302
  });
300
303
 
301
304
  const extension = await getJSFileExtension({ helpers, options });
302
- const filePath = helpers.joinPathFromRoot(
305
+ const filename = path.join(
303
306
  ...[
304
307
  hasSrcDirectory ? "src" : undefined,
305
308
  hasAppRouter
@@ -310,7 +313,7 @@ const createPreviewRoute = async ({
310
313
  ),
311
314
  );
312
315
 
313
- if (await checkPathExists(filePath)) {
316
+ if (await checkHasProjectFile({ filename, helpers })) {
314
317
  return;
315
318
  }
316
319
 
@@ -320,7 +323,6 @@ const createPreviewRoute = async ({
320
323
  if (isTypeScriptProject) {
321
324
  contents = source`
322
325
  import { NextRequest } from "next/server";
323
- import { draftMode } from "next/headers";
324
326
  import { redirectToPreviewURL } from "@prismicio/next";
325
327
 
326
328
  import { createClient } from "../../../prismicio";
@@ -328,14 +330,11 @@ const createPreviewRoute = async ({
328
330
  export async function GET(request: NextRequest) {
329
331
  const client = createClient();
330
332
 
331
- draftMode().enable();
332
-
333
333
  await redirectToPreviewURL({ client, request });
334
334
  }
335
335
  `;
336
336
  } else {
337
337
  contents = source`
338
- import { draftMode } from "next/headers";
339
338
  import { redirectToPreviewURL } from "@prismicio/next";
340
339
 
341
340
  import { createClient } from "../../../prismicio";
@@ -343,8 +342,6 @@ const createPreviewRoute = async ({
343
342
  export async function GET(request) {
344
343
  const client = createClient();
345
344
 
346
- draftMode().enable();
347
-
348
345
  await redirectToPreviewURL({ client, request });
349
346
  }
350
347
  `;
@@ -382,12 +379,12 @@ const createPreviewRoute = async ({
382
379
  }
383
380
  }
384
381
 
385
- if (options.format) {
386
- contents = await helpers.format(contents, filePath);
387
- }
388
-
389
- await fs.mkdir(path.dirname(filePath), { recursive: true });
390
- await fs.writeFile(filePath, contents);
382
+ await writeProjectFile({
383
+ filename,
384
+ contents,
385
+ format: options.format,
386
+ helpers,
387
+ });
391
388
  };
392
389
 
393
390
  const createExitPreviewRoute = async ({
@@ -402,7 +399,7 @@ const createExitPreviewRoute = async ({
402
399
  });
403
400
 
404
401
  const extension = await getJSFileExtension({ helpers, options });
405
- const filePath = helpers.joinPathFromRoot(
402
+ const filename = path.join(
406
403
  ...[
407
404
  hasSrcDirectory ? "src" : undefined,
408
405
  hasAppRouter
@@ -413,14 +410,14 @@ const createExitPreviewRoute = async ({
413
410
  ),
414
411
  );
415
412
 
416
- if (await checkPathExists(filePath)) {
413
+ if (await checkHasProjectFile({ filename, helpers })) {
417
414
  return;
418
415
  }
419
416
 
420
417
  let contents: string;
421
418
 
422
419
  if (hasAppRouter) {
423
- contents = `
420
+ contents = source`
424
421
  import { exitPreview } from "@prismicio/next";
425
422
 
426
423
  export async function GET() {
@@ -448,17 +445,18 @@ const createExitPreviewRoute = async ({
448
445
  }
449
446
  }
450
447
 
451
- if (options.format) {
452
- contents = await helpers.format(contents, filePath);
453
- }
454
-
455
- await fs.mkdir(path.dirname(filePath), { recursive: true });
456
- await fs.writeFile(filePath, contents);
448
+ await writeProjectFile({
449
+ filename,
450
+ contents,
451
+ format: options.format,
452
+ helpers,
453
+ });
457
454
  };
458
455
 
459
456
  const modifySliceMachineConfig = async ({
460
457
  helpers,
461
458
  options,
459
+ actions,
462
460
  }: SliceMachineContext<PluginOptions>) => {
463
461
  const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
464
462
  const project = await helpers.getProject();
@@ -471,24 +469,15 @@ const modifySliceMachineConfig = async ({
471
469
  // is empty.
472
470
  if (
473
471
  hasSrcDirectory &&
472
+ project.config.libraries &&
474
473
  JSON.stringify(project.config.libraries) === JSON.stringify(["./slices"])
475
474
  ) {
476
- try {
477
- const entries = await fs.readdir(helpers.joinPathFromRoot("slices"));
475
+ const sliceLibrary = await actions.readSliceLibrary({
476
+ libraryID: project.config.libraries[0],
477
+ });
478
478
 
479
- if (!entries.map((entry) => path.parse(entry).name).includes("index")) {
480
- project.config.libraries = ["./src/slices"];
481
- }
482
- } catch (error) {
483
- if (
484
- error instanceof Error &&
485
- "code" in error &&
486
- error.code === "ENOENT"
487
- ) {
488
- // The directory does not exist, which means we
489
- // can safely nest the library.
490
- project.config.libraries = ["./src/slices"];
491
- }
479
+ if (sliceLibrary.sliceIDs.length < 1) {
480
+ project.config.libraries = ["./src/slices"];
492
481
  }
493
482
  }
494
483
 
@@ -510,7 +499,7 @@ const createRevalidateRoute = async ({
510
499
  const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
511
500
 
512
501
  const extension = await getJSFileExtension({ helpers, options });
513
- const filePath = helpers.joinPathFromRoot(
502
+ const filename = path.join(
514
503
  ...[
515
504
  hasSrcDirectory ? "src" : undefined,
516
505
  `app/api/revalidate/route.${extension}`,
@@ -519,11 +508,11 @@ const createRevalidateRoute = async ({
519
508
  ),
520
509
  );
521
510
 
522
- if (await checkPathExists(filePath)) {
511
+ if (await checkHasProjectFile({ filename, helpers })) {
523
512
  return;
524
513
  }
525
514
 
526
- let contents = source`
515
+ const contents = source`
527
516
  import { NextResponse } from "next/server";
528
517
  import { revalidateTag } from "next/cache";
529
518
 
@@ -534,12 +523,12 @@ const createRevalidateRoute = async ({
534
523
  }
535
524
  `;
536
525
 
537
- if (options.format) {
538
- contents = await helpers.format(contents, filePath);
539
- }
540
-
541
- await fs.mkdir(path.dirname(filePath), { recursive: true });
542
- await fs.writeFile(filePath, contents);
526
+ await writeProjectFile({
527
+ filename,
528
+ contents,
529
+ format: options.format,
530
+ helpers,
531
+ });
543
532
  };
544
533
 
545
534
  export const projectInit: ProjectInitHook<PluginOptions> = async (
@@ -3,31 +3,34 @@ import type {
3
3
  SliceCreateHookData,
4
4
  SliceMachineContext,
5
5
  } from "@slicemachine/plugin-kit";
6
+ import {
7
+ upsertGlobalTypeScriptTypes,
8
+ writeSliceFile,
9
+ writeSliceModel,
10
+ } from "@slicemachine/plugin-kit/fs";
6
11
  import { stripIndent } from "common-tags";
7
- import * as fs from "node:fs/promises";
8
- import * as path from "node:path";
9
12
 
10
- import { buildSliceDirectoryPath } from "../lib/buildSliceDirectoryPath";
11
13
  import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
12
14
  import { getJSFileExtension } from "../lib/getJSFileExtension";
13
15
  import { pascalCase } from "../lib/pascalCase";
14
16
  import { rejectIfNecessary } from "../lib/rejectIfNecessary";
15
- import { updateSliceModelFile } from "../lib/updateSliceModelFile";
16
- import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
17
17
  import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
18
18
 
19
19
  import type { PluginOptions } from "../types";
20
20
 
21
21
  type Args = {
22
- dir: string;
23
22
  data: SliceCreateHookData;
24
23
  } & SliceMachineContext<PluginOptions>;
25
24
 
26
- const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
25
+ const createComponentFile = async ({
26
+ data,
27
+ helpers,
28
+ actions,
29
+ options,
30
+ }: Args) => {
27
31
  const extension = await getJSFileExtension({ helpers, options, jsx: true });
28
- const filePath = path.join(dir, `index.${extension}`);
29
- const model = data.model;
30
- const pascalName = pascalCase(model.name);
32
+ const filename = `index.${extension}`;
33
+ const pascalName = pascalCase(data.model.name);
31
34
 
32
35
  let contents: string;
33
36
 
@@ -47,7 +50,7 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
47
50
  export type ${pascalName}Props = SliceComponentProps<Content.${pascalName}Slice>;
48
51
 
49
52
  /**
50
- * Component for "${model.name}" Slices.
53
+ * Component for "${data.model.name}" Slices.
51
54
  */
52
55
  const ${pascalName} = ({ slice }: ${pascalName}Props): JSX.Element => {
53
56
  return (
@@ -55,7 +58,7 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
55
58
  data-slice-type={slice.slice_type}
56
59
  data-slice-variation={slice.variation}
57
60
  >
58
- Placeholder component for ${model.id} (variation: {slice.variation}) Slices
61
+ Placeholder component for ${data.model.id} (variation: {slice.variation}) Slices
59
62
  </section>
60
63
  );
61
64
  };
@@ -75,7 +78,7 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
75
78
  data-slice-type={slice.slice_type}
76
79
  data-slice-variation={slice.variation}
77
80
  >
78
- Placeholder component for ${model.id} (variation: {slice.variation}) Slices
81
+ Placeholder component for ${data.model.id} (variation: {slice.variation}) Slices
79
82
  </section>
80
83
  );
81
84
  };
@@ -84,40 +87,45 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
84
87
  `;
85
88
  }
86
89
 
87
- if (options.format) {
88
- contents = await helpers.format(contents, filePath);
89
- }
90
-
91
- await fs.writeFile(filePath, contents);
90
+ await writeSliceFile({
91
+ libraryID: data.libraryID,
92
+ model: data.model,
93
+ filename,
94
+ contents,
95
+ format: options.format,
96
+ actions,
97
+ helpers,
98
+ });
92
99
  };
93
100
 
94
101
  export const sliceCreate: SliceCreateHook<PluginOptions> = async (
95
102
  data,
96
103
  context,
97
104
  ) => {
98
- const dir = buildSliceDirectoryPath({
99
- libraryID: data.libraryID,
100
- model: data.model,
101
- helpers: context.helpers,
102
- });
103
-
104
- await fs.mkdir(dir, { recursive: true });
105
-
106
105
  rejectIfNecessary(
107
106
  await Promise.allSettled([
108
- updateSliceModelFile({
107
+ writeSliceModel({
109
108
  libraryID: data.libraryID,
110
109
  model: data.model,
111
- ...context,
110
+ format: context.options.format,
111
+ helpers: context.helpers,
112
112
  }),
113
- createComponentFile({ dir, data, ...context }),
113
+ createComponentFile({ data, ...context }),
114
114
  ]),
115
115
  );
116
116
 
117
117
  rejectIfNecessary(
118
118
  await Promise.allSettled([
119
- upsertGlobalContentTypes(context),
120
- upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context }),
119
+ upsertSliceLibraryIndexFile({
120
+ libraryID: data.libraryID,
121
+ ...context,
122
+ }),
123
+ upsertGlobalTypeScriptTypes({
124
+ filename: context.options.generatedTypesFilePath,
125
+ format: context.options.format,
126
+ helpers: context.helpers,
127
+ actions: context.actions,
128
+ }),
121
129
  ]),
122
130
  );
123
131
  };
@@ -1,8 +1,9 @@
1
1
  import { SliceMachineContext } from "@slicemachine/plugin-kit";
2
+ import { checkHasProjectFile } from "@slicemachine/plugin-kit/fs";
3
+ import * as path from "node:path";
2
4
 
3
5
  import { PluginOptions } from "../types";
4
6
  import { checkHasSrcDirectory } from "./checkHasSrcDirectory";
5
- import { checkPathExists } from "./checkPathExists";
6
7
 
7
8
  type CheckHasAppRouterArgs = Pick<
8
9
  SliceMachineContext<PluginOptions>,
@@ -14,9 +15,8 @@ export async function checkHasAppRouter(
14
15
  ): Promise<boolean> {
15
16
  const hasSrcDirectory = await checkHasSrcDirectory({ helpers: args.helpers });
16
17
 
17
- return await checkPathExists(
18
- hasSrcDirectory
19
- ? args.helpers.joinPathFromRoot("src", "app")
20
- : args.helpers.joinPathFromRoot("app"),
21
- );
18
+ return await checkHasProjectFile({
19
+ filename: hasSrcDirectory ? path.join("src", "app") : "app",
20
+ helpers: args.helpers,
21
+ });
22
22
  }
@@ -1,7 +1,7 @@
1
1
  import { SliceMachineContext } from "@slicemachine/plugin-kit";
2
+ import { checkHasProjectFile } from "@slicemachine/plugin-kit/fs";
2
3
 
3
4
  import { PluginOptions } from "../types";
4
- import { checkPathExists } from "./checkPathExists";
5
5
 
6
6
  type CheckHasSrcDirectoryArgs = Pick<
7
7
  SliceMachineContext<PluginOptions>,
@@ -11,5 +11,8 @@ type CheckHasSrcDirectoryArgs = Pick<
11
11
  export async function checkHasSrcDirectory(
12
12
  args: CheckHasSrcDirectoryArgs,
13
13
  ): Promise<boolean> {
14
- return await checkPathExists(args.helpers.joinPathFromRoot("src"));
14
+ return await checkHasProjectFile({
15
+ filename: "src",
16
+ helpers: args.helpers,
17
+ });
15
18
  }
@@ -1,7 +1,7 @@
1
1
  import { SliceMachineContext } from "@slicemachine/plugin-kit";
2
+ import { checkIsTypeScriptProject as baseCheckIsTypeScriptProject } from "@slicemachine/plugin-kit/fs";
2
3
 
3
4
  import { PluginOptions } from "../types";
4
- import { checkPathExists } from "./checkPathExists";
5
5
 
6
6
  type CheckIsTypeScriptProjectArgs = {
7
7
  helpers: SliceMachineContext<PluginOptions>["helpers"];
@@ -11,7 +11,8 @@ type CheckIsTypeScriptProjectArgs = {
11
11
  export const checkIsTypeScriptProject = async (
12
12
  args: CheckIsTypeScriptProjectArgs,
13
13
  ): Promise<boolean> => {
14
- const tsConfigPath = args.helpers.joinPathFromRoot("tsconfig.json");
15
-
16
- return args.options.typescript ?? (await checkPathExists(tsConfigPath));
14
+ return (
15
+ args.options.typescript ??
16
+ baseCheckIsTypeScriptProject({ helpers: args.helpers })
17
+ );
17
18
  };
@@ -1,13 +1,16 @@
1
1
  import { SliceMachineContext } from "@slicemachine/plugin-kit";
2
+ import {
3
+ buildSliceDirectoryPath,
4
+ buildSliceLibraryDirectoryPath,
5
+ writeProjectFile,
6
+ } from "@slicemachine/plugin-kit/fs";
2
7
  import { stripIndent } from "common-tags";
3
- import * as fs from "node:fs/promises";
4
8
  import * as path from "node:path";
5
9
  import { NON_EDITABLE_FILE_BANNER } from "../constants";
6
10
 
7
11
  import { PluginOptions } from "../types";
8
12
 
9
13
  import { pascalCase } from "./pascalCase";
10
- import { buildSliceLibraryDirectoryPath } from "./buildSliceLibraryDirectoryPath";
11
14
  import { getJSFileExtension } from "./getJSFileExtension";
12
15
 
13
16
  type UpsertSliceLibraryIndexFileArgs = {
@@ -17,18 +20,6 @@ type UpsertSliceLibraryIndexFileArgs = {
17
20
  export const upsertSliceLibraryIndexFile = async (
18
21
  args: UpsertSliceLibraryIndexFileArgs,
19
22
  ): Promise<void> => {
20
- const extension = await getJSFileExtension({
21
- helpers: args.helpers,
22
- options: args.options,
23
- });
24
- const filePath = path.join(
25
- buildSliceLibraryDirectoryPath({
26
- libraryID: args.libraryID,
27
- helpers: args.helpers,
28
- }),
29
- `index.${extension}`,
30
- );
31
-
32
23
  const slices = await args.actions.readAllSliceModelsForLibrary({
33
24
  libraryID: args.libraryID,
34
25
  });
@@ -42,28 +33,44 @@ export const upsertSliceLibraryIndexFile = async (
42
33
  import dynamic from 'next/dynamic'
43
34
 
44
35
  export const components = {
45
- ${slices
46
- .map((slice) => {
47
- const id = slice.model.id;
48
- const dirName = pascalCase(slice.model.name);
49
-
50
- return `${id}: dynamic(() => import('./${dirName}'))`;
51
- })
52
- .join(",\n")}
36
+ ${(
37
+ await Promise.all(
38
+ slices.map(async (slice) => {
39
+ const id = slice.model.id;
40
+ const dirName = path.basename(
41
+ await buildSliceDirectoryPath({
42
+ model: slice.model,
43
+ helpers: args.helpers,
44
+ libraryID: args.libraryID,
45
+ }),
46
+ );
47
+
48
+ return `${id}: dynamic(() => import('./${dirName}'))`;
49
+ }),
50
+ )
51
+ ).join(",\n")}
53
52
  }
54
53
  `;
55
54
  } else {
56
55
  contents = stripIndent`
57
56
  ${NON_EDITABLE_FILE_BANNER}
58
57
 
59
- ${slices
60
- .map((slice) => {
61
- const componentName = pascalCase(slice.model.name);
62
- const dirName = pascalCase(slice.model.name);
58
+ ${(
59
+ await Promise.all(
60
+ slices.map(async (slice) => {
61
+ const dirName = path.basename(
62
+ await buildSliceDirectoryPath({
63
+ model: slice.model,
64
+ helpers: args.helpers,
65
+ libraryID: args.libraryID,
66
+ }),
67
+ );
68
+ const componentName = pascalCase(slice.model.name);
63
69
 
64
- return `import ${componentName} from "./${dirName}";`;
65
- })
66
- .join("\n")}
70
+ return `import ${componentName} from "./${dirName}";`;
71
+ }),
72
+ )
73
+ ).join("\n")}
67
74
 
68
75
  export const components = {
69
76
  ${slices
@@ -78,9 +85,22 @@ export const upsertSliceLibraryIndexFile = async (
78
85
  `;
79
86
  }
80
87
 
81
- if (args.options.format) {
82
- contents = await args.helpers.format(contents, filePath);
83
- }
88
+ const extension = await getJSFileExtension({
89
+ helpers: args.helpers,
90
+ options: args.options,
91
+ });
92
+ const filePath = path.join(
93
+ buildSliceLibraryDirectoryPath({
94
+ libraryID: args.libraryID,
95
+ helpers: args.helpers,
96
+ }),
97
+ `index.${extension}`,
98
+ );
84
99
 
85
- await fs.writeFile(filePath, contents);
100
+ await writeProjectFile({
101
+ filename: filePath,
102
+ contents,
103
+ format: args.options.format,
104
+ helpers: args.helpers,
105
+ });
86
106
  };