@slicemachine/adapter-nuxt 0.3.10-dev-next-release.2 → 0.3.10-dev-next-release.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ const magicast = require("magicast");
7
7
  const checkPathExists = require("../lib/checkPathExists.cjs");
8
8
  const rejectIfNecessary = require("../lib/rejectIfNecessary.cjs");
9
9
  const checkIsTypeScriptProject = require("../lib/checkIsTypeScriptProject.cjs");
10
+ const checkHasSrcDirectory = require("../lib/checkHasSrcDirectory.cjs");
10
11
  function _interopNamespaceDefault(e) {
11
12
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
12
13
  if (e) {
@@ -101,11 +102,33 @@ const createSliceSimulatorPage = async ({ helpers, options }) => {
101
102
  }
102
103
  await fs__namespace.writeFile(filePath, contents);
103
104
  };
105
+ const modifySliceMachineConfig = async ({ helpers, options }) => {
106
+ var _a2;
107
+ const hasSrcDirectory = await checkHasSrcDirectory.checkHasSrcDirectory({ helpers });
108
+ const project = await helpers.getProject();
109
+ (_a2 = project.config).localSliceSimulatorURL || (_a2.localSliceSimulatorURL = "http://localhost:3000/slice-simulator");
110
+ if (hasSrcDirectory && JSON.stringify(project.config.libraries) === JSON.stringify(["./slices"])) {
111
+ try {
112
+ const entries = await fs__namespace.readdir(helpers.joinPathFromRoot("slices"));
113
+ if (!entries.map((entry) => path__namespace.parse(entry).name).includes("index")) {
114
+ project.config.libraries = ["./src/slices"];
115
+ }
116
+ } catch (error) {
117
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
118
+ project.config.libraries = ["./src/slices"];
119
+ }
120
+ }
121
+ }
122
+ await helpers.updateSliceMachineConfig(project.config, {
123
+ format: options.format
124
+ });
125
+ };
104
126
  const projectInit = async ({ installDependencies: _installDependencies }, context) => {
105
127
  rejectIfNecessary.rejectIfNecessary(await Promise.allSettled([
106
- await installDependencies({ installDependencies: _installDependencies }),
107
- await configurePrismicModule(context),
108
- await createSliceSimulatorPage(context)
128
+ installDependencies({ installDependencies: _installDependencies }),
129
+ configurePrismicModule(context),
130
+ createSliceSimulatorPage(context),
131
+ modifySliceMachineConfig(context)
109
132
  ]));
110
133
  };
111
134
  exports.projectInit = projectInit;
