@prismicio/adapter-nuxt 0.0.3-beta.19 → 0.0.3-beta.20

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.
@@ -1,13 +1,21 @@
1
1
  import * as path from "node:path";
2
- import { checkHasProjectFile, readProjectFile, writeProjectFile, deleteProjectFile } from "@prismicio/plugin-kit/fs";
2
+ import { checkHasProjectFile, writeProjectFile, readProjectFile, deleteProjectFile } from "@prismicio/plugin-kit/fs";
3
+ import { stripIndent } from "common-tags";
3
4
  import { loadFile, builders, writeFile } from "magicast";
4
5
  import { buildSrcPath } from "../lib/buildSrcPath.js";
6
+ import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject.js";
5
7
  import { rejectIfNecessary } from "../lib/rejectIfNecessary.js";
8
+ import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile.js";
9
+ var __freeze = Object.freeze;
10
+ var __defProp = Object.defineProperty;
11
+ var __template = (cooked, raw) => __freeze(__defProp(cooked, "raw", { value: __freeze(cooked.slice()) }));
12
+ var _a;
6
13
  const NUXT_PRISMIC = "@nuxtjs/prismic";
7
14
  const installDependencies = async ({ installDependencies: installDependencies2 }) => {
8
15
  await installDependencies2({
9
16
  dependencies: {
10
- [NUXT_PRISMIC]: "^4.0.0"
17
+ [NUXT_PRISMIC]: "latest",
18
+ "@prismicio/client": "latest"
11
19
  },
12
20
  dev: true
13
21
  });
@@ -38,19 +46,60 @@ const configurePrismicModule = async ({ helpers }) => {
38
46
  config.modules.push(NUXT_PRISMIC);
39
47
  }
40
48
  if (!hasInlinedConfiguration) {
41
- mod.imports.$add({
42
- from: "./prismic.config.json",
43
- imported: "apiEndpoint"
44
- });
45
- mod.imports.$add({
49
+ mod.imports.$prepend({
46
50
  from: "./prismic.config.json",
47
51
  imported: "repositoryName"
48
52
  });
49
53
  config.prismic ||= {};
50
- config.prismic.endpoint = builders.raw("apiEndpoint || repositoryName");
54
+ config.prismic.endpoint = builders.raw("repositoryName");
51
55
  }
52
56
  await writeFile(mod, nuxtConfigPath);
53
57
  };
58
+ const createSliceSimulatorPage = async ({ helpers, options }) => {
59
+ const isTypeScriptProject = await checkIsTypeScriptProject({
60
+ helpers,
61
+ options
62
+ });
63
+ const appPagesDirectoryExists = await checkHasProjectFile({
64
+ filename: "app/pages",
65
+ helpers
66
+ });
67
+ const srcPagesDirectoryExists = await checkHasProjectFile({
68
+ filename: "src/pages",
69
+ helpers
70
+ });
71
+ const pagesDirectoryExists = await checkHasProjectFile({
72
+ filename: "pages",
73
+ helpers
74
+ });
75
+ let filename;
76
+ if (appPagesDirectoryExists) {
77
+ filename = path.join("app/pages", "slice-simulator.vue");
78
+ } else if (srcPagesDirectoryExists) {
79
+ filename = path.join("src/pages", "slice-simulator.vue");
80
+ } else if (pagesDirectoryExists) {
81
+ filename = path.join("pages", "slice-simulator.vue");
82
+ } else {
83
+ filename = await buildSrcPath({
84
+ filename: path.join("pages", "slice-simulator.vue"),
85
+ helpers
86
+ });
87
+ }
88
+ if (await checkHasProjectFile({ filename, helpers })) {
89
+ return;
90
+ }
91
+ const scriptAttributes = ["setup"];
92
+ if (isTypeScriptProject) {
93
+ scriptAttributes.push('lang="ts"');
94
+ }
95
+ const contents = stripIndent(_a || (_a = __template(["\n <script ", '>\n import { components } from "~/slices";\n <\/script>\n\n <template>\n <SliceSimulator #default="{ slices }">\n <SliceZone :slices="slices" :components="components" />\n </SliceSimulator>\n </template>\n '])), scriptAttributes.join(" "));
96
+ await writeProjectFile({
97
+ filename,
98
+ contents,
99
+ format: options.format,
100
+ helpers
101
+ });
102
+ };
54
103
  const moveOrDeleteAppVue = async ({ helpers, options }) => {
55
104
  const filenameAppVue = await buildSrcPath({ filename: "app.vue", helpers });
56
105
  if (!await checkHasProjectFile({ filename: filenameAppVue, helpers })) {
@@ -103,13 +152,24 @@ const modifyPrismicConfig = async ({ helpers, options, actions }) => {
103
152
  format: options.format
104
153
  });
105
154
  };
155
+ const upsertSliceLibraryIndexFiles = async (context) => {
156
+ const project = await context.helpers.getProject();
157
+ if (!project.config.libraries) {
158
+ return;
159
+ }
160
+ await Promise.all(project.config.libraries.map(async (libraryID) => {
161
+ await upsertSliceLibraryIndexFile({ libraryID, ...context });
162
+ }));
163
+ };
106
164
  const projectInit = async ({ installDependencies: _installDependencies }, context) => {
107
165
  rejectIfNecessary(await Promise.allSettled([
108
166
  installDependencies({ installDependencies: _installDependencies }),
109
167
  configurePrismicModule(context),
168
+ createSliceSimulatorPage(context),
110
169
  moveOrDeleteAppVue(context),
111
170
  modifyPrismicConfig(context)
112
171
  ]));
172
+ await upsertSliceLibraryIndexFiles(context);
113
173
  };
114
174
  export {
115
175
  projectInit
@@ -1 +1 @@
1
- {"version":3,"file":"project-init.js","sources":["../../../src/hooks/project-init.ts"],"sourcesContent":["import * as path from \"node:path\";\n\nimport type {\n\tProjectInitHook,\n\tProjectInitHookData,\n\tPluginSystemContext,\n} from \"@prismicio/plugin-kit\";\nimport {\n\tcheckHasProjectFile,\n\tdeleteProjectFile,\n\treadProjectFile,\n\twriteProjectFile,\n} from \"@prismicio/plugin-kit/fs\";\nimport { builders, loadFile, writeFile } from \"magicast\";\n\nimport { buildSrcPath } from \"../lib/buildSrcPath\";\nimport { rejectIfNecessary } from \"../lib/rejectIfNecessary\";\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\tawait installDependencies({\n\t\tdependencies: {\n\t\t\t[NUXT_PRISMIC]: \"^4.0.0\",\n\t\t},\n\t\tdev: true,\n\t});\n};\n\ntype ConfigurePrismicModuleArgs = PluginSystemContext<PluginOptions>;\n\nconst configurePrismicModule = async ({\n\thelpers,\n}: ConfigurePrismicModuleArgs) => {\n\tlet nuxtConfigFilename = \"nuxt.config.js\";\n\n\tif (!(await checkHasProjectFile({ filename: nuxtConfigFilename, helpers }))) {\n\t\tnuxtConfigFilename = \"nuxt.config.ts\";\n\n\t\t// nuxt.config.* not found\n\t\tif (\n\t\t\t!(await checkHasProjectFile({ filename: nuxtConfigFilename, helpers }))\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst nuxtConfigPath = helpers.joinPathFromRoot(nuxtConfigFilename);\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\tif (!hasInlinedConfiguration) {\n\t\t// Import Prismic configuration\n\t\tmod.imports.$add({\n\t\t\tfrom: \"./prismic.config.json\",\n\t\t\timported: \"apiEndpoint\",\n\t\t});\n\t\tmod.imports.$add({\n\t\t\tfrom: \"./prismic.config.json\",\n\t\t\timported: \"repositoryName\",\n\t\t});\n\n\t\t// Add inline configuration\n\t\tconfig.prismic ||= {};\n\t\tconfig.prismic.endpoint = builders.raw(\"apiEndpoint || repositoryName\");\n\t}\n\n\tawait writeFile(mod, nuxtConfigPath);\n};\n\nconst moveOrDeleteAppVue = async ({\n\thelpers,\n\toptions,\n}: PluginSystemContext<PluginOptions>) => {\n\tconst filenameAppVue = await buildSrcPath({ filename: \"app.vue\", helpers });\n\n\t// If there's no `app.vue`, there's nothing to do.\n\tif (!(await checkHasProjectFile({ filename: filenameAppVue, helpers }))) {\n\t\treturn;\n\t}\n\n\tconst filecontentAppVue = await readProjectFile({\n\t\tfilename: filenameAppVue,\n\t\thelpers,\n\t\tencoding: \"utf-8\",\n\t});\n\n\t// We check for app.vue to contain Nuxt default welcome component to determine\n\t// if we need to consider it as the default one or not.\n\tif (!filecontentAppVue.includes(\"<NuxtWelcome\")) {\n\t\treturn;\n\t}\n\n\tconst filenameIndexVue = await buildSrcPath({\n\t\tfilename: path.join(\"pages/index.vue\"),\n\t\thelpers,\n\t});\n\n\t// If we don't have an `index.vue` we create one with the content of `app.vue`\n\tif (!(await checkHasProjectFile({ filename: filenameIndexVue, helpers }))) {\n\t\tawait writeProjectFile({\n\t\t\tfilename: filenameIndexVue,\n\t\t\tcontents: filecontentAppVue,\n\t\t\tformat: options.format,\n\t\t\thelpers,\n\t\t});\n\t}\n\n\t// Delete `app.vue`\n\tawait deleteProjectFile({\n\t\tfilename: filenameAppVue,\n\t\thelpers,\n\t});\n};\n\nconst modifyPrismicConfig = async ({\n\thelpers,\n\toptions,\n\tactions,\n}: PluginSystemContext<PluginOptions>) => {\n\tconst hasAppDirectory = await checkHasProjectFile({\n\t\tfilename: \"app\",\n\t\thelpers,\n\t});\n\tconst hasSrcDirectory = await checkHasProjectFile({\n\t\tfilename: \"src\",\n\t\thelpers,\n\t});\n\tconst project = await helpers.getProject();\n\n\t// Nest the default Slice Library in the `app` or `src` directory if it\n\t// exists and is empty.\n\tif (\n\t\t(hasAppDirectory || hasSrcDirectory) &&\n\t\tproject.config.libraries &&\n\t\tJSON.stringify(project.config.libraries) === JSON.stringify([\"./slices\"])\n\t) {\n\t\tconst sliceLibrary = await actions.readSliceLibrary({\n\t\t\tlibraryID: project.config.libraries[0],\n\t\t});\n\n\t\tif (sliceLibrary.sliceIDs.length < 1) {\n\t\t\tproject.config.libraries = hasAppDirectory\n\t\t\t\t? [\"./app/slices\"]\n\t\t\t\t: [\"./src/slices\"];\n\t\t}\n\t}\n\n\tawait helpers.updatePrismicConfig(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\tmoveOrDeleteAppVue(context),\n\t\t\tmodifyPrismicConfig(context),\n\t\t]),\n\t);\n};\n"],"names":["installDependencies"],"mappings":";;;;;AAmBA,MAAM,eAAe;AAMrB,MAAM,sBAAsB,OAAO,EAClC,qBAAAA,2BAC6B;AAC7B,QAAMA,qBAAoB;AAAA,IACzB,cAAc;AAAA,MACb,CAAC,YAAY,GAAG;AAAA,IAAA;AAAA,IAEjB,KAAK;AAAA,EAAA,CACL;AACF;AAIA,MAAM,yBAAyB,OAAO,EACrC,cACgC;AAChC,MAAI,qBAAqB;AAEzB,MAAI,CAAE,MAAM,oBAAoB,EAAE,UAAU,oBAAoB,QAAA,CAAS,GAAI;AAC5E,yBAAqB;AAGrB,QACC,CAAE,MAAM,oBAAoB,EAAE,UAAU,oBAAoB,QAAA,CAAS,GACpE;AACD;AAAA,IACD;AAAA,EACD;AAEA,QAAM,iBAAiB,QAAQ,iBAAiB,kBAAkB;AAElE,QAAM,MAAM,MAAM,SAAS,cAAc;AACzC,QAAM,SACL,IAAI,QAAQ,QAAQ,UAAU,kBAC3B,IAAI,QAAQ,QAAQ,MAAM,CAAC,IAC3B,IAAI,QAAQ;AAGhB,MAAI,0BAA0B;AAC9B,QAAM,8BAA8B,OAAO,WAAW,CAAA,GAAI,KACzD,CAAC,iBAA4C;AAC5C,QAAI,OAAO,iBAAiB,UAAU;AACrC,aAAO,iBAAiB;AAAA,IACzB,WAAW,MAAM,QAAQ,YAAY,GAAG;AACvC,gCAA0B,CAAC,CAAC,aAAa,CAAC;A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AAM,kBAAkB,MAAM,oBAAoB;AAAA,IACjD,UAAU;AAAA,IACV;AAAA,EAAA,CACA;AACD,QAAM,kBAAkB,MAAM,oBAAoB;AAAA,IACjD,UAAU;AAAA,IACV;AAAA,EAAA,CACA;AACD,QAAM,UAAU,MAAM,QAAQ,WAAA;AAI9B,OACE,mBAAmB,oBACpB,QAAQ,OAAO,aACf,KAAK,UAAU,QAAQ,OAAO,SAAS,MAAM,KAAK,UAAU,CAAC,UAAU,CAAC,GACvE;AACD,UAAM,eAAe,MAAM,QAAQ,iBAAiB;AAAA,MACnD,WAAW,QAAQ,OAAO,UAAU,CAAC;AAAA,IAAA,CACrC;AAED,QAAI,aAAa,SAAS,SAAS,GAAG;AACrC,cAAQ,OAAO,YAAY,kBACxB,CAAC,cAAc,IACf,CAAC,cAAc;AAAA,IACnB;AAAA,EACD;AAEA,QAAM,QAAQ,oBAAoB,QAAQ,QAAQ;AAAA,IACjD,QAAQ,QAAQ;AAAA,EAAA,CAChB;AACF;AAEO,MAAM,cAA8C,OAC1D,EAAE,qBAAqB,qBAAA,GACvB,YACG;AACH,oBACC,MAAM,QAAQ,WAAW;AAAA,IACxB,oBAAoB,EAAE,qBAAqB,sBAAsB;AAAA,IACjE,uBAAuB,OAAO;AAAA,IAC9B,mBAAmB,OAAO;AAAA,IAC1B,oBAAoB,OAAO;AAAA,EAAA,CAC3B,CAAC;AAEJ;"}
1
+ {"version":3,"file":"project-init.js","sources":["../../../src/hooks/project-init.ts"],"sourcesContent":["import * as path from \"node:path\";\n\nimport type {\n\tProjectInitHook,\n\tProjectInitHookData,\n\tPluginSystemContext,\n} from \"@prismicio/plugin-kit\";\nimport {\n\tcheckHasProjectFile,\n\tdeleteProjectFile,\n\treadProjectFile,\n\twriteProjectFile,\n} from \"@prismicio/plugin-kit/fs\";\nimport { stripIndent } from \"common-tags\";\nimport { builders, loadFile, writeFile } from \"magicast\";\n\nimport { buildSrcPath } from \"../lib/buildSrcPath\";\nimport { checkIsTypeScriptProject } from \"../lib/checkIsTypeScriptProject\";\nimport { rejectIfNecessary } from \"../lib/rejectIfNecessary\";\nimport { upsertSliceLibraryIndexFile } from \"../lib/upsertSliceLibraryIndexFile\";\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\tawait installDependencies({\n\t\tdependencies: {\n\t\t\t[NUXT_PRISMIC]: \"latest\",\n\t\t\t\"@prismicio/client\": \"latest\",\n\t\t},\n\t\tdev: true,\n\t});\n};\n\ntype ConfigurePrismicModuleArgs = PluginSystemContext<PluginOptions>;\n\nconst configurePrismicModule = async ({\n\thelpers,\n}: ConfigurePrismicModuleArgs) => {\n\tlet nuxtConfigFilename = \"nuxt.config.js\";\n\n\tif (!(await checkHasProjectFile({ filename: nuxtConfigFilename, helpers }))) {\n\t\tnuxtConfigFilename = \"nuxt.config.ts\";\n\n\t\t// nuxt.config.* not found\n\t\tif (\n\t\t\t!(await checkHasProjectFile({ filename: nuxtConfigFilename, helpers }))\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst nuxtConfigPath = helpers.joinPathFromRoot(nuxtConfigFilename);\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\tif (!hasInlinedConfiguration) {\n\t\t// Import Prismic configuration\n\t\tmod.imports.$prepend({\n\t\t\tfrom: \"./prismic.config.json\",\n\t\t\timported: \"repositoryName\",\n\t\t});\n\n\t\t// Add inline configuration\n\t\tconfig.prismic ||= {};\n\t\tconfig.prismic.endpoint = builders.raw(\"repositoryName\");\n\t}\n\n\tawait writeFile(mod, nuxtConfigPath);\n};\n\ntype CreateSliceSimulatorPageArgs = PluginSystemContext<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 appPagesDirectoryExists = await checkHasProjectFile({\n\t\tfilename: \"app/pages\",\n\t\thelpers,\n\t});\n\n\tconst srcPagesDirectoryExists = await checkHasProjectFile({\n\t\tfilename: \"src/pages\",\n\t\thelpers,\n\t});\n\n\tconst pagesDirectoryExists = await checkHasProjectFile({\n\t\tfilename: \"pages\",\n\t\thelpers,\n\t});\n\n\tlet filename: string;\n\t// We first give priority to existing `pages` directory, then to `srcDir`\n\t// because there could be conflicts with legacy `app` directory.\n\tif (appPagesDirectoryExists) {\n\t\tfilename = path.join(\"app/pages\", \"slice-simulator.vue\");\n\t} else if (srcPagesDirectoryExists) {\n\t\tfilename = path.join(\"src/pages\", \"slice-simulator.vue\");\n\t} else if (pagesDirectoryExists) {\n\t\tfilename = path.join(\"pages\", \"slice-simulator.vue\");\n\t} else {\n\t\tfilename = await buildSrcPath({\n\t\t\tfilename: path.join(\"pages\", \"slice-simulator.vue\"),\n\t\t\thelpers,\n\t\t});\n\t}\n\n\tif (await checkHasProjectFile({ filename, helpers })) {\n\t\treturn;\n\t}\n\n\tconst scriptAttributes = [\"setup\"];\n\tif (isTypeScriptProject) {\n\t\tscriptAttributes.push('lang=\"ts\"');\n\t}\n\n\tconst contents = stripIndent`\n\t\t<script ${scriptAttributes.join(\" \")}>\n\t\timport { components } from \"~/slices\";\n\t\t</script>\n\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\t`;\n\n\tawait writeProjectFile({\n\t\tfilename,\n\t\tcontents,\n\t\tformat: options.format,\n\t\thelpers,\n\t});\n};\n\nconst moveOrDeleteAppVue = async ({\n\thelpers,\n\toptions,\n}: PluginSystemContext<PluginOptions>) => {\n\tconst filenameAppVue = await buildSrcPath({ filename: \"app.vue\", helpers });\n\n\t// If there's no `app.vue`, there's nothing to do.\n\tif (!(await checkHasProjectFile({ filename: filenameAppVue, helpers }))) {\n\t\treturn;\n\t}\n\n\tconst filecontentAppVue = await readProjectFile({\n\t\tfilename: filenameAppVue,\n\t\thelpers,\n\t\tencoding: \"utf-8\",\n\t});\n\n\t// We check for app.vue to contain Nuxt default welcome component to determine\n\t// if we need to consider it as the default one or not.\n\tif (!filecontentAppVue.includes(\"<NuxtWelcome\")) {\n\t\treturn;\n\t}\n\n\tconst filenameIndexVue = await buildSrcPath({\n\t\tfilename: path.join(\"pages/index.vue\"),\n\t\thelpers,\n\t});\n\n\t// If we don't have an `index.vue` we create one with the content of `app.vue`\n\tif (!(await checkHasProjectFile({ filename: filenameIndexVue, helpers }))) {\n\t\tawait writeProjectFile({\n\t\t\tfilename: filenameIndexVue,\n\t\t\tcontents: filecontentAppVue,\n\t\t\tformat: options.format,\n\t\t\thelpers,\n\t\t});\n\t}\n\n\t// Delete `app.vue`\n\tawait deleteProjectFile({\n\t\tfilename: filenameAppVue,\n\t\thelpers,\n\t});\n};\n\nconst modifyPrismicConfig = async ({\n\thelpers,\n\toptions,\n\tactions,\n}: PluginSystemContext<PluginOptions>) => {\n\tconst hasAppDirectory = await checkHasProjectFile({\n\t\tfilename: \"app\",\n\t\thelpers,\n\t});\n\tconst hasSrcDirectory = await checkHasProjectFile({\n\t\tfilename: \"src\",\n\t\thelpers,\n\t});\n\tconst project = await helpers.getProject();\n\n\t// Nest the default Slice Library in the `app` or `src` directory if it\n\t// exists and is empty.\n\tif (\n\t\t(hasAppDirectory || hasSrcDirectory) &&\n\t\tproject.config.libraries &&\n\t\tJSON.stringify(project.config.libraries) === JSON.stringify([\"./slices\"])\n\t) {\n\t\tconst sliceLibrary = await actions.readSliceLibrary({\n\t\t\tlibraryID: project.config.libraries[0],\n\t\t});\n\n\t\tif (sliceLibrary.sliceIDs.length < 1) {\n\t\t\tproject.config.libraries = hasAppDirectory\n\t\t\t\t? [\"./app/slices\"]\n\t\t\t\t: [\"./src/slices\"];\n\t\t}\n\t}\n\n\tawait helpers.updatePrismicConfig(project.config, {\n\t\tformat: options.format,\n\t});\n};\n\nconst upsertSliceLibraryIndexFiles = async (\n\tcontext: PluginSystemContext<PluginOptions>,\n) => {\n\t// We must use the `getProject()` helper to get the latest version of\n\t// the project config. The config may have been modified in\n\t// `modifyPrismicConfig()` and will not be reflected in\n\t// `context.project`.\n\tconst project = await context.helpers.getProject();\n\n\tif (!project.config.libraries) {\n\t\treturn;\n\t}\n\n\tawait Promise.all(\n\t\tproject.config.libraries.map(async (libraryID) => {\n\t\t\tawait upsertSliceLibraryIndexFile({ libraryID, ...context });\n\t\t}),\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\tmoveOrDeleteAppVue(context),\n\t\t\tmodifyPrismicConfig(context),\n\t\t]),\n\t);\n\n\t// This must happen after `modifyPrismicConfig()` since the\n\t// location of the default Slice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EA,QAAM,QAAQ,oBAAoB,QAAQ,QAAQ;AAAA,IACjD,QAAQ,QAAQ;AAAA,EAAA,CAChB;AACF;AAEA,MAAM,+BAA+B,OACpC,YACG;AAKH,QAAM,UAAU,MAAM,QAAQ,QAAQ,WAAA;AAEtC,MAAI,CAAC,QAAQ,OAAO,WAAW;AAC9B;AAAA,EACD;AAEA,QAAM,QAAQ,IACb,QAAQ,OAAO,UAAU,IAAI,OAAO,cAAa;AAChD,UAAM,4BAA4B,EAAE,WAAW,GAAG,SAAS;AAAA,EAC5D,CAAC,CAAC;AAEJ;AAEO,MAAM,cAA8C,OAC1D,EAAE,qBAAqB,qBAAA,GACvB,YACG;AACH,oBACC,MAAM,QAAQ,WAAW;AAAA,IACxB,oBAAoB,EAAE,qBAAqB,sBAAsB;AAAA,IACjE,uBAAuB,OAAO;AAAA,IAC9B,yBAAyB,OAAO;AAAA,IAChC,mBAAmB,OAAO;AAAA,IAC1B,oBAAoB,OAAO;AAAA,EAAA,CAC3B,CAAC;AAKH,QAAM,6BAA6B,OAAO;AAC3C;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/adapter-nuxt",
3
- "version": "0.0.3-beta.19",
3
+ "version": "0.0.3-beta.20",
4
4
  "description": "Prismic adapter for Nuxt.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -52,7 +52,7 @@
52
52
  "audit": "yarn npm audit --environment production --severity high"
53
53
  },
54
54
  "dependencies": {
55
- "@prismicio/plugin-kit": "0.0.3-beta.19",
55
+ "@prismicio/plugin-kit": "0.0.3-beta.20",
56
56
  "change-case": "5.4.4",
57
57
  "common-tags": "1.8.2",
58
58
  "magicast": "0.5.1"
@@ -11,10 +11,13 @@ import {
11
11
  readProjectFile,
12
12
  writeProjectFile,
13
13
  } from "@prismicio/plugin-kit/fs";
14
+ import { stripIndent } from "common-tags";
14
15
  import { builders, loadFile, writeFile } from "magicast";
15
16
 
16
17
  import { buildSrcPath } from "../lib/buildSrcPath";
18
+ import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
17
19
  import { rejectIfNecessary } from "../lib/rejectIfNecessary";
20
+ import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
18
21
  import type { PluginOptions } from "../types";
19
22
 
20
23
  const NUXT_PRISMIC = "@nuxtjs/prismic";
@@ -28,7 +31,8 @@ const installDependencies = async ({
28
31
  }: InstallDependenciesArgs) => {
29
32
  await installDependencies({
30
33
  dependencies: {
31
- [NUXT_PRISMIC]: "^4.0.0",
34
+ [NUXT_PRISMIC]: "latest",
35
+ "@prismicio/client": "latest",
32
36
  },
33
37
  dev: true,
34
38
  });
@@ -84,23 +88,90 @@ const configurePrismicModule = async ({
84
88
  // Append Prismic module configuration
85
89
  if (!hasInlinedConfiguration) {
86
90
  // Import Prismic configuration
87
- mod.imports.$add({
88
- from: "./prismic.config.json",
89
- imported: "apiEndpoint",
90
- });
91
- mod.imports.$add({
91
+ mod.imports.$prepend({
92
92
  from: "./prismic.config.json",
93
93
  imported: "repositoryName",
94
94
  });
95
95
 
96
96
  // Add inline configuration
97
97
  config.prismic ||= {};
98
- config.prismic.endpoint = builders.raw("apiEndpoint || repositoryName");
98
+ config.prismic.endpoint = builders.raw("repositoryName");
99
99
  }
100
100
 
101
101
  await writeFile(mod, nuxtConfigPath);
102
102
  };
103
103
 
104
+ type CreateSliceSimulatorPageArgs = PluginSystemContext<PluginOptions>;
105
+
106
+ const createSliceSimulatorPage = async ({
107
+ helpers,
108
+ options,
109
+ }: CreateSliceSimulatorPageArgs) => {
110
+ const isTypeScriptProject = await checkIsTypeScriptProject({
111
+ helpers,
112
+ options,
113
+ });
114
+
115
+ const appPagesDirectoryExists = await checkHasProjectFile({
116
+ filename: "app/pages",
117
+ helpers,
118
+ });
119
+
120
+ const srcPagesDirectoryExists = await checkHasProjectFile({
121
+ filename: "src/pages",
122
+ helpers,
123
+ });
124
+
125
+ const pagesDirectoryExists = await checkHasProjectFile({
126
+ filename: "pages",
127
+ helpers,
128
+ });
129
+
130
+ let filename: string;
131
+ // We first give priority to existing `pages` directory, then to `srcDir`
132
+ // because there could be conflicts with legacy `app` directory.
133
+ if (appPagesDirectoryExists) {
134
+ filename = path.join("app/pages", "slice-simulator.vue");
135
+ } else if (srcPagesDirectoryExists) {
136
+ filename = path.join("src/pages", "slice-simulator.vue");
137
+ } else if (pagesDirectoryExists) {
138
+ filename = path.join("pages", "slice-simulator.vue");
139
+ } else {
140
+ filename = await buildSrcPath({
141
+ filename: path.join("pages", "slice-simulator.vue"),
142
+ helpers,
143
+ });
144
+ }
145
+
146
+ if (await checkHasProjectFile({ filename, helpers })) {
147
+ return;
148
+ }
149
+
150
+ const scriptAttributes = ["setup"];
151
+ if (isTypeScriptProject) {
152
+ scriptAttributes.push('lang="ts"');
153
+ }
154
+
155
+ const contents = stripIndent`
156
+ <script ${scriptAttributes.join(" ")}>
157
+ import { components } from "~/slices";
158
+ </script>
159
+
160
+ <template>
161
+ <SliceSimulator #default="{ slices }">
162
+ <SliceZone :slices="slices" :components="components" />
163
+ </SliceSimulator>
164
+ </template>
165
+ `;
166
+
167
+ await writeProjectFile({
168
+ filename,
169
+ contents,
170
+ format: options.format,
171
+ helpers,
172
+ });
173
+ };
174
+
104
175
  const moveOrDeleteAppVue = async ({
105
176
  helpers,
106
177
  options,
@@ -184,6 +255,26 @@ const modifyPrismicConfig = async ({
184
255
  });
185
256
  };
186
257
 
258
+ const upsertSliceLibraryIndexFiles = async (
259
+ context: PluginSystemContext<PluginOptions>,
260
+ ) => {
261
+ // We must use the `getProject()` helper to get the latest version of
262
+ // the project config. The config may have been modified in
263
+ // `modifyPrismicConfig()` and will not be reflected in
264
+ // `context.project`.
265
+ const project = await context.helpers.getProject();
266
+
267
+ if (!project.config.libraries) {
268
+ return;
269
+ }
270
+
271
+ await Promise.all(
272
+ project.config.libraries.map(async (libraryID) => {
273
+ await upsertSliceLibraryIndexFile({ libraryID, ...context });
274
+ }),
275
+ );
276
+ };
277
+
187
278
  export const projectInit: ProjectInitHook<PluginOptions> = async (
188
279
  { installDependencies: _installDependencies },
189
280
  context,
@@ -192,8 +283,13 @@ export const projectInit: ProjectInitHook<PluginOptions> = async (
192
283
  await Promise.allSettled([
193
284
  installDependencies({ installDependencies: _installDependencies }),
194
285
  configurePrismicModule(context),
286
+ createSliceSimulatorPage(context),
195
287
  moveOrDeleteAppVue(context),
196
288
  modifyPrismicConfig(context),
197
289
  ]),
198
290
  );
291
+
292
+ // This must happen after `modifyPrismicConfig()` since the
293
+ // location of the default Slice library may change.
294
+ await upsertSliceLibraryIndexFiles(context);
199
295
  };