@prismicio/adapter-nuxt 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +5 -0
- package/dist/constants.js.map +1 -0
- package/dist/hooks/project-init.d.ts +3 -0
- package/dist/hooks/project-init.js +117 -0
- package/dist/hooks/project-init.js.map +1 -0
- package/dist/hooks/slice-create.d.ts +3 -0
- package/dist/hooks/slice-create.js +68 -0
- package/dist/hooks/slice-create.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/buildSrcPath.d.ts +5 -0
- package/dist/lib/buildSrcPath.js +23 -0
- package/dist/lib/buildSrcPath.js.map +1 -0
- package/dist/lib/checkIsTypeScriptProject.d.ts +8 -0
- package/dist/lib/checkIsTypeScriptProject.js +8 -0
- package/dist/lib/checkIsTypeScriptProject.js.map +1 -0
- package/dist/lib/getJSFileExtension.d.ts +5 -0
- package/dist/lib/getJSFileExtension.js +12 -0
- package/dist/lib/getJSFileExtension.js.map +1 -0
- package/dist/lib/pascalCase.d.ts +8 -0
- package/dist/lib/pascalCase.js +8 -0
- package/dist/lib/pascalCase.js.map +1 -0
- package/dist/lib/rejectIfNecessary.d.ts +1 -0
- package/dist/lib/rejectIfNecessary.js +10 -0
- package/dist/lib/rejectIfNecessary.js.map +1 -0
- package/dist/lib/upsertSliceLibraryIndexFile.d.ts +7 -0
- package/dist/lib/upsertSliceLibraryIndexFile.js +74 -0
- package/dist/lib/upsertSliceLibraryIndexFile.js.map +1 -0
- package/dist/package.json.js +5 -0
- package/dist/package.json.js.map +1 -0
- package/dist/plugin.d.ts +2 -0
- package/dist/plugin.js +144 -0
- package/dist/plugin.js.map +1 -0
- package/dist/types.d.ts +34 -0
- package/package.json +79 -0
- package/src/constants.ts +5 -0
- package/src/hooks/project-init.ts +199 -0
- package/src/hooks/slice-create.ts +131 -0
- package/src/index.ts +5 -0
- package/src/lib/buildSrcPath.ts +30 -0
- package/src/lib/checkIsTypeScriptProject.ts +18 -0
- package/src/lib/getJSFileExtension.ts +22 -0
- package/src/lib/pascalCase.ts +12 -0
- package/src/lib/rejectIfNecessary.ts +16 -0
- package/src/lib/upsertSliceLibraryIndexFile.ts +110 -0
- package/src/plugin.ts +191 -0
- package/src/types.ts +38 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { PluginSystemContext } from "@prismicio/plugin-kit";
|
|
2
|
+
|
|
3
|
+
import { PluginOptions } from "../types";
|
|
4
|
+
|
|
5
|
+
import { checkIsTypeScriptProject } from "./checkIsTypeScriptProject";
|
|
6
|
+
|
|
7
|
+
type GetJSFileExtensionArgs = Pick<
|
|
8
|
+
PluginSystemContext<PluginOptions>,
|
|
9
|
+
"helpers" | "options"
|
|
10
|
+
>;
|
|
11
|
+
|
|
12
|
+
export const getJSFileExtension = async ({
|
|
13
|
+
helpers,
|
|
14
|
+
options,
|
|
15
|
+
}: GetJSFileExtensionArgs): Promise<string> => {
|
|
16
|
+
const isTypeScriptProject = await checkIsTypeScriptProject({
|
|
17
|
+
helpers,
|
|
18
|
+
options,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return isTypeScriptProject ? "ts" : "js";
|
|
22
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { pascalCase as basePascalCase } from "pascal-case";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a string to a Pascal cased string.
|
|
5
|
+
*
|
|
6
|
+
* @param input - String to convert into a Pascal cased string.
|
|
7
|
+
*
|
|
8
|
+
* @returns Pascal cased string version of `input`.
|
|
9
|
+
*/
|
|
10
|
+
export const pascalCase = (...input: (string | undefined)[]): string => {
|
|
11
|
+
return basePascalCase(input.filter(Boolean).join(" "));
|
|
12
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const rejectIfNecessary = <
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
+
TPromiseSettledResults extends readonly PromiseSettledResult<any>[],
|
|
4
|
+
>(
|
|
5
|
+
promiseSettledResults: TPromiseSettledResults,
|
|
6
|
+
): void => {
|
|
7
|
+
const rejectedReasons = promiseSettledResults
|
|
8
|
+
.filter(
|
|
9
|
+
(result): result is PromiseRejectedResult => result.status === "rejected",
|
|
10
|
+
)
|
|
11
|
+
.map((result) => result.reason);
|
|
12
|
+
|
|
13
|
+
if (rejectedReasons.length > 0) {
|
|
14
|
+
throw rejectedReasons[0];
|
|
15
|
+
}
|
|
16
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { PluginSystemContext } from "@prismicio/plugin-kit";
|
|
4
|
+
import {
|
|
5
|
+
buildSliceDirectoryPath,
|
|
6
|
+
buildSliceLibraryDirectoryPath,
|
|
7
|
+
writeProjectFile,
|
|
8
|
+
} from "@prismicio/plugin-kit/fs";
|
|
9
|
+
import { stripIndent } from "common-tags";
|
|
10
|
+
|
|
11
|
+
import { NON_EDITABLE_FILE_BANNER } from "../constants";
|
|
12
|
+
import { PluginOptions } from "../types";
|
|
13
|
+
|
|
14
|
+
import { getJSFileExtension } from "./getJSFileExtension";
|
|
15
|
+
import { pascalCase } from "./pascalCase";
|
|
16
|
+
|
|
17
|
+
type UpsertSliceLibraryIndexFileArgs = {
|
|
18
|
+
libraryID: string;
|
|
19
|
+
} & PluginSystemContext<PluginOptions>;
|
|
20
|
+
|
|
21
|
+
export const upsertSliceLibraryIndexFile = async (
|
|
22
|
+
args: UpsertSliceLibraryIndexFileArgs,
|
|
23
|
+
): Promise<void> => {
|
|
24
|
+
const slices = await args.actions.readAllSliceModelsForLibrary({
|
|
25
|
+
libraryID: args.libraryID,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
let contents: string;
|
|
29
|
+
|
|
30
|
+
if (args.options.lazyLoadSlices) {
|
|
31
|
+
contents = stripIndent`
|
|
32
|
+
${NON_EDITABLE_FILE_BANNER}
|
|
33
|
+
|
|
34
|
+
import { defineAsyncComponent } from "vue";
|
|
35
|
+
import { defineSliceZoneComponents } from "@prismicio/vue";
|
|
36
|
+
|
|
37
|
+
export const components = defineSliceZoneComponents({
|
|
38
|
+
${(
|
|
39
|
+
await Promise.all(
|
|
40
|
+
slices.map(async (slice) => {
|
|
41
|
+
const id = slice.model.id;
|
|
42
|
+
const dirName = path.basename(
|
|
43
|
+
await buildSliceDirectoryPath({
|
|
44
|
+
model: slice.model,
|
|
45
|
+
helpers: args.helpers,
|
|
46
|
+
libraryID: args.libraryID,
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return `${id}: defineAsyncComponent(() => import("./${dirName}/index.vue"))`;
|
|
51
|
+
}),
|
|
52
|
+
)
|
|
53
|
+
).join(",\n")}
|
|
54
|
+
});
|
|
55
|
+
`;
|
|
56
|
+
} else {
|
|
57
|
+
contents = stripIndent`
|
|
58
|
+
${NON_EDITABLE_FILE_BANNER}
|
|
59
|
+
|
|
60
|
+
import { defineSliceZoneComponents } from "@prismicio/vue";
|
|
61
|
+
|
|
62
|
+
${(
|
|
63
|
+
await Promise.all(
|
|
64
|
+
slices.map(async (slice) => {
|
|
65
|
+
const dirName = path.basename(
|
|
66
|
+
await buildSliceDirectoryPath({
|
|
67
|
+
model: slice.model,
|
|
68
|
+
helpers: args.helpers,
|
|
69
|
+
libraryID: args.libraryID,
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
const componentName = pascalCase(slice.model.name);
|
|
73
|
+
|
|
74
|
+
return `import ${componentName} from "./${dirName}/index.vue";`;
|
|
75
|
+
}),
|
|
76
|
+
)
|
|
77
|
+
).join("\n")}
|
|
78
|
+
|
|
79
|
+
export const components = defineSliceZoneComponents({
|
|
80
|
+
${slices
|
|
81
|
+
.map((slice) => {
|
|
82
|
+
const id = slice.model.id;
|
|
83
|
+
const componentName = pascalCase(slice.model.name);
|
|
84
|
+
|
|
85
|
+
return `${id}: ${componentName}`;
|
|
86
|
+
})
|
|
87
|
+
.join(",\n")}
|
|
88
|
+
});
|
|
89
|
+
`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const extension = await getJSFileExtension({
|
|
93
|
+
helpers: args.helpers,
|
|
94
|
+
options: args.options,
|
|
95
|
+
});
|
|
96
|
+
const filePath = path.join(
|
|
97
|
+
buildSliceLibraryDirectoryPath({
|
|
98
|
+
libraryID: args.libraryID,
|
|
99
|
+
helpers: args.helpers,
|
|
100
|
+
}),
|
|
101
|
+
`index.${extension}`,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
await writeProjectFile({
|
|
105
|
+
filename: filePath,
|
|
106
|
+
contents,
|
|
107
|
+
format: args.options.format,
|
|
108
|
+
helpers: args.helpers,
|
|
109
|
+
});
|
|
110
|
+
};
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { definePlugin } from "@prismicio/plugin-kit";
|
|
2
|
+
import {
|
|
3
|
+
deleteCustomTypeDirectory,
|
|
4
|
+
deleteSliceDirectory,
|
|
5
|
+
readCustomTypeLibrary,
|
|
6
|
+
readCustomTypeModel,
|
|
7
|
+
readSliceLibrary,
|
|
8
|
+
readSliceModel,
|
|
9
|
+
renameCustomType,
|
|
10
|
+
renameSlice,
|
|
11
|
+
upsertGlobalTypeScriptTypes,
|
|
12
|
+
writeCustomTypeModel,
|
|
13
|
+
writeSliceModel,
|
|
14
|
+
} from "@prismicio/plugin-kit/fs";
|
|
15
|
+
|
|
16
|
+
import { name as pkgName } from "../package.json";
|
|
17
|
+
|
|
18
|
+
import { projectInit } from "./hooks/project-init";
|
|
19
|
+
import { sliceCreate } from "./hooks/slice-create";
|
|
20
|
+
import { rejectIfNecessary } from "./lib/rejectIfNecessary";
|
|
21
|
+
import { upsertSliceLibraryIndexFile } from "./lib/upsertSliceLibraryIndexFile";
|
|
22
|
+
import { PluginOptions } from "./types";
|
|
23
|
+
|
|
24
|
+
export const plugin = definePlugin<PluginOptions>({
|
|
25
|
+
meta: {
|
|
26
|
+
name: pkgName,
|
|
27
|
+
},
|
|
28
|
+
defaultOptions: {
|
|
29
|
+
format: true,
|
|
30
|
+
lazyLoadSlices: true,
|
|
31
|
+
},
|
|
32
|
+
setup({ hook }) {
|
|
33
|
+
////////////////////////////////////////////////////////////////
|
|
34
|
+
// project:*
|
|
35
|
+
////////////////////////////////////////////////////////////////
|
|
36
|
+
|
|
37
|
+
hook("project:init", projectInit);
|
|
38
|
+
|
|
39
|
+
////////////////////////////////////////////////////////////////
|
|
40
|
+
// slice:*
|
|
41
|
+
////////////////////////////////////////////////////////////////
|
|
42
|
+
|
|
43
|
+
hook("slice:create", sliceCreate);
|
|
44
|
+
hook("slice:update", async (data, context) => {
|
|
45
|
+
await writeSliceModel({
|
|
46
|
+
libraryID: data.libraryID,
|
|
47
|
+
model: data.model,
|
|
48
|
+
...context,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
await upsertGlobalTypeScriptTypes({
|
|
52
|
+
filename: context.options.generatedTypesFilePath,
|
|
53
|
+
format: context.options.format,
|
|
54
|
+
...context,
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
hook("slice:rename", async (data, context) => {
|
|
58
|
+
await renameSlice({
|
|
59
|
+
libraryID: data.libraryID,
|
|
60
|
+
model: data.model,
|
|
61
|
+
format: context.options.format,
|
|
62
|
+
...context,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
rejectIfNecessary(
|
|
66
|
+
await Promise.allSettled([
|
|
67
|
+
upsertSliceLibraryIndexFile({
|
|
68
|
+
libraryID: data.libraryID,
|
|
69
|
+
...context,
|
|
70
|
+
}),
|
|
71
|
+
upsertGlobalTypeScriptTypes({
|
|
72
|
+
filename: context.options.generatedTypesFilePath,
|
|
73
|
+
format: context.options.format,
|
|
74
|
+
...context,
|
|
75
|
+
}),
|
|
76
|
+
]),
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
hook("slice:delete", async (data, context) => {
|
|
80
|
+
await deleteSliceDirectory({
|
|
81
|
+
libraryID: data.libraryID,
|
|
82
|
+
model: data.model,
|
|
83
|
+
...context,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
rejectIfNecessary(
|
|
87
|
+
await Promise.allSettled([
|
|
88
|
+
upsertSliceLibraryIndexFile({
|
|
89
|
+
libraryID: data.libraryID,
|
|
90
|
+
...context,
|
|
91
|
+
}),
|
|
92
|
+
upsertGlobalTypeScriptTypes({
|
|
93
|
+
filename: context.options.generatedTypesFilePath,
|
|
94
|
+
format: context.options.format,
|
|
95
|
+
...context,
|
|
96
|
+
}),
|
|
97
|
+
]),
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
hook("slice:read", async (data, context) => {
|
|
101
|
+
return await readSliceModel({
|
|
102
|
+
libraryID: data.libraryID,
|
|
103
|
+
sliceID: data.sliceID,
|
|
104
|
+
...context,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
////////////////////////////////////////////////////////////////
|
|
109
|
+
// slice-library:*
|
|
110
|
+
////////////////////////////////////////////////////////////////
|
|
111
|
+
|
|
112
|
+
hook("slice-library:read", async (data, context) => {
|
|
113
|
+
return await readSliceLibrary({
|
|
114
|
+
libraryID: data.libraryID,
|
|
115
|
+
...context,
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
////////////////////////////////////////////////////////////////
|
|
120
|
+
// custom-type:*
|
|
121
|
+
////////////////////////////////////////////////////////////////
|
|
122
|
+
|
|
123
|
+
hook("custom-type:create", async (data, context) => {
|
|
124
|
+
await writeCustomTypeModel({
|
|
125
|
+
model: data.model,
|
|
126
|
+
format: context.options.format,
|
|
127
|
+
...context,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
await upsertGlobalTypeScriptTypes({
|
|
131
|
+
filename: context.options.generatedTypesFilePath,
|
|
132
|
+
format: context.options.format,
|
|
133
|
+
...context,
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
hook("custom-type:update", async (data, context) => {
|
|
137
|
+
await writeCustomTypeModel({
|
|
138
|
+
model: data.model,
|
|
139
|
+
format: context.options.format,
|
|
140
|
+
...context,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
await upsertGlobalTypeScriptTypes({
|
|
144
|
+
filename: context.options.generatedTypesFilePath,
|
|
145
|
+
format: context.options.format,
|
|
146
|
+
...context,
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
hook("custom-type:rename", async (data, context) => {
|
|
150
|
+
await renameCustomType({
|
|
151
|
+
model: data.model,
|
|
152
|
+
format: context.options.format,
|
|
153
|
+
...context,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
await upsertGlobalTypeScriptTypes({
|
|
157
|
+
filename: context.options.generatedTypesFilePath,
|
|
158
|
+
format: context.options.format,
|
|
159
|
+
...context,
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
hook("custom-type:delete", async (data, context) => {
|
|
163
|
+
await deleteCustomTypeDirectory({
|
|
164
|
+
customTypeID: data.model.id,
|
|
165
|
+
...context,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
await upsertGlobalTypeScriptTypes({
|
|
169
|
+
filename: context.options.generatedTypesFilePath,
|
|
170
|
+
format: context.options.format,
|
|
171
|
+
...context,
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
hook("custom-type:read", async (data, context) => {
|
|
175
|
+
return await readCustomTypeModel({
|
|
176
|
+
customTypeID: data.id,
|
|
177
|
+
...context,
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
////////////////////////////////////////////////////////////////
|
|
182
|
+
// custom-type-library:*
|
|
183
|
+
////////////////////////////////////////////////////////////////
|
|
184
|
+
|
|
185
|
+
hook("custom-type-library:read", async (_data, context) => {
|
|
186
|
+
return await readCustomTypeLibrary({
|
|
187
|
+
helpers: context.helpers,
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
});
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type PluginOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Determines if generated files should be formatted using Prettier.
|
|
4
|
+
*
|
|
5
|
+
* @defaultValue `true`
|
|
6
|
+
*/
|
|
7
|
+
format?: boolean;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Determines if Slice components should be lazy loaded when rendered.
|
|
11
|
+
*
|
|
12
|
+
* @defaultValue `true`
|
|
13
|
+
*/
|
|
14
|
+
lazyLoadSlices?: boolean;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Determines if generated files should be written in TypeScript or
|
|
18
|
+
* JavaScript.
|
|
19
|
+
*
|
|
20
|
+
* @defaultValue `true` if the project contains a `tsconfig.json`, `false` otherwise.
|
|
21
|
+
*/
|
|
22
|
+
typescript?: boolean;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The filepath at which generated TypeScript types will be saved.
|
|
26
|
+
*
|
|
27
|
+
* @defaultValue `prismicio-types.d.ts`
|
|
28
|
+
*/
|
|
29
|
+
generatedTypesFilePath?: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The filepath at which the active Prismic environment is stored as an
|
|
33
|
+
* environment variable.
|
|
34
|
+
*
|
|
35
|
+
* @defaultValue `.env.local`
|
|
36
|
+
*/
|
|
37
|
+
environmentVariableFilePath?: string;
|
|
38
|
+
};
|