@@ -1 +1 @@
1
- {"version":3,"file":"project-init.cjs","sources":["../../../src/hooks/project-init.ts"],"sourcesContent":["import * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\nimport type {\n\tProjectInitHook,\n\tProjectInitHookData,\n\tSliceMachineContext,\n} from \"@slicemachine/plugin-kit\";\nimport { stripIndent } from \"common-tags\";\nimport { loadFile, writeFile, type ASTNode } from \"magicast\";\n\nimport { checkPathExists } from \"../lib/checkPathExists\";\nimport { rejectIfNecessary } from \"../lib/rejectIfNecessary\";\nimport { checkIsTypeScriptProject } from \"../lib/checkIsTypeScriptProject\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst NUXT_PRISMIC = \"@nuxtjs/prismic\";\n\ntype InstallDependenciesArgs = {\n\tinstallDependencies: ProjectInitHookData[\"installDependencies\"];\n};\n\nconst installDependencies = async ({\n\tinstallDependencies,\n}: InstallDependenciesArgs) => {\n\ttry {\n\t\tawait installDependencies({\n\t\t\tdependencies: {\n\t\t\t\t[NUXT_PRISMIC]: \"^3.0.0\",\n\t\t\t},\n\t\t\tdev: true,\n\t\t});\n\t} catch (error) {\n\t\t// TODO: Remove when latest is published and documented\n\t\t// Fallback to RC if latest is still not available\n\t\tawait installDependencies({\n\t\t\tdependencies: {\n\t\t\t\t[NUXT_PRISMIC]: \"rc\",\n\t\t\t},\n\t\t\tdev: true,\n\t\t});\n\t}\n};\n\ntype ConfigurePrismicModuleArgs = SliceMachineContext<PluginOptions>;\n\nconst configurePrismicModule = async ({\n\thelpers,\n\tproject,\n}: ConfigurePrismicModuleArgs) => {\n\tlet nuxtConfigPath = helpers.joinPathFromRoot(\"nuxt.config.js\");\n\n\tif (!(await checkPathExists(nuxtConfigPath))) {\n\t\tnuxtConfigPath = helpers.joinPathFromRoot(\"nuxt.config.ts\");\n\n\t\t// nuxt.config.* not found\n\t\tif (!(await checkPathExists(nuxtConfigPath))) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst mod = await loadFile(nuxtConfigPath);\n\tconst config =\n\t\tmod.exports.default.$type === \"function-call\"\n\t\t\t? mod.exports.default.$args[0]\n\t\t\t: mod.exports.default;\n\n\t// Register Prismic module\n\tlet hasInlinedConfiguration = false;\n\tconst hasPrismicModuleRegistered = (config.modules || []).find(\n\t\t(registration: string | [string, unknown]) => {\n\t\t\tif (typeof registration === \"string\") {\n\t\t\t\treturn registration === NUXT_PRISMIC;\n\t\t\t} else if (Array.isArray(registration)) {\n\t\t\t\thasInlinedConfiguration = !!registration[1];\n\n\t\t\t\treturn registration[0] === NUXT_PRISMIC;\n\t\t\t}\n\n\t\t\treturn false;\n\t\t},\n\t);\n\n\tif (!hasPrismicModuleRegistered) {\n\t\tconfig.modules ||= [];\n\t\tconfig.modules.push(NUXT_PRISMIC);\n\t}\n\n\t// Append Prismic module configuration\n\tconst endpoint = project.config.repositoryName;\n\tif (!hasInlinedConfiguration) {\n\t\tconfig.prismic ||= {};\n\t\tconfig.prismic.endpoint = endpoint;\n\t}\n\n\tawait writeFile(mod as unknown as ASTNode, nuxtConfigPath);\n};\n\ntype CreateSliceSimulatorPageArgs = SliceMachineContext<PluginOptions>;\n\nconst createSliceSimulatorPage = async ({\n\thelpers,\n\toptions,\n}: CreateSliceSimulatorPageArgs) => {\n\tconst isTypeScriptProject = await checkIsTypeScriptProject({\n\t\thelpers,\n\t\toptions,\n\t});\n\n\tconst srcPagesDirectoryExists = await checkPathExists(\n\t\thelpers.joinPathFromRoot(\"src/pages\"),\n\t);\n\n\tconst filePath = helpers.joinPathFromRoot(\n\t\tsrcPagesDirectoryExists ? \"src/pages\" : \"pages\",\n\t\t\"slice-simulator.vue\",\n\t);\n\n\tif (await checkPathExists(filePath)) {\n\t\treturn;\n\t}\n\n\tawait fs.mkdir(path.dirname(filePath), { recursive: true });\n\n\tconst scriptAttributes = [\"setup\"];\n\tif (isTypeScriptProject) {\n\t\tscriptAttributes.push('lang=\"ts\"');\n\t}\n\n\tlet contents = stripIndent`\n\t\t<template>\n\t\t\t<SliceSimulator #default=\"{ slices }\">\n\t\t\t\t<SliceZone :slices=\"slices\" :components=\"components\" />\n\t\t\t</SliceSimulator>\n\t\t</template>\n\n\t\t<script ${scriptAttributes.join(\" \")}>\n\t\timport { SliceSimulator } from \"@slicemachine/adapter-nuxt/simulator\";\n\t\timport { components } from \"~/slices\";\n\t\t</script>\n\t`;\n\n\tif (options.format) {\n\t\tcontents = await helpers.format(contents, filePath, {\n\t\t\tprettier: { parser: \"vue\" },\n\t\t});\n\t}\n\n\tawait fs.writeFile(filePath, contents);\n};\n\nexport const projectInit: ProjectInitHook<PluginOptions> = async (\n\t{ installDependencies: _installDependencies },\n\tcontext,\n) => {\n\trejectIfNecessary(\n\t\tawait Promise.allSettled([\n\t\t\tawait installDependencies({ installDependencies: _installDependencies }),\n\t\t\tawait configurePrismicModule(context),\n\t\t\tawait createSliceSimulatorPage(context),\n\t\t]),\n\t);\n};\n"],"names":["installDependencies","checkPathExists","loadFile","writeFile","checkIsTypeScriptProject","fs","path","stripIndent","rejectIfNecessary"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAA;AAgBA,MAAM,eAAe;AAMrB,MAAM,sBAAsB,OAAO,EAClC,qBAAAA,2BAC6B;AACzB,MAAA;AACH,UAAMA,qBAAoB;AAAA,MACzB,cAAc;AAAA,QACb,CAAC,YAAY,GAAG;AAAA,MAChB;AAAA,MACD,KAAK;AAAA,IAAA,CACL;AAAA,WACO;AAGR,UAAMA,qBAAoB;AAAA,MACzB,cAAc;AAAA,QACb,CAAC,YAAY,GAAG;AAAA,MAChB;AAAA,MACD,KAAK;AAAA,IAAA,CACL;AAAA,EACD;AACF;AAIA,MAAM,yBAAyB,OAAO,EACrC,SACA,cACgC;AAC5B,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K,QAAAI,cAAG,MAAMC,gBAAK,QAAQ,QAAQ,GAAG,EAAE,WAAW,KAAA,CAAM;AAEpD,QAAA,mBAAmB,CAAC,OAAO;AACjC,MAAI,qBAAqB;AACxB,qBAAiB,KAAK,WAAW;AAAA,EACjC;AAED,MAAI,WAAWC,WAAW,YAAA,OAAA,KAAA,WAAA,CAAA,6KAOW,wIAA1B,CAAA,IAAA,iBAAiB,KAAK,GAAG,CAAA;AAMpC,MAAI,QAAQ,QAAQ;AACnB,eAAW,MAAM,QAAQ,OAAO,UAAU,UAAU;AAAA,MACnD,UAAU,EAAE,QAAQ,MAAO;AAAA,IAAA,CAC3B;AAAA,EACD;AAEK,QAAAF,cAAG,UAAU,UAAU,QAAQ;AACtC;AAEO,MAAM,cAA8C,OAC1D,EAAE,qBAAqB,qBAAA,GACvB,YACG;AAEFG,sCAAA,MAAM,QAAQ,WAAW;AAAA,IACxB,MAAM,oBAAoB,EAAE,qBAAqB,sBAAsB;AAAA,IACvE,MAAM,uBAAuB,OAAO;AAAA,IACpC,MAAM,yBAAyB,OAAO;AAAA,EACtC,CAAA,CAAC;AAEJ;;"}
1
+ {"version":3,"file":"project-init.cjs","sources":["../../../src/hooks/project-init.ts"],"sourcesContent":["import * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\nimport type {\n\tProjectInitHook,\n\tProjectInitHookData,\n\tSliceMachineContext,\n} from \"@slicemachine/plugin-kit\";\nimport { stripIndent } from \"common-tags\";\nimport { loadFile, writeFile, type ASTNode } from \"magicast\";\n\nimport { checkPathExists } from \"../lib/checkPathExists\";\nimport { rejectIfNecessary } from \"../lib/rejectIfNecessary\";\nimport { checkIsTypeScriptProject } from \"../lib/checkIsTypeScriptProject\";\nimport { checkHasSrcDirectory } from \"../lib/checkHasSrcDirectory\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst NUXT_PRISMIC = \"@nuxtjs/prismic\";\n\ntype InstallDependenciesArgs = {\n\tinstallDependencies: ProjectInitHookData[\"installDependencies\"];\n};\n\nconst installDependencies = async ({\n\tinstallDependencies,\n}: InstallDependenciesArgs) => {\n\ttry {\n\t\tawait installDependencies({\n\t\t\tdependencies: {\n\t\t\t\t[NUXT_PRISMIC]: \"^3.0.0\",\n\t\t\t},\n\t\t\tdev: true,\n\t\t});\n\t} catch (error) {\n\t\t// TODO: Remove when latest is published and documented\n\t\t// Fallback to RC if latest is still not available\n\t\tawait installDependencies({\n\t\t\tdependencies: {\n\t\t\t\t[NUXT_PRISMIC]: \"rc\",\n\t\t\t},\n\t\t\tdev: true,\n\t\t});\n\t}\n};\n\ntype ConfigurePrismicModuleArgs = SliceMachineContext<PluginOptions>;\n\nconst configurePrismicModule = async ({\n\thelpers,\n\tproject,\n}: ConfigurePrismicModuleArgs) => {\n\tlet nuxtConfigPath = helpers.joinPathFromRoot(\"nuxt.config.js\");\n\n\tif (!(await checkPathExists(nuxtConfigPath))) {\n\t\tnuxtConfigPath = helpers.joinPathFromRoot(\"nuxt.config.ts\");\n\n\t\t// nuxt.config.* not found\n\t\tif (!(await checkPathExists(nuxtConfigPath))) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst mod = await loadFile(nuxtConfigPath);\n\tconst config =\n\t\tmod.exports.default.$type === \"function-call\"\n\t\t\t? mod.exports.default.$args[0]\n\t\t\t: mod.exports.default;\n\n\t// Register Prismic module\n\tlet hasInlinedConfiguration = false;\n\tconst hasPrismicModuleRegistered = (config.modules || []).find(\n\t\t(registration: string | [string, unknown]) => {\n\t\t\tif (typeof registration === \"string\") {\n\t\t\t\treturn registration === NUXT_PRISMIC;\n\t\t\t} else if (Array.isArray(registration)) {\n\t\t\t\thasInlinedConfiguration = !!registration[1];\n\n\t\t\t\treturn registration[0] === NUXT_PRISMIC;\n\t\t\t}\n\n\t\t\treturn false;\n\t\t},\n\t);\n\n\tif (!hasPrismicModuleRegistered) {\n\t\tconfig.modules ||= [];\n\t\tconfig.modules.push(NUXT_PRISMIC);\n\t}\n\n\t// Append Prismic module configuration\n\tconst endpoint = project.config.repositoryName;\n\tif (!hasInlinedConfiguration) {\n\t\tconfig.prismic ||= {};\n\t\tconfig.prismic.endpoint = endpoint;\n\t}\n\n\tawait writeFile(mod as unknown as ASTNode, nuxtConfigPath);\n};\n\ntype CreateSliceSimulatorPageArgs = SliceMachineContext<PluginOptions>;\n\nconst createSliceSimulatorPage = async ({\n\thelpers,\n\toptions,\n}: CreateSliceSimulatorPageArgs) => {\n\tconst isTypeScriptProject = await checkIsTypeScriptProject({\n\t\thelpers,\n\t\toptions,\n\t});\n\n\tconst srcPagesDirectoryExists = await checkPathExists(\n\t\thelpers.joinPathFromRoot(\"src/pages\"),\n\t);\n\n\tconst filePath = helpers.joinPathFromRoot(\n\t\tsrcPagesDirectoryExists ? \"src/pages\" : \"pages\",\n\t\t\"slice-simulator.vue\",\n\t);\n\n\tif (await checkPathExists(filePath)) {\n\t\treturn;\n\t}\n\n\tawait fs.mkdir(path.dirname(filePath), { recursive: true });\n\n\tconst scriptAttributes = [\"setup\"];\n\tif (isTypeScriptProject) {\n\t\tscriptAttributes.push('lang=\"ts\"');\n\t}\n\n\tlet contents = stripIndent`\n\t\t<template>\n\t\t\t<SliceSimulator #default=\"{ slices }\">\n\t\t\t\t<SliceZone :slices=\"slices\" :components=\"components\" />\n\t\t\t</SliceSimulator>\n\t\t</template>\n\n\t\t<script ${scriptAttributes.join(\" \")}>\n\t\timport { SliceSimulator } from \"@slicemachine/adapter-nuxt/simulator\";\n\t\timport { components } from \"~/slices\";\n\t\t</script>\n\t`;\n\n\tif (options.format) {\n\t\tcontents = await helpers.format(contents, filePath, {\n\t\t\tprettier: { parser: \"vue\" },\n\t\t});\n\t}\n\n\tawait fs.writeFile(filePath, contents);\n};\n\nconst modifySliceMachineConfig = async ({\n\thelpers,\n\toptions,\n}: SliceMachineContext<PluginOptions>) => {\n\tconst hasSrcDirectory = await checkHasSrcDirectory({ helpers });\n\tconst project = await helpers.getProject();\n\n\t// Add Slice Simulator URL.\n\tproject.config.localSliceSimulatorURL ||=\n\t\t\"http://localhost:3000/slice-simulator\";\n\n\t// Nest the default Slice Library in the src directory if it exists and\n\t// is empty.\n\tif (\n\t\thasSrcDirectory &&\n\t\tJSON.stringify(project.config.libraries) === JSON.stringify([\"./slices\"])\n\t) {\n\t\ttry {\n\t\t\tconst entries = await fs.readdir(helpers.joinPathFromRoot(\"slices\"));\n\n\t\t\tif (!entries.map((entry) => path.parse(entry).name).includes(\"index\")) {\n\t\t\t\tproject.config.libraries = [\"./src/slices\"];\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (\n\t\t\t\terror instanceof Error &&\n\t\t\t\t\"code\" in error &&\n\t\t\t\terror.code === \"ENOENT\"\n\t\t\t) {\n\t\t\t\t// The directory does not exist, which means we\n\t\t\t\t// can safely nest the library.\n\t\t\t\tproject.config.libraries = [\"./src/slices\"];\n\t\t\t}\n\t\t}\n\t}\n\n\tawait helpers.updateSliceMachineConfig(project.config, {\n\t\tformat: options.format,\n\t});\n};\n\nexport const projectInit: ProjectInitHook<PluginOptions> = async (\n\t{ installDependencies: _installDependencies },\n\tcontext,\n) => {\n\trejectIfNecessary(\n\t\tawait Promise.allSettled([\n\t\t\tinstallDependencies({ installDependencies: _installDependencies }),\n\t\t\tconfigurePrismicModule(context),\n\t\t\tcreateSliceSimulatorPage(context),\n\t\t\tmodifySliceMachineConfig(context),\n\t\t]),\n\t);\n};\n"],"names":["installDependencies","checkPathExists","loadFile","writeFile","checkIsTypeScriptProject","fs","path","stripIndent","checkHasSrcDirectory","_a","rejectIfNecessary"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAA;AAiBA,MAAM,eAAe;AAMrB,MAAM,sBAAsB,OAAO,EAClC,qBAAAA,2BAC6B;AACzB,MAAA;AACH,UAAMA,qBAAoB;AAAA,MACzB,cAAc;AAAA,QACb,CAAC,YAAY,GAAG;AAAA,MAChB;AAAA,MACD,KAAK;AAAA,IAAA,CACL;AAAA,WACO;AAGR,UAAMA,q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AAc;AAAA,MAC1C;AAAA,IACD;AAAA,EACD;AAEK,QAAA,QAAQ,yBAAyB,QAAQ,QAAQ;AAAA,IACtD,QAAQ,QAAQ;AAAA,EAAA,CAChB;AACF;AAEO,MAAM,cAA8C,OAC1D,EAAE,qBAAqB,qBAAA,GACvB,YACG;AAEFI,sCAAA,MAAM,QAAQ,WAAW;AAAA,IACxB,oBAAoB,EAAE,qBAAqB,sBAAsB;AAAA,IACjE,uBAAuB,OAAO;AAAA,IAC9B,yBAAyB,OAAO;AAAA,IAChC,yBAAyB,OAAO;AAAA,EAChC,CAAA,CAAC;AAEJ;;"}
@@ -5,6 +5,7 @@ import { loadFile, writeFile } from "magicast";
5
5
  import { checkPathExists } from "../lib/checkPathExists.js";
6
6
  import { rejectIfNecessary } from "../lib/rejectIfNecessary.js";
7
7
  import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject.js";
8
+ import { checkHasSrcDirectory } from "../lib/checkHasSrcDirectory.js";
8
9
  var __freeze = Object.freeze;
9
10
  var __defProp = Object.defineProperty;
10
11
  var __template = (cooked, raw) => __freeze(__defProp(cooked, "raw", { value: __freeze(raw || cooked.slice()) }));
@@ -81,11 +82,33 @@ const createSliceSimulatorPage = async ({ helpers, options }) => {
81
82
  }
82
83
  await fs.writeFile(filePath, contents);
83
84
  };
85
+ const modifySliceMachineConfig = async ({ helpers, options }) => {
86
+ var _a2;
87
+ const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
88
+ const project = await helpers.getProject();
89
+ (_a2 = project.config).localSliceSimulatorURL || (_a2.localSliceSimulatorURL = "http://localhost:3000/slice-simulator");
90
+ if (hasSrcDirectory && JSON.stringify(project.config.libraries) === JSON.stringify(["./slices"])) {
91
+ try {
92
+ const entries = await fs.readdir(helpers.joinPathFromRoot("slices"));
93
+ if (!entries.map((entry) => path.parse(entry).name).includes("index")) {
94
+ project.config.libraries = ["./src/slices"];
95
+ }
96
+ } catch (error) {
97
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
98
+ project.config.libraries = ["./src/slices"];
99
+ }
100
+ }
101
+ }
102
+ await helpers.updateSliceMachineConfig(project.config, {
103
+ format: options.format
104
+ });
105
+ };
84
106
  const projectInit = async ({ installDependencies: _installDependencies }, context) => {
85
107
  rejectIfNecessary(await Promise.allSettled([
86
- await installDependencies({ installDependencies: _installDependencies }),
87
- await configurePrismicModule(context),
88
- await createSliceSimulatorPage(context)
108
+ installDependencies({ installDependencies: _installDependencies }),
109
+ configurePrismicModule(context),
110
+ createSliceSimulatorPage(context),
111
+ modifySliceMachineConfig(context)
89
112
  ]));
90
113
  };
91
114
  export {
@@ -1 +1 @@
1
- {"version":3,"file":"project-init.js","sources":["../../../src/hooks/project-init.ts"],"sourcesContent":["import * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\nimport type {\n\tProjectInitHook,\n\tProjectInitHookData,\n\tSliceMachineContext,\n} from \"@slicemachine/plugin-kit\";\nimport { stripIndent } from \"common-tags\";\nimport { loadFile, writeFile, type ASTNode } from \"magicast\";\n\nimport { checkPathExists } from \"../lib/checkPathExists\";\nimport { rejectIfNecessary } from \"../lib/rejectIfNecessary\";\nimport { checkIsTypeScriptProject } from \"../lib/checkIsTypeScriptProject\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst NUXT_PRISMIC = \"@nuxtjs/prismic\";\n\ntype InstallDependenciesArgs = {\n\tinstallDependencies: ProjectInitHookData[\"installDependencies\"];\n};\n\nconst installDependencies = async ({\n\tinstallDependencies,\n}: InstallDependenciesArgs) => {\n\ttry {\n\t\tawait installDependencies({\n\t\t\tdependencies: {\n\t\t\t\t[NUXT_PRISMIC]: \"^3.0.0\",\n\t\t\t},\n\t\t\tdev: true,\n\t\t});\n\t} catch (error) {\n\t\t// TODO: Remove when latest is published and documented\n\t\t// Fallback to RC if latest is still not available\n\t\tawait installDependencies({\n\t\t\tdependencies: {\n\t\t\t\t[NUXT_PRISMIC]: \"rc\",\n\t\t\t},\n\t\t\tdev: true,\n\t\t});\n\t}\n};\n\ntype ConfigurePrismicModuleArgs = SliceMachineContext<PluginOptions>;\n\nconst configurePrismicModule = async ({\n\thelpers,\n\tproject,\n}: ConfigurePrismicModuleArgs) => {\n\tlet nuxtConfigPath = helpers.joinPathFromRoot(\"nuxt.config.js\");\n\n\tif (!(await checkPathExists(nuxtConfigPath))) {\n\t\tnuxtConfigPath = helpers.joinPathFromRoot(\"nuxt.config.ts\");\n\n\t\t// nuxt.config.* not found\n\t\tif (!(await checkPathExists(nuxtConfigPath))) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst mod = await loadFile(nuxtConfigPath);\n\tconst config =\n\t\tmod.exports.default.$type === \"function-call\"\n\t\t\t? mod.exports.default.$args[0]\n\t\t\t: mod.exports.default;\n\n\t// Register Prismic module\n\tlet hasInlinedConfiguration = false;\n\tconst hasPrismicModuleRegistered = (config.modules || []).find(\n\t\t(registration: string | [string, unknown]) => {\n\t\t\tif (typeof registration === \"string\") {\n\t\t\t\treturn registration === NUXT_PRISMIC;\n\t\t\t} else if (Array.isArray(registration)) {\n\t\t\t\thasInlinedConfiguration = !!registration[1];\n\n\t\t\t\treturn registration[0] === NUXT_PRISMIC;\n\t\t\t}\n\n\t\t\treturn false;\n\t\t},\n\t);\n\n\tif (!hasPrismicModuleRegistered) {\n\t\tconfig.modules ||= [];\n\t\tconfig.modules.push(NUXT_PRISMIC);\n\t}\n\n\t// Append Prismic module configuration\n\tconst endpoint = project.config.repositoryName;\n\tif (!hasInlinedConfiguration) {\n\t\tconfig.prismic ||= {};\n\t\tconfig.prismic.endpoint = endpoint;\n\t}\n\n\tawait writeFile(mod as unknown as ASTNode, nuxtConfigPath);\n};\n\ntype CreateSliceSimulatorPageArgs = SliceMachineContext<PluginOptions>;\n\nconst createSliceSimulatorPage = async ({\n\thelpers,\n\toptions,\n}: CreateSliceSimulatorPageArgs) => {\n\tconst isTypeScriptProject = await checkIsTypeScriptProject({\n\t\thelpers,\n\t\toptions,\n\t});\n\n\tconst srcPagesDirectoryExists = await checkPathExists(\n\t\thelpers.joinPathFromRoot(\"src/pages\"),\n\t);\n\n\tconst filePath = helpers.joinPathFromRoot(\n\t\tsrcPagesDirectoryExists ? \"src/pages\" : \"pages\",\n\t\t\"slice-simulator.vue\",\n\t);\n\n\tif (await checkPathExists(filePath)) {\n\t\treturn;\n\t}\n\n\tawait fs.mkdir(path.dirname(filePath), { recursive: true });\n\n\tconst scriptAttributes = [\"setup\"];\n\tif (isTypeScriptProject) {\n\t\tscriptAttributes.push('lang=\"ts\"');\n\t}\n\n\tlet contents = stripIndent`\n\t\t<template>\n\t\t\t<SliceSimulator #default=\"{ slices }\">\n\t\t\t\t<SliceZone :slices=\"slices\" :components=\"components\" />\n\t\t\t</SliceSimulator>\n\t\t</template>\n\n\t\t<script ${scriptAttributes.join(\" \")}>\n\t\timport { SliceSimulator } from \"@slicemachine/adapter-nuxt/simulator\";\n\t\timport { components } from \"~/slices\";\n\t\t</script>\n\t`;\n\n\tif (options.format) {\n\t\tcontents = await helpers.format(contents, filePath, {\n\t\t\tprettier: { parser: \"vue\" },\n\t\t});\n\t}\n\n\tawait fs.writeFile(filePath, contents);\n};\n\nexport const projectInit: ProjectInitHook<PluginOptions> = async (\n\t{ installDependencies: _installDependencies },\n\tcontext,\n) => {\n\trejectIfNecessary(\n\t\tawait Promise.allSettled([\n\t\t\tawait installDependencies({ installDependencies: _installDependencies }),\n\t\t\tawait configurePrismicModule(context),\n\t\t\tawait createSliceSimulatorPage(context),\n\t\t]),\n\t);\n};\n"],"names":["installDependencies"],"mappings":";;;;;;;;;;AAAA,IAAA;AAgBA,MAAM,eAAe;AAMrB,MAAM,sBAAsB,OAAO,EAClC,qBAAAA,2BAC6B;AACzB,MAAA;AACH,UAAMA,qBAAoB;AAAA,MACzB,cAAc;AAAA,QACb,CAAC,YAAY,GAAG;AAAA,MAChB;AAAA,MACD,KAAK;AAAA,IAAA,CACL;AAAA,WACO;AAGR,UAAMA,qBAAoB;AAAA,MACzB,cAAc;AAAA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QAAQ,iBACxB,0BAA0B,cAAc,SACxC,qBAAqB;AAGlB,MAAA,MAAM,gBAAgB,QAAQ,GAAG;AACpC;AAAA,EACA;AAEK,QAAA,GAAG,MAAM,KAAK,QAAQ,QAAQ,GAAG,EAAE,WAAW,KAAA,CAAM;AAEpD,QAAA,mBAAmB,CAAC,OAAO;AACjC,MAAI,qBAAqB;AACxB,qBAAiB,KAAK,WAAW;AAAA,EACjC;AAED,MAAI,WAAW,YAAW,OAAA,KAAA,WAAA,CAAA,6KAOW,wIAA1B,CAAA,IAAA,iBAAiB,KAAK,GAAG,CAAA;AAMpC,MAAI,QAAQ,QAAQ;AACnB,eAAW,MAAM,QAAQ,OAAO,UAAU,UAAU;AAAA,MACnD,UAAU,EAAE,QAAQ,MAAO;AAAA,IAAA,CAC3B;AAAA,EACD;AAEK,QAAA,GAAG,UAAU,UAAU,QAAQ;AACtC;AAEO,MAAM,cAA8C,OAC1D,EAAE,qBAAqB,qBAAA,GACvB,YACG;AAEF,oBAAA,MAAM,QAAQ,WAAW;AAAA,IACxB,MAAM,oBAAoB,EAAE,qBAAqB,sBAAsB;AAAA,IACvE,MAAM,uBAAuB,OAAO;AAAA,IACpC,MAAM,yBAAyB,OAAO;AAAA,EACtC,CAAA,CAAC;AAEJ;"}
1
+ {"version":3,"file":"project-init.js","sources":["../../../src/hooks/project-init.ts"],"sourcesContent":["import * as fs from \"node:fs/promises\";\nimport * as path from \"node:path\";\nimport type {\n\tProjectInitHook,\n\tProjectInitHookData,\n\tSliceMachineContext,\n} from \"@slicemachine/plugin-kit\";\nimport { stripIndent } from \"common-tags\";\nimport { loadFile, writeFile, type ASTNode } from \"magicast\";\n\nimport { checkPathExists } from \"../lib/checkPathExists\";\nimport { rejectIfNecessary } from \"../lib/rejectIfNecessary\";\nimport { checkIsTypeScriptProject } from \"../lib/checkIsTypeScriptProject\";\nimport { checkHasSrcDirectory } from \"../lib/checkHasSrcDirectory\";\n\nimport type { PluginOptions } from \"../types\";\n\nconst NUXT_PRISMIC = \"@nuxtjs/prismic\";\n\ntype InstallDependenciesArgs = {\n\tinstallDependencies: ProjectInitHookData[\"installDependencies\"];\n};\n\nconst installDependencies = async ({\n\tinstallDependencies,\n}: InstallDependenciesArgs) => {\n\ttry {\n\t\tawait installDependencies({\n\t\t\tdependencies: {\n\t\t\t\t[NUXT_PRISMIC]: \"^3.0.0\",\n\t\t\t},\n\t\t\tdev: true,\n\t\t});\n\t} catch (error) {\n\t\t// TODO: Remove when latest is published and documented\n\t\t// Fallback to RC if latest is still not available\n\t\tawait installDependencies({\n\t\t\tdependencies: {\n\t\t\t\t[NUXT_PRISMIC]: \"rc\",\n\t\t\t},\n\t\t\tdev: true,\n\t\t});\n\t}\n};\n\ntype ConfigurePrismicModuleArgs = SliceMachineContext<PluginOptions>;\n\nconst configurePrismicModule = async ({\n\thelpers,\n\tproject,\n}: ConfigurePrismicModuleArgs) => {\n\tlet nuxtConfigPath = helpers.joinPathFromRoot(\"nuxt.config.js\");\n\n\tif (!(await checkPathExists(nuxtConfigPath))) {\n\t\tnuxtConfigPath = helpers.joinPathFromRoot(\"nuxt.config.ts\");\n\n\t\t// nuxt.config.* not found\n\t\tif (!(await checkPathExists(nuxtConfigPath))) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst mod = await loadFile(nuxtConfigPath);\n\tconst config =\n\t\tmod.exports.default.$type === \"function-call\"\n\t\t\t? mod.exports.default.$args[0]\n\t\t\t: mod.exports.default;\n\n\t// Register Prismic module\n\tlet hasInlinedConfiguration = false;\n\tconst hasPrismicModuleRegistered = (config.modules || []).find(\n\t\t(registration: string | [string, unknown]) => {\n\t\t\tif (typeof registration === \"string\") {\n\t\t\t\treturn registration === NUXT_PRISMIC;\n\t\t\t} else if (Array.isArray(registration)) {\n\t\t\t\thasInlinedConfiguration = !!registration[1];\n\n\t\t\t\treturn registration[0] === NUXT_PRISMIC;\n\t\t\t}\n\n\t\t\treturn false;\n\t\t},\n\t);\n\n\tif (!hasPrismicModuleRegistered) {\n\t\tconfig.modules ||= [];\n\t\tconfig.modules.push(NUXT_PRISMIC);\n\t}\n\n\t// Append Prismic module configuration\n\tconst endpoint = project.config.repositoryName;\n\tif (!hasInlinedConfiguration) {\n\t\tconfig.prismic ||= {};\n\t\tconfig.prismic.endpoint = endpoint;\n\t}\n\n\tawait writeFile(mod as unknown as ASTNode, nuxtConfigPath);\n};\n\ntype CreateSliceSimulatorPageArgs = SliceMachineContext<PluginOptions>;\n\nconst createSliceSimulatorPage = async ({\n\thelpers,\n\toptions,\n}: CreateSliceSimulatorPageArgs) => {\n\tconst isTypeScriptProject = await checkIsTypeScriptProject({\n\t\thelpers,\n\t\toptions,\n\t});\n\n\tconst srcPagesDirectoryExists = await checkPathExists(\n\t\thelpers.joinPathFromRoot(\"src/pages\"),\n\t);\n\n\tconst filePath = helpers.joinPathFromRoot(\n\t\tsrcPagesDirectoryExists ? \"src/pages\" : \"pages\",\n\t\t\"slice-simulator.vue\",\n\t);\n\n\tif (await checkPathExists(filePath)) {\n\t\treturn;\n\t}\n\n\tawait fs.mkdir(path.dirname(filePath), { recursive: true });\n\n\tconst scriptAttributes = [\"setup\"];\n\tif (isTypeScriptProject) {\n\t\tscriptAttributes.push('lang=\"ts\"');\n\t}\n\n\tlet contents = stripIndent`\n\t\t<template>\n\t\t\t<SliceSimulator #default=\"{ slices }\">\n\t\t\t\t<SliceZone :slices=\"slices\" :components=\"components\" />\n\t\t\t</SliceSimulator>\n\t\t</template>\n\n\t\t<script ${scriptAttributes.join(\" \")}>\n\t\timport { SliceSimulator } from \"@slicemachine/adapter-nuxt/simulator\";\n\t\timport { components } from \"~/slices\";\n\t\t</script>\n\t`;\n\n\tif (options.format) {\n\t\tcontents = await helpers.format(contents, filePath, {\n\t\t\tprettier: { parser: \"vue\" },\n\t\t});\n\t}\n\n\tawait fs.writeFile(filePath, contents);\n};\n\nconst modifySliceMachineConfig = async ({\n\thelpers,\n\toptions,\n}: SliceMachineContext<PluginOptions>) => {\n\tconst hasSrcDirectory = await checkHasSrcDirectory({ helpers });\n\tconst project = await helpers.getProject();\n\n\t// Add Slice Simulator URL.\n\tproject.config.localSliceSimulatorURL ||=\n\t\t\"http://localhost:3000/slice-simulator\";\n\n\t// Nest the default Slice Library in the src directory if it exists and\n\t// is empty.\n\tif (\n\t\thasSrcDirectory &&\n\t\tJSON.stringify(project.config.libraries) === JSON.stringify([\"./slices\"])\n\t) {\n\t\ttry {\n\t\t\tconst entries = await fs.readdir(helpers.joinPathFromRoot(\"slices\"));\n\n\t\t\tif (!entries.map((entry) => path.parse(entry).name).includes(\"index\")) {\n\t\t\t\tproject.config.libraries = [\"./src/slices\"];\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tif (\n\t\t\t\terror instanceof Error &&\n\t\t\t\t\"code\" in error &&\n\t\t\t\terror.code === \"ENOENT\"\n\t\t\t) {\n\t\t\t\t// The directory does not exist, which means we\n\t\t\t\t// can safely nest the library.\n\t\t\t\tproject.config.libraries = [\"./src/slices\"];\n\t\t\t}\n\t\t}\n\t}\n\n\tawait helpers.updateSliceMachineConfig(project.config, {\n\t\tformat: options.format,\n\t});\n};\n\nexport const projectInit: ProjectInitHook<PluginOptions> = async (\n\t{ installDependencies: _installDependencies },\n\tcontext,\n) => {\n\trejectIfNecessary(\n\t\tawait Promise.allSettled([\n\t\t\tinstallDependencies({ installDependencies: _installDependencies }),\n\t\t\tconfigurePrismicModule(context),\n\t\t\tcreateSliceSimulatorPage(context),\n\t\t\tmodifySliceMachineConfig(context),\n\t\t]),\n\t);\n};\n"],"names":["installDependencies","_a"],"mappings":";;;;;;;;;;;AAAA,IAAA;AAiBA,MAAM,eAAe;AAMrB,MAAM,sBAAsB,OAAO,EAClC,qBAAAA,2BAC6B;AACzB,MAAA;AACH,UAAMA,qBAAoB;AAAA,MACzB,cAAc;AAAA,QACb,CAAC,YAAY,GAAG;AAAA,MAChB;AAAA,MACD,KAAK;AAAA,IAAA,CACL;AAAA,WACO;AAGR,UAAMA,qBAAoB;AAAA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AQ,QAAQ;AAAA,IACtD,QAAQ,QAAQ;AAAA,EAAA,CAChB;AACF;AAEO,MAAM,cAA8C,OAC1D,EAAE,qBAAqB,qBAAA,GACvB,YACG;AAEF,oBAAA,MAAM,QAAQ,WAAW;AAAA,IACxB,oBAAoB,EAAE,qBAAqB,sBAAsB;AAAA,IACjE,uBAAuB,OAAO;AAAA,IAC9B,yBAAyB,OAAO;AAAA,IAChC,yBAAyB,OAAO;AAAA,EAChC,CAAA,CAAC;AAEJ;"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const checkPathExists = require("./checkPathExists.cjs");
4
+ async function checkHasSrcDirectory(args) {
5
+ return await checkPathExists.checkPathExists(args.helpers.joinPathFromRoot("src"));
6
+ }
7
+ exports.checkHasSrcDirectory = checkHasSrcDirectory;
8
+ //# sourceMappingURL=checkHasSrcDirectory.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkHasSrcDirectory.cjs","sources":["../../../src/lib/checkHasSrcDirectory.ts"],"sourcesContent":["import { SliceMachineContext } from \"@slicemachine/plugin-kit\";\n\nimport { PluginOptions } from \"../types\";\nimport { checkPathExists } from \"./checkPathExists\";\n\ntype CheckHasSrcDirectoryArgs = Pick<\n\tSliceMachineContext<PluginOptions>,\n\t\"helpers\"\n>;\n\nexport async function checkHasSrcDirectory(\n\targs: CheckHasSrcDirectoryArgs,\n): Promise<boolean> {\n\treturn await checkPathExists(args.helpers.joinPathFromRoot(\"src\"));\n}\n"],"names":["checkPathExists"],"mappings":";;;AAUA,eAAsB,qBACrB,MAA8B;AAE9B,SAAO,MAAMA,gBAAgB,gBAAA,KAAK,QAAQ,iBAAiB,KAAK,CAAC;AAClE;;"}
@@ -0,0 +1,5 @@
1
+ import { SliceMachineContext } from "@slicemachine/plugin-kit";
2
+ import { PluginOptions } from "../types";
3
+ type CheckHasSrcDirectoryArgs = Pick<SliceMachineContext<PluginOptions>, "helpers">;
4
+ export declare function checkHasSrcDirectory(args: CheckHasSrcDirectoryArgs): Promise<boolean>;
5
+ export {};
@@ -0,0 +1,8 @@
1
+ import { checkPathExists } from "./checkPathExists.js";
2
+ async function checkHasSrcDirectory(args) {
3
+ return await checkPathExists(args.helpers.joinPathFromRoot("src"));
4
+ }
5
+ export {
6
+ checkHasSrcDirectory
7
+ };
8
+ //# sourceMappingURL=checkHasSrcDirectory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkHasSrcDirectory.js","sources":["../../../src/lib/checkHasSrcDirectory.ts"],"sourcesContent":["import { SliceMachineContext } from \"@slicemachine/plugin-kit\";\n\nimport { PluginOptions } from \"../types\";\nimport { checkPathExists } from \"./checkPathExists\";\n\ntype CheckHasSrcDirectoryArgs = Pick<\n\tSliceMachineContext<PluginOptions>,\n\t\"helpers\"\n>;\n\nexport async function checkHasSrcDirectory(\n\targs: CheckHasSrcDirectoryArgs,\n): Promise<boolean> {\n\treturn await checkPathExists(args.helpers.joinPathFromRoot(\"src\"));\n}\n"],"names":[],"mappings":";AAUA,eAAsB,qBACrB,MAA8B;AAE9B,SAAO,MAAM,gBAAgB,KAAK,QAAQ,iBAAiB,KAAK,CAAC;AAClE;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slicemachine/adapter-nuxt",
3
- "version": "0.3.10-dev-next-release.2",
3
+ "version": "0.3.10-dev-next-release.3",
4
4
  "description": "Slice Machine adapter for Nuxt 3.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -61,7 +61,7 @@
61
61
  "dependencies": {
62
62
  "@prismicio/simulator": "^0.1.4",
63
63
  "@prismicio/types-internal": "^2.0.0",
64
- "@slicemachine/plugin-kit": "^0.4.10-dev-next-release.2",
64
+ "@slicemachine/plugin-kit": "^0.4.10-dev-next-release.3",
65
65
  "common-tags": "^1.8.2",
66
66
  "fp-ts": "^2.13.1",
67
67
  "fs-extra": "^11.1.0",
@@ -105,5 +105,5 @@
105
105
  "engines": {
106
106
  "node": ">=14.15.0"
107
107
  },
108
- "gitHead": "4cffa7b380ea81e13115f879ea8cc5b4974e8859"
108
+ "gitHead": "eecb326986278132db9083830f7596e3efd047ec"
109
109
  }
@@ -11,6 +11,7 @@ import { loadFile, writeFile, type ASTNode } from "magicast";
11
11
  import { checkPathExists } from "../lib/checkPathExists";
12
12
  import { rejectIfNecessary } from "../lib/rejectIfNecessary";
13
13
  import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
14
+ import { checkHasSrcDirectory } from "../lib/checkHasSrcDirectory";
14
15
 
15
16
  import type { PluginOptions } from "../types";
16
17
 
@@ -149,15 +150,57 @@ const createSliceSimulatorPage = async ({
149
150
  await fs.writeFile(filePath, contents);
150
151
  };
151
152
 
153
+ const modifySliceMachineConfig = async ({
154
+ helpers,
155
+ options,
156
+ }: SliceMachineContext<PluginOptions>) => {
157
+ const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
158
+ const project = await helpers.getProject();
159
+
160
+ // Add Slice Simulator URL.
161
+ project.config.localSliceSimulatorURL ||=
162
+ "http://localhost:3000/slice-simulator";
163
+
164
+ // Nest the default Slice Library in the src directory if it exists and
165
+ // is empty.
166
+ if (
167
+ hasSrcDirectory &&
168
+ JSON.stringify(project.config.libraries) === JSON.stringify(["./slices"])
169
+ ) {
170
+ try {
171
+ const entries = await fs.readdir(helpers.joinPathFromRoot("slices"));
172
+
173
+ if (!entries.map((entry) => path.parse(entry).name).includes("index")) {
174
+ project.config.libraries = ["./src/slices"];
175
+ }
176
+ } catch (error) {
177
+ if (
178
+ error instanceof Error &&
179
+ "code" in error &&
180
+ error.code === "ENOENT"
181
+ ) {
182
+ // The directory does not exist, which means we
183
+ // can safely nest the library.
184
+ project.config.libraries = ["./src/slices"];
185
+ }
186
+ }
187
+ }
188
+
189
+ await helpers.updateSliceMachineConfig(project.config, {
190
+ format: options.format,
191
+ });
192
+ };
193
+
152
194
  export const projectInit: ProjectInitHook<PluginOptions> = async (
153
195
  { installDependencies: _installDependencies },
154
196
  context,
155
197
  ) => {
156
198
  rejectIfNecessary(
157
199
  await Promise.allSettled([
158
- await installDependencies({ installDependencies: _installDependencies }),
159
- await configurePrismicModule(context),
160
- await createSliceSimulatorPage(context),
200
+ installDependencies({ installDependencies: _installDependencies }),
201
+ configurePrismicModule(context),
202
+ createSliceSimulatorPage(context),
203
+ modifySliceMachineConfig(context),
161
204
  ]),
162
205
  );
163
206
  };
@@ -0,0 +1,15 @@
1
+ import { SliceMachineContext } from "@slicemachine/plugin-kit";
2
+
3
+ import { PluginOptions } from "../types";
4
+ import { checkPathExists } from "./checkPathExists";
5
+
6
+ type CheckHasSrcDirectoryArgs = Pick<
7
+ SliceMachineContext<PluginOptions>,
8
+ "helpers"
9
+ >;
10
+
11
+ export async function checkHasSrcDirectory(
12
+ args: CheckHasSrcDirectoryArgs,
13
+ ): Promise<boolean> {
14
+ return await checkPathExists(args.helpers.joinPathFromRoot("src"));
15
+ }