prismic 1.1.0 → 1.2.0
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.
- package/dist/{builders-hKD4IrLX-ClhMQQkN.mjs → builders-hKD4IrLX-BrpqCAS2.mjs} +1 -1
- package/dist/index.mjs +115 -101
- package/dist/nextjs-HiDO_p-p.mjs +318 -0
- package/dist/nuxt-CDrqbn0o.mjs +59 -0
- package/dist/string-BUjs_2AH.mjs +7 -0
- package/dist/{sveltekit-BMDXAfYz.mjs → sveltekit-Qx8xJZOd.mjs} +45 -35
- package/package.json +1 -1
- package/src/adapters/index.ts +177 -0
- package/src/{frameworks → adapters}/nextjs.templates.ts +102 -102
- package/src/adapters/nextjs.ts +211 -0
- package/src/adapters/nuxt.ts +232 -0
- package/src/adapters/sveltekit.ts +226 -0
- package/src/clients/core.ts +104 -0
- package/src/clients/wroom.ts +111 -0
- package/src/commands/init.ts +57 -69
- package/src/commands/login.ts +12 -29
- package/src/commands/logout.ts +8 -28
- package/src/commands/preview-add.ts +57 -0
- package/src/commands/preview-list.ts +54 -0
- package/src/commands/preview-remove.ts +51 -0
- package/src/commands/preview-set-simulator.ts +60 -0
- package/src/commands/preview.ts +28 -0
- package/src/commands/sync.ts +49 -87
- package/src/commands/webhook-create.ts +89 -0
- package/src/commands/webhook-disable.ts +60 -0
- package/src/commands/webhook-enable.ts +60 -0
- package/src/commands/webhook-list.ts +43 -0
- package/src/commands/webhook-remove.ts +52 -0
- package/src/commands/webhook-set-triggers.ts +93 -0
- package/src/commands/webhook-view.ts +65 -0
- package/src/commands/webhook.ts +43 -0
- package/src/commands/whoami.ts +7 -28
- package/src/config.ts +2 -11
- package/src/index.ts +122 -105
- package/src/lib/command.ts +188 -0
- package/src/lib/file.ts +13 -1
- package/src/lib/packageJson.ts +70 -1
- package/src/lib/segment.ts +26 -30
- package/src/project.ts +54 -0
- package/dist/frameworks-DtGBrEuY.mjs +0 -17
- package/dist/nextjs-2qjzSaQI.mjs +0 -312
- package/dist/nuxt-DKsgbqpV.mjs +0 -59
- package/src/frameworks/index.ts +0 -398
- package/src/frameworks/nextjs.ts +0 -211
- package/src/frameworks/nuxt.ts +0 -252
- package/src/frameworks/sveltekit.ts +0 -234
- /package/src/{frameworks → adapters}/nuxt.templates.ts +0 -0
- /package/src/{frameworks → adapters}/sveltekit.templates.ts +0 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { SharedSlice } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { pascalCase } from "change-case";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { relative } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
|
|
8
|
+
import { Adapter } from ".";
|
|
9
|
+
import { exists, writeFileRecursive } from "../lib/file";
|
|
10
|
+
import { addDependencies, findPackageJson, getNpmPackageVersion } from "../lib/packageJson";
|
|
11
|
+
import { dedent } from "../lib/string";
|
|
12
|
+
import { appendTrailingSlash } from "../lib/url";
|
|
13
|
+
import { checkIsTypeScriptProject, findProjectRoot } from "../project";
|
|
14
|
+
import {
|
|
15
|
+
revalidateRouteTemplate,
|
|
16
|
+
exitPreviewRouteTemplate,
|
|
17
|
+
previewRouteTemplate,
|
|
18
|
+
prismicIOFileTemplate,
|
|
19
|
+
sliceSimulatorPageTemplate,
|
|
20
|
+
sliceTemplate,
|
|
21
|
+
} from "./nextjs.templates";
|
|
22
|
+
|
|
23
|
+
export class NextJsAdapter extends Adapter {
|
|
24
|
+
readonly id = "next";
|
|
25
|
+
|
|
26
|
+
async onProjectInitialized(): Promise<void> {
|
|
27
|
+
await addDependencies({
|
|
28
|
+
"@prismicio/client": `^${await getNpmPackageVersion("@prismicio/client")}`,
|
|
29
|
+
"@prismicio/react": `^${await getNpmPackageVersion("@prismicio/react")}`,
|
|
30
|
+
"@prismicio/next": `^${await getNpmPackageVersion("@prismicio/next")}`,
|
|
31
|
+
});
|
|
32
|
+
await createPrismicIoFile();
|
|
33
|
+
await createSliceSimulatorPage();
|
|
34
|
+
await createPreviewRoute();
|
|
35
|
+
await createExitPreviewRoute();
|
|
36
|
+
await createRevalidateRoute();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async onSliceCreated(model: SharedSlice, library: URL): Promise<void> {
|
|
40
|
+
const sliceDirectoryName = pascalCase(model.name);
|
|
41
|
+
const sliceDirectory = new URL(sliceDirectoryName, appendTrailingSlash(library));
|
|
42
|
+
|
|
43
|
+
const extension = await getJsFileExtension();
|
|
44
|
+
const componentPath = new URL(`index.${extension}x`, sliceDirectory);
|
|
45
|
+
const contents = sliceTemplate({
|
|
46
|
+
name: model.name,
|
|
47
|
+
typescript: await checkIsTypeScriptProject(),
|
|
48
|
+
});
|
|
49
|
+
await writeFileRecursive(componentPath, contents);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
onSliceUpdated(): void {}
|
|
53
|
+
|
|
54
|
+
onSliceDeleted(): void {}
|
|
55
|
+
|
|
56
|
+
onCustomTypeCreated(): void {}
|
|
57
|
+
|
|
58
|
+
onCustomTypeUpdated(): void {}
|
|
59
|
+
|
|
60
|
+
onCustomTypeDeleted(): void {}
|
|
61
|
+
|
|
62
|
+
async createSliceIndexFile(library: URL): Promise<void> {
|
|
63
|
+
const allSlices = await this.getSlices();
|
|
64
|
+
const slices = allSlices.filter((slice) => slice.library.href === library.href);
|
|
65
|
+
const imports = slices.map((slice) => {
|
|
66
|
+
const componentName = pascalCase(slice.model.name);
|
|
67
|
+
const relativeDirectory = relative(fileURLToPath(library), fileURLToPath(slice.directory));
|
|
68
|
+
return `import ${componentName} from "./${relativeDirectory}";`;
|
|
69
|
+
});
|
|
70
|
+
const componentLines = slices.map((slice) => {
|
|
71
|
+
const componentName = pascalCase(slice.model.name);
|
|
72
|
+
return `${slice.model.id}: ${componentName}`;
|
|
73
|
+
});
|
|
74
|
+
const contents = dedent`
|
|
75
|
+
// Code generated by Prismic. DO NOT EDIT.
|
|
76
|
+
|
|
77
|
+
${imports.join("\n")}
|
|
78
|
+
|
|
79
|
+
export const components = {
|
|
80
|
+
${componentLines.join(",\n")}
|
|
81
|
+
};
|
|
82
|
+
`;
|
|
83
|
+
const extension = await getJsFileExtension();
|
|
84
|
+
const filename = `index.${extension}`;
|
|
85
|
+
const indexPath = new URL(filename, library);
|
|
86
|
+
await writeFileRecursive(indexPath, contents);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async getDefaultSliceLibrary(): Promise<URL> {
|
|
90
|
+
const projectRoot = await findProjectRoot();
|
|
91
|
+
const hasSrcDirectory = await checkHasSrc();
|
|
92
|
+
const defaultSliceLibrary = new URL(hasSrcDirectory ? "src/slices/" : "slices/", projectRoot);
|
|
93
|
+
return defaultSliceLibrary;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function createRevalidateRoute(): Promise<void> {
|
|
98
|
+
const appRouter = await checkUsesAppRouter();
|
|
99
|
+
if (!appRouter) return;
|
|
100
|
+
|
|
101
|
+
const sourceRoot = await getSourceRoot();
|
|
102
|
+
const extension = await getJsFileExtension();
|
|
103
|
+
const filePath = new URL(`app/api/revalidate/route.${extension}`, sourceRoot);
|
|
104
|
+
if (await exists(filePath)) return;
|
|
105
|
+
|
|
106
|
+
const version = await getNextJsVersion();
|
|
107
|
+
const major = Number.parseInt(version.split(".")[0]);
|
|
108
|
+
const supportsCacheLife = major >= 16;
|
|
109
|
+
|
|
110
|
+
const contents = revalidateRouteTemplate({ supportsCacheLife });
|
|
111
|
+
await writeFileRecursive(filePath, contents);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function createExitPreviewRoute(): Promise<void> {
|
|
115
|
+
const appRouter = await checkUsesAppRouter();
|
|
116
|
+
const sourceRoot = await getSourceRoot();
|
|
117
|
+
const extension = await getJsFileExtension();
|
|
118
|
+
const filePath = new URL(
|
|
119
|
+
appRouter ? `app/api/exit-preview/route.${extension}` : `pages/api/exit-preview.${extension}`,
|
|
120
|
+
sourceRoot,
|
|
121
|
+
);
|
|
122
|
+
if (await exists(filePath)) return;
|
|
123
|
+
|
|
124
|
+
const typescript = await checkIsTypeScriptProject();
|
|
125
|
+
|
|
126
|
+
const contents = exitPreviewRouteTemplate({ typescript, appRouter });
|
|
127
|
+
await writeFileRecursive(filePath, contents);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function createPreviewRoute(): Promise<void> {
|
|
131
|
+
const appRouter = await checkUsesAppRouter();
|
|
132
|
+
const sourceRoot = await getSourceRoot();
|
|
133
|
+
const extension = await getJsFileExtension();
|
|
134
|
+
const filePath = new URL(
|
|
135
|
+
appRouter ? `app/api/preview/route.${extension}` : `pages/api/preview.${extension}`,
|
|
136
|
+
sourceRoot,
|
|
137
|
+
);
|
|
138
|
+
if (await exists(filePath)) return;
|
|
139
|
+
|
|
140
|
+
const typescript = await checkIsTypeScriptProject();
|
|
141
|
+
|
|
142
|
+
const contents = previewRouteTemplate({ typescript, appRouter });
|
|
143
|
+
await writeFileRecursive(filePath, contents);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function createSliceSimulatorPage(): Promise<void> {
|
|
147
|
+
const appRouter = await checkUsesAppRouter();
|
|
148
|
+
const sourceRoot = await getSourceRoot();
|
|
149
|
+
const extension = `${await getJsFileExtension()}x`;
|
|
150
|
+
const filePath = new URL(
|
|
151
|
+
appRouter ? `app/slice-simulator/page.${extension}` : `pages/slice-simulator.${extension}`,
|
|
152
|
+
sourceRoot,
|
|
153
|
+
);
|
|
154
|
+
if (await exists(filePath)) return;
|
|
155
|
+
|
|
156
|
+
const typescript = await checkIsTypeScriptProject();
|
|
157
|
+
|
|
158
|
+
const contents = sliceSimulatorPageTemplate({ typescript, appRouter });
|
|
159
|
+
await writeFileRecursive(filePath, contents);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function createPrismicIoFile(): Promise<void> {
|
|
163
|
+
const extension = await getJsFileExtension();
|
|
164
|
+
const sourceRoot = await getSourceRoot();
|
|
165
|
+
const filePath = new URL(`prismicio.${extension}`, sourceRoot);
|
|
166
|
+
if (await exists(filePath)) return;
|
|
167
|
+
|
|
168
|
+
const typescript = await checkIsTypeScriptProject();
|
|
169
|
+
const appRouter = await checkUsesAppRouter();
|
|
170
|
+
const hasSrcDirectory = await checkHasSrc();
|
|
171
|
+
|
|
172
|
+
const contents = prismicIOFileTemplate({
|
|
173
|
+
typescript,
|
|
174
|
+
appRouter,
|
|
175
|
+
hasSrcDirectory,
|
|
176
|
+
});
|
|
177
|
+
await writeFileRecursive(filePath, contents);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function checkUsesAppRouter() {
|
|
181
|
+
const sourceDirectory = await getSourceRoot();
|
|
182
|
+
const appDirectory = new URL("app/", sourceDirectory);
|
|
183
|
+
const usesAppRouter = await exists(appDirectory);
|
|
184
|
+
return usesAppRouter;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function getSourceRoot() {
|
|
188
|
+
const projectRoot = await findProjectRoot();
|
|
189
|
+
const hasSrcDirectory = await checkHasSrc();
|
|
190
|
+
if (hasSrcDirectory) return new URL("src/", projectRoot);
|
|
191
|
+
return projectRoot;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function checkHasSrc(): Promise<boolean> {
|
|
195
|
+
const projectRoot = await findProjectRoot();
|
|
196
|
+
const srcDirectory = new URL("src/", projectRoot);
|
|
197
|
+
return exists(srcDirectory);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function getJsFileExtension() {
|
|
201
|
+
const isTypeScriptProject = await checkIsTypeScriptProject();
|
|
202
|
+
const jsFileExtension = isTypeScriptProject ? "ts" : "js";
|
|
203
|
+
return jsFileExtension;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function getNextJsVersion() {
|
|
207
|
+
const packageJsonPath = await findPackageJson();
|
|
208
|
+
const require = createRequire(packageJsonPath);
|
|
209
|
+
const { version } = require("next/package.json");
|
|
210
|
+
return version;
|
|
211
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import type { SharedSlice } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { pascalCase } from "change-case";
|
|
4
|
+
import { builders, loadFile, writeFile as magicastWriteFile } from "magicast";
|
|
5
|
+
import { readFile, rm } from "node:fs/promises";
|
|
6
|
+
import { relative } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
import { Adapter } from ".";
|
|
10
|
+
import { readConfig, updateConfig } from "../config";
|
|
11
|
+
import { exists, writeFileRecursive } from "../lib/file";
|
|
12
|
+
import { addDependencies, getNpmPackageVersion } from "../lib/packageJson";
|
|
13
|
+
import { dedent } from "../lib/string";
|
|
14
|
+
import { appendTrailingSlash } from "../lib/url";
|
|
15
|
+
import { checkIsTypeScriptProject, findProjectRoot } from "../project";
|
|
16
|
+
import { sliceSimulatorPageTemplate, sliceTemplate } from "./nuxt.templates";
|
|
17
|
+
|
|
18
|
+
const NUXT_PRISMIC = "@nuxtjs/prismic";
|
|
19
|
+
|
|
20
|
+
export class NuxtAdapter extends Adapter {
|
|
21
|
+
readonly id = "nuxt";
|
|
22
|
+
|
|
23
|
+
async onProjectInitialized(): Promise<void> {
|
|
24
|
+
await addDependencies({
|
|
25
|
+
"@prismicio/client": `^${await getNpmPackageVersion("@prismicio/client")}`,
|
|
26
|
+
[NUXT_PRISMIC]: `^${await getNpmPackageVersion(NUXT_PRISMIC)}`,
|
|
27
|
+
});
|
|
28
|
+
await configureNuxtModule();
|
|
29
|
+
await createSliceSimulatorPage();
|
|
30
|
+
await moveOrDeleteAppVue();
|
|
31
|
+
await modifySliceLibraryPath(this);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async onSliceCreated(model: SharedSlice, library: URL): Promise<void> {
|
|
35
|
+
const sliceDirectoryName = pascalCase(model.name);
|
|
36
|
+
const sliceDirectory = new URL(sliceDirectoryName, appendTrailingSlash(library));
|
|
37
|
+
|
|
38
|
+
const componentPath = new URL("index.vue", sliceDirectory);
|
|
39
|
+
const contents = sliceTemplate({
|
|
40
|
+
name: model.name,
|
|
41
|
+
typescript: await checkIsTypeScriptProject(),
|
|
42
|
+
});
|
|
43
|
+
await writeFileRecursive(componentPath, contents);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
onSliceUpdated(): void {}
|
|
47
|
+
|
|
48
|
+
onSliceDeleted(): void {}
|
|
49
|
+
|
|
50
|
+
onCustomTypeCreated(): void {}
|
|
51
|
+
|
|
52
|
+
onCustomTypeUpdated(): void {}
|
|
53
|
+
|
|
54
|
+
onCustomTypeDeleted(): void {}
|
|
55
|
+
|
|
56
|
+
async createSliceIndexFile(library: URL): Promise<void> {
|
|
57
|
+
const allSlices = await this.getSlices();
|
|
58
|
+
const slices = allSlices.filter((slice) => slice.library.href === library.href);
|
|
59
|
+
const componentLines = slices.map((slice) => {
|
|
60
|
+
const relativeDirectory = relative(fileURLToPath(library), fileURLToPath(slice.directory));
|
|
61
|
+
return `${slice.model.id}: defineAsyncComponent(() => import("./${relativeDirectory}/index.vue"))`;
|
|
62
|
+
});
|
|
63
|
+
const contents = dedent`
|
|
64
|
+
// Code generated by Prismic. DO NOT EDIT.
|
|
65
|
+
|
|
66
|
+
import { defineAsyncComponent } from "vue";
|
|
67
|
+
import { defineSliceZoneComponents } from "@prismicio/vue";
|
|
68
|
+
|
|
69
|
+
export const components = defineSliceZoneComponents({
|
|
70
|
+
${componentLines.join(",\n")}
|
|
71
|
+
});
|
|
72
|
+
`;
|
|
73
|
+
const extension = await getJsFileExtension();
|
|
74
|
+
const filename = `index.${extension}`;
|
|
75
|
+
const indexPath = new URL(filename, library);
|
|
76
|
+
await writeFileRecursive(indexPath, contents);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async getDefaultSliceLibrary(): Promise<URL> {
|
|
80
|
+
const srcDir = await getSrcDir();
|
|
81
|
+
return new URL("slices/", srcDir);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function getSrcDir(): Promise<URL> {
|
|
86
|
+
const projectRoot = await findProjectRoot();
|
|
87
|
+
const appDir = new URL("app/", projectRoot);
|
|
88
|
+
if (await exists(appDir)) return appDir;
|
|
89
|
+
const srcDir = new URL("src/", projectRoot);
|
|
90
|
+
if (await exists(srcDir)) return srcDir;
|
|
91
|
+
return projectRoot;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function configureNuxtModule(): Promise<void> {
|
|
95
|
+
const projectRoot = await findProjectRoot();
|
|
96
|
+
|
|
97
|
+
let configUrl = new URL("nuxt.config.js", projectRoot);
|
|
98
|
+
if (!(await exists(configUrl))) {
|
|
99
|
+
configUrl = new URL("nuxt.config.ts", projectRoot);
|
|
100
|
+
}
|
|
101
|
+
if (!(await exists(configUrl))) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const filepath = fileURLToPath(configUrl);
|
|
106
|
+
const mod = await loadFile(filepath);
|
|
107
|
+
const config =
|
|
108
|
+
mod.exports.default.$type === "function-call"
|
|
109
|
+
? mod.exports.default.$args[0]
|
|
110
|
+
: mod.exports.default;
|
|
111
|
+
|
|
112
|
+
// Check if @nuxtjs/prismic is already registered
|
|
113
|
+
let hasInlinedConfiguration = false;
|
|
114
|
+
const hasPrismicModuleRegistered = (config.modules || []).find(
|
|
115
|
+
(registration: string | [string, unknown]) => {
|
|
116
|
+
if (typeof registration === "string") {
|
|
117
|
+
return registration === NUXT_PRISMIC;
|
|
118
|
+
} else if (Array.isArray(registration)) {
|
|
119
|
+
hasInlinedConfiguration = !!registration[1];
|
|
120
|
+
return registration[0] === NUXT_PRISMIC;
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
},
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
if (!hasPrismicModuleRegistered) {
|
|
127
|
+
config.modules ||= [];
|
|
128
|
+
config.modules.push(NUXT_PRISMIC);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Append Prismic module configuration if not inlined
|
|
132
|
+
if (!hasInlinedConfiguration) {
|
|
133
|
+
mod.imports.$prepend({
|
|
134
|
+
from: "./prismic.config.json",
|
|
135
|
+
imported: "repositoryName",
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
config.prismic ||= {};
|
|
139
|
+
config.prismic.endpoint = builders.raw("repositoryName");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
await magicastWriteFile(mod, filepath);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function createSliceSimulatorPage(): Promise<void> {
|
|
146
|
+
const projectRoot = await findProjectRoot();
|
|
147
|
+
const typescript = await checkIsTypeScriptProject();
|
|
148
|
+
|
|
149
|
+
// Check for existing pages directories in priority order
|
|
150
|
+
const appPagesDir = new URL("app/pages/", projectRoot);
|
|
151
|
+
const srcPagesDir = new URL("src/pages/", projectRoot);
|
|
152
|
+
const pagesDir = new URL("pages/", projectRoot);
|
|
153
|
+
|
|
154
|
+
let targetDir: URL;
|
|
155
|
+
if (await exists(appPagesDir)) {
|
|
156
|
+
targetDir = appPagesDir;
|
|
157
|
+
} else if (await exists(srcPagesDir)) {
|
|
158
|
+
targetDir = srcPagesDir;
|
|
159
|
+
} else if (await exists(pagesDir)) {
|
|
160
|
+
targetDir = pagesDir;
|
|
161
|
+
} else {
|
|
162
|
+
targetDir = new URL("pages/", await getSrcDir());
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const filePath = new URL("slice-simulator.vue", targetDir);
|
|
166
|
+
|
|
167
|
+
if (await exists(filePath)) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const contents = sliceSimulatorPageTemplate({ typescript });
|
|
172
|
+
await writeFileRecursive(filePath, contents);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function moveOrDeleteAppVue(): Promise<void> {
|
|
176
|
+
const srcDir = await getSrcDir();
|
|
177
|
+
const appVuePath = new URL("app.vue", srcDir);
|
|
178
|
+
|
|
179
|
+
if (!(await exists(appVuePath))) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const contents = await readFile(appVuePath, "utf8");
|
|
184
|
+
|
|
185
|
+
if (!contents.includes("<NuxtWelcome")) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const indexVuePath = new URL("pages/index.vue", srcDir);
|
|
190
|
+
|
|
191
|
+
if (!(await exists(indexVuePath))) {
|
|
192
|
+
await writeFileRecursive(indexVuePath, contents);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
await rm(appVuePath);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function modifySliceLibraryPath(adapter: NuxtAdapter): Promise<void> {
|
|
199
|
+
const projectRoot = await findProjectRoot();
|
|
200
|
+
const hasAppDir = await exists(new URL("app/", projectRoot));
|
|
201
|
+
const hasSrcDir = await exists(new URL("src/", projectRoot));
|
|
202
|
+
|
|
203
|
+
if (!hasAppDir && !hasSrcDir) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let config;
|
|
208
|
+
try {
|
|
209
|
+
config = await readConfig();
|
|
210
|
+
} catch {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const libraries = config.libraries;
|
|
215
|
+
if (!libraries || JSON.stringify(libraries) !== JSON.stringify(["./slices"])) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Only modify if the default library has no slices yet
|
|
220
|
+
const slices = await adapter.getSlices();
|
|
221
|
+
if (slices.length > 0) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const newLibrary = hasAppDir ? "./app/slices" : "./src/slices";
|
|
226
|
+
await updateConfig({ libraries: [newLibrary] });
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function getJsFileExtension(): Promise<string> {
|
|
230
|
+
const isTypeScriptProject = await checkIsTypeScriptProject();
|
|
231
|
+
return isTypeScriptProject ? "ts" : "js";
|
|
232
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import type { SharedSlice } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { pascalCase } from "change-case";
|
|
4
|
+
import { loadFile } from "magicast";
|
|
5
|
+
import { writeFile } from "node:fs/promises";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import { relative } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
|
|
10
|
+
import { Adapter } from ".";
|
|
11
|
+
import { exists, writeFileRecursive } from "../lib/file";
|
|
12
|
+
import { addDependencies, findPackageJson, getNpmPackageVersion } from "../lib/packageJson";
|
|
13
|
+
import { dedent } from "../lib/string";
|
|
14
|
+
import { appendTrailingSlash } from "../lib/url";
|
|
15
|
+
import { checkIsTypeScriptProject, findProjectRoot } from "../project";
|
|
16
|
+
import {
|
|
17
|
+
previewAPIRouteTemplate,
|
|
18
|
+
prismicIOFileTemplate,
|
|
19
|
+
rootLayoutTemplate,
|
|
20
|
+
sliceSimulatorPageTemplate,
|
|
21
|
+
sliceTemplate,
|
|
22
|
+
} from "./sveltekit.templates";
|
|
23
|
+
|
|
24
|
+
export class SvelteKitAdapter extends Adapter {
|
|
25
|
+
readonly id = "sveltekit";
|
|
26
|
+
|
|
27
|
+
async onProjectInitialized(): Promise<void> {
|
|
28
|
+
await addDependencies({
|
|
29
|
+
"@prismicio/client": `^${await getNpmPackageVersion("@prismicio/client")}`,
|
|
30
|
+
"@prismicio/svelte": `^${await getNpmPackageVersion("@prismicio/svelte")}`,
|
|
31
|
+
});
|
|
32
|
+
await createPrismicIoFile();
|
|
33
|
+
await createSliceSimulatorPage();
|
|
34
|
+
await createPreviewRouteMatcher();
|
|
35
|
+
await createPreviewAPIRoute();
|
|
36
|
+
await createPreviewRouteDirectory();
|
|
37
|
+
await createRootLayoutServerFile();
|
|
38
|
+
await createRootLayoutFile();
|
|
39
|
+
await modifyViteConfig();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async onSliceCreated(model: SharedSlice, library: URL): Promise<void> {
|
|
43
|
+
const sliceDirectoryName = pascalCase(model.name);
|
|
44
|
+
const sliceDirectory = new URL(sliceDirectoryName, appendTrailingSlash(library));
|
|
45
|
+
|
|
46
|
+
const componentPath = new URL("index.svelte", sliceDirectory);
|
|
47
|
+
const contents = sliceTemplate({
|
|
48
|
+
name: model.name,
|
|
49
|
+
typescript: await checkIsTypeScriptProject(),
|
|
50
|
+
version: await getSvelteMajor(),
|
|
51
|
+
});
|
|
52
|
+
await writeFileRecursive(componentPath, contents);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
onSliceUpdated(): void {}
|
|
56
|
+
|
|
57
|
+
onSliceDeleted(): void {}
|
|
58
|
+
|
|
59
|
+
onCustomTypeCreated(): void {}
|
|
60
|
+
|
|
61
|
+
onCustomTypeUpdated(): void {}
|
|
62
|
+
|
|
63
|
+
onCustomTypeDeleted(): void {}
|
|
64
|
+
|
|
65
|
+
async createSliceIndexFile(library: URL): Promise<void> {
|
|
66
|
+
const allSlices = await this.getSlices();
|
|
67
|
+
const slices = allSlices.filter((slice) => slice.library.href === library.href);
|
|
68
|
+
const imports = slices.map((slice) => {
|
|
69
|
+
const componentName = pascalCase(slice.model.name);
|
|
70
|
+
const relativeDirectory = relative(fileURLToPath(library), fileURLToPath(slice.directory));
|
|
71
|
+
return `import ${componentName} from "./${relativeDirectory}/index.svelte";`;
|
|
72
|
+
});
|
|
73
|
+
const componentLines = slices.map((slice) => {
|
|
74
|
+
const componentName = pascalCase(slice.model.name);
|
|
75
|
+
return `${slice.model.id}: ${componentName}`;
|
|
76
|
+
});
|
|
77
|
+
const contents = dedent`
|
|
78
|
+
// Code generated by Prismic. DO NOT EDIT.
|
|
79
|
+
|
|
80
|
+
${imports.join("\n")}
|
|
81
|
+
|
|
82
|
+
export const components = {
|
|
83
|
+
${componentLines.join(",\n")}
|
|
84
|
+
};
|
|
85
|
+
`;
|
|
86
|
+
const extension = await getJsFileExtension();
|
|
87
|
+
const filename = `index.${extension}`;
|
|
88
|
+
const indexPath = new URL(filename, library);
|
|
89
|
+
await writeFileRecursive(indexPath, contents);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async getDefaultSliceLibrary(): Promise<URL> {
|
|
93
|
+
const projectRoot = await findProjectRoot();
|
|
94
|
+
return new URL("src/lib/slices/", projectRoot);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function createPrismicIoFile(): Promise<void> {
|
|
99
|
+
const extension = await getJsFileExtension();
|
|
100
|
+
const projectRoot = await findProjectRoot();
|
|
101
|
+
const filePath = new URL(`src/lib/prismicio.${extension}`, projectRoot);
|
|
102
|
+
if (await exists(filePath)) return;
|
|
103
|
+
|
|
104
|
+
const typescript = await checkIsTypeScriptProject();
|
|
105
|
+
const contents = prismicIOFileTemplate({ typescript });
|
|
106
|
+
await writeFileRecursive(filePath, contents);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function createSliceSimulatorPage(): Promise<void> {
|
|
110
|
+
const projectRoot = await findProjectRoot();
|
|
111
|
+
const filePath = new URL("src/routes/slice-simulator/+page.svelte", projectRoot);
|
|
112
|
+
if (await exists(filePath)) return;
|
|
113
|
+
|
|
114
|
+
const contents = sliceSimulatorPageTemplate({
|
|
115
|
+
version: await getSvelteMajor(),
|
|
116
|
+
});
|
|
117
|
+
await writeFileRecursive(filePath, contents);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function createPreviewRouteMatcher(): Promise<void> {
|
|
121
|
+
const extension = await getJsFileExtension();
|
|
122
|
+
const projectRoot = await findProjectRoot();
|
|
123
|
+
const filePath = new URL(`src/params/preview.${extension}`, projectRoot);
|
|
124
|
+
if (await exists(filePath)) return;
|
|
125
|
+
|
|
126
|
+
const contents = dedent`
|
|
127
|
+
export function match(param) {
|
|
128
|
+
return param === 'preview';
|
|
129
|
+
}
|
|
130
|
+
`;
|
|
131
|
+
await writeFileRecursive(filePath, contents);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function createPreviewAPIRoute(): Promise<void> {
|
|
135
|
+
const extension = await getJsFileExtension();
|
|
136
|
+
const projectRoot = await findProjectRoot();
|
|
137
|
+
const filePath = new URL(`src/routes/api/preview/+server.${extension}`, projectRoot);
|
|
138
|
+
if (await exists(filePath)) return;
|
|
139
|
+
|
|
140
|
+
const typescript = await checkIsTypeScriptProject();
|
|
141
|
+
const contents = previewAPIRouteTemplate({ typescript });
|
|
142
|
+
await writeFileRecursive(filePath, contents);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function createPreviewRouteDirectory(): Promise<void> {
|
|
146
|
+
const projectRoot = await findProjectRoot();
|
|
147
|
+
const filePath = new URL("src/routes/[[preview=preview]]/README.md", projectRoot);
|
|
148
|
+
if (await exists(filePath)) return;
|
|
149
|
+
|
|
150
|
+
const contents = dedent`
|
|
151
|
+
This directory adds support for optional \`/preview\` routes. Do not remove this directory.
|
|
152
|
+
|
|
153
|
+
All routes within this directory will be served using the following URLs:
|
|
154
|
+
|
|
155
|
+
- \`/example-route\` (prerendered)
|
|
156
|
+
- \`/preview/example-route\` (server-rendered)
|
|
157
|
+
|
|
158
|
+
See <https://prismic.io/docs/svelte-preview> for more information.
|
|
159
|
+
`;
|
|
160
|
+
await writeFileRecursive(filePath, contents);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function createRootLayoutServerFile(): Promise<void> {
|
|
164
|
+
const extension = await getJsFileExtension();
|
|
165
|
+
const projectRoot = await findProjectRoot();
|
|
166
|
+
const filePath = new URL(`src/routes/+layout.server.${extension}`, projectRoot);
|
|
167
|
+
if (await exists(filePath)) return;
|
|
168
|
+
|
|
169
|
+
const contents = dedent`
|
|
170
|
+
export const prerender = "auto";
|
|
171
|
+
`;
|
|
172
|
+
await writeFileRecursive(filePath, contents);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function createRootLayoutFile(): Promise<void> {
|
|
176
|
+
const projectRoot = await findProjectRoot();
|
|
177
|
+
const filePath = new URL("src/routes/+layout.svelte", projectRoot);
|
|
178
|
+
if (await exists(filePath)) return;
|
|
179
|
+
|
|
180
|
+
const contents = rootLayoutTemplate({
|
|
181
|
+
version: await getSvelteMajor(),
|
|
182
|
+
});
|
|
183
|
+
await writeFileRecursive(filePath, contents);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function modifyViteConfig(): Promise<void> {
|
|
187
|
+
const projectRoot = await findProjectRoot();
|
|
188
|
+
let configUrl = new URL("vite.config.js", projectRoot);
|
|
189
|
+
if (!(await exists(configUrl))) {
|
|
190
|
+
configUrl = new URL("vite.config.ts", projectRoot);
|
|
191
|
+
}
|
|
192
|
+
if (!(await exists(configUrl))) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const filepath = configUrl.pathname;
|
|
197
|
+
const mod = await loadFile(filepath);
|
|
198
|
+
if (mod.exports.default.$type !== "function-call") {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const config = mod.exports.default.$args[0];
|
|
203
|
+
config.server ??= {};
|
|
204
|
+
config.server.fs ??= {};
|
|
205
|
+
config.server.fs.allow ??= [];
|
|
206
|
+
if (!config.server.fs.allow.includes("./prismic.config.json")) {
|
|
207
|
+
config.server.fs.allow.push("./prismic.config.json");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const contents = mod.generate().code.replace(/\n\s*\n(?=\s*server:)/, "\n");
|
|
211
|
+
await writeFile(configUrl, contents);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async function getJsFileExtension(): Promise<string> {
|
|
215
|
+
const isTypeScriptProject = await checkIsTypeScriptProject();
|
|
216
|
+
return isTypeScriptProject ? "ts" : "js";
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function getSvelteMajor(): Promise<number> {
|
|
220
|
+
const packageJsonPath = await findPackageJson();
|
|
221
|
+
const require = createRequire(packageJsonPath);
|
|
222
|
+
const { version } = require("svelte/package.json");
|
|
223
|
+
const major = Number.parseInt(version.split(".")[0]);
|
|
224
|
+
if (Number.isNaN(major)) return Infinity;
|
|
225
|
+
return major;
|
|
226
|
+
}
|