@slicemachine/adapter-sveltekit 0.0.0 → 0.3.10-dev-adapter-svelte-kit.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.
- package/LICENSE +202 -0
- package/README.md +98 -0
- package/dist/_node_modules/data-uri-to-buffer/dist/index.cjs +41 -0
- package/dist/_node_modules/data-uri-to-buffer/dist/index.cjs.map +1 -0
- package/dist/_node_modules/data-uri-to-buffer/dist/index.js +41 -0
- package/dist/_node_modules/data-uri-to-buffer/dist/index.js.map +1 -0
- package/dist/_node_modules/fetch-blob/file.cjs +61 -0
- package/dist/_node_modules/fetch-blob/file.cjs.map +1 -0
- package/dist/_node_modules/fetch-blob/file.js +61 -0
- package/dist/_node_modules/fetch-blob/file.js.map +1 -0
- package/dist/_node_modules/fetch-blob/index.cjs +223 -0
- package/dist/_node_modules/fetch-blob/index.cjs.map +1 -0
- package/dist/_node_modules/fetch-blob/index.js +223 -0
- package/dist/_node_modules/fetch-blob/index.js.map +1 -0
- package/dist/_node_modules/fetch-blob/streams.cjs +43 -0
- package/dist/_node_modules/fetch-blob/streams.cjs.map +1 -0
- package/dist/_node_modules/fetch-blob/streams.js +42 -0
- package/dist/_node_modules/fetch-blob/streams.js.map +1 -0
- package/dist/_node_modules/formdata-polyfill/esm.min.cjs +116 -0
- package/dist/_node_modules/formdata-polyfill/esm.min.cjs.map +1 -0
- package/dist/_node_modules/formdata-polyfill/esm.min.js +116 -0
- package/dist/_node_modules/formdata-polyfill/esm.min.js.map +1 -0
- package/dist/_node_modules/node-domexception/index.cjs +11 -0
- package/dist/_node_modules/node-domexception/index.cjs.map +1 -0
- package/dist/_node_modules/node-domexception/index.js +10 -0
- package/dist/_node_modules/node-domexception/index.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/body.cjs +262 -0
- package/dist/_node_modules/node-fetch/src/body.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/body.js +262 -0
- package/dist/_node_modules/node-fetch/src/body.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/errors/abort-error.cjs +10 -0
- package/dist/_node_modules/node-fetch/src/errors/abort-error.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/errors/abort-error.js +10 -0
- package/dist/_node_modules/node-fetch/src/errors/abort-error.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/errors/base.cjs +17 -0
- package/dist/_node_modules/node-fetch/src/errors/base.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/errors/base.js +17 -0
- package/dist/_node_modules/node-fetch/src/errors/base.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/errors/fetch-error.cjs +19 -0
- package/dist/_node_modules/node-fetch/src/errors/fetch-error.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/errors/fetch-error.js +19 -0
- package/dist/_node_modules/node-fetch/src/errors/fetch-error.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/headers.cjs +190 -0
- package/dist/_node_modules/node-fetch/src/headers.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/headers.js +190 -0
- package/dist/_node_modules/node-fetch/src/headers.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/index.cjs +292 -0
- package/dist/_node_modules/node-fetch/src/index.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/index.js +292 -0
- package/dist/_node_modules/node-fetch/src/index.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/request.cjs +206 -0
- package/dist/_node_modules/node-fetch/src/request.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/request.js +206 -0
- package/dist/_node_modules/node-fetch/src/request.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/response.cjs +122 -0
- package/dist/_node_modules/node-fetch/src/response.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/response.js +123 -0
- package/dist/_node_modules/node-fetch/src/response.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/get-search.cjs +12 -0
- package/dist/_node_modules/node-fetch/src/utils/get-search.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/get-search.js +12 -0
- package/dist/_node_modules/node-fetch/src/utils/get-search.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/is-redirect.cjs +8 -0
- package/dist/_node_modules/node-fetch/src/utils/is-redirect.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/is-redirect.js +8 -0
- package/dist/_node_modules/node-fetch/src/utils/is-redirect.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/is.cjs +28 -0
- package/dist/_node_modules/node-fetch/src/utils/is.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/is.js +28 -0
- package/dist/_node_modules/node-fetch/src/utils/is.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/multipart-parser.cjs +356 -0
- package/dist/_node_modules/node-fetch/src/utils/multipart-parser.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/multipart-parser.js +356 -0
- package/dist/_node_modules/node-fetch/src/utils/multipart-parser.js.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/referrer.cjs +149 -0
- package/dist/_node_modules/node-fetch/src/utils/referrer.cjs.map +1 -0
- package/dist/_node_modules/node-fetch/src/utils/referrer.js +149 -0
- package/dist/_node_modules/node-fetch/src/utils/referrer.js.map +1 -0
- package/dist/_node_modules/web-streams-polyfill/dist/ponyfill.es2018.cjs +3822 -0
- package/dist/_node_modules/web-streams-polyfill/dist/ponyfill.es2018.cjs.map +1 -0
- package/dist/_node_modules/web-streams-polyfill/dist/ponyfill.es2018.js +3822 -0
- package/dist/_node_modules/web-streams-polyfill/dist/ponyfill.es2018.js.map +1 -0
- package/dist/_virtual/_commonjsHelpers.cjs +5 -0
- package/dist/_virtual/_commonjsHelpers.cjs.map +1 -0
- package/dist/_virtual/_commonjsHelpers.js +5 -0
- package/dist/_virtual/_commonjsHelpers.js.map +1 -0
- package/dist/_virtual/ponyfill.es2018.cjs +5 -0
- package/dist/_virtual/ponyfill.es2018.cjs.map +1 -0
- package/dist/_virtual/ponyfill.es2018.js +5 -0
- package/dist/_virtual/ponyfill.es2018.js.map +1 -0
- package/dist/constants.cjs +5 -0
- package/dist/constants.cjs.map +1 -0
- 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.cjs +211 -0
- package/dist/hooks/project-init.cjs.map +1 -0
- package/dist/hooks/project-init.d.ts +3 -0
- package/dist/hooks/project-init.js +194 -0
- package/dist/hooks/project-init.js.map +1 -0
- package/dist/hooks/slice-create.cjs +69 -0
- package/dist/hooks/slice-create.cjs.map +1 -0
- package/dist/hooks/slice-create.d.ts +3 -0
- package/dist/hooks/slice-create.js +69 -0
- package/dist/hooks/slice-create.js.map +1 -0
- package/dist/hooks/sliceSimulator-setup-read.cjs +144 -0
- package/dist/hooks/sliceSimulator-setup-read.cjs.map +1 -0
- package/dist/hooks/sliceSimulator-setup-read.d.ts +3 -0
- package/dist/hooks/sliceSimulator-setup-read.js +144 -0
- package/dist/hooks/sliceSimulator-setup-read.js.map +1 -0
- package/dist/hooks/snippet-read.cjs +114 -0
- package/dist/hooks/snippet-read.cjs.map +1 -0
- package/dist/hooks/snippet-read.d.ts +3 -0
- package/dist/hooks/snippet-read.js +114 -0
- package/dist/hooks/snippet-read.js.map +1 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/checkIsTypeScriptProject.cjs +8 -0
- package/dist/lib/checkIsTypeScriptProject.cjs.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.cjs +12 -0
- package/dist/lib/getJSFileExtension.cjs.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.cjs +8 -0
- package/dist/lib/pascalCase.cjs.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.cjs +10 -0
- package/dist/lib/rejectIfNecessary.cjs.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.cjs +67 -0
- package/dist/lib/upsertSliceLibraryIndexFile.cjs.map +1 -0
- package/dist/lib/upsertSliceLibraryIndexFile.d.ts +7 -0
- package/dist/lib/upsertSliceLibraryIndexFile.js +50 -0
- package/dist/lib/upsertSliceLibraryIndexFile.js.map +1 -0
- package/dist/packages/adapter-sveltekit/package.json.cjs +5 -0
- package/dist/packages/adapter-sveltekit/package.json.cjs.map +1 -0
- package/dist/packages/adapter-sveltekit/package.json.js +5 -0
- package/dist/packages/adapter-sveltekit/package.json.js.map +1 -0
- package/dist/plugin.cjs +204 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.d.ts +2 -0
- package/dist/plugin.js +204 -0
- package/dist/plugin.js.map +1 -0
- package/dist/simulator/SliceSimulator.svelte +65 -0
- package/dist/simulator/SliceSimulator.svelte.d.ts +35 -0
- package/dist/simulator/index.d.ts +1 -0
- package/dist/simulator/index.js +1 -0
- package/dist/types.d.ts +21 -0
- package/package.json +111 -2
- package/src/constants.ts +5 -0
- package/src/hooks/project-init.ts +265 -0
- package/src/hooks/slice-create.ts +120 -0
- package/src/hooks/sliceSimulator-setup-read.ts +203 -0
- package/src/hooks/snippet-read.ts +163 -0
- package/src/index.ts +3 -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 +77 -0
- package/src/plugin.ts +275 -0
- package/src/simulator/SliceSimulator.svelte +65 -0
- package/src/simulator/index.ts +1 -0
- package/src/types.ts +23 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SliceMachineHelpers,
|
|
3
|
+
SnippetReadHook,
|
|
4
|
+
} from "@slicemachine/plugin-kit";
|
|
5
|
+
import { stripIndent } from "common-tags";
|
|
6
|
+
|
|
7
|
+
import type { PluginOptions } from "../types";
|
|
8
|
+
|
|
9
|
+
const dotPath = (...segments: (string | string[])[]): string => {
|
|
10
|
+
return segments.flat().join(".");
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const format = async (input: string, helpers: SliceMachineHelpers) => {
|
|
14
|
+
const formattedInput = await helpers.format(input, undefined, {
|
|
15
|
+
includeNewlineAtEnd: false,
|
|
16
|
+
prettier: {
|
|
17
|
+
plugins: ["prettier-plugin-svelte"],
|
|
18
|
+
parser: "svelte",
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return formattedInput;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const snippetRead: SnippetReadHook<PluginOptions> = async (
|
|
26
|
+
data,
|
|
27
|
+
{ helpers },
|
|
28
|
+
) => {
|
|
29
|
+
const { fieldPath } = data;
|
|
30
|
+
|
|
31
|
+
const label = "Svelte";
|
|
32
|
+
|
|
33
|
+
switch (data.model.type) {
|
|
34
|
+
case "StructuredText": {
|
|
35
|
+
return [
|
|
36
|
+
{
|
|
37
|
+
label: `${label} (components)`,
|
|
38
|
+
language: "svelte",
|
|
39
|
+
code: await format(
|
|
40
|
+
stripIndent`
|
|
41
|
+
<PrismicRichText field={${dotPath(fieldPath)}} />
|
|
42
|
+
`,
|
|
43
|
+
helpers,
|
|
44
|
+
),
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
label: `${label} (plain text)`,
|
|
48
|
+
language: "svelte",
|
|
49
|
+
code: await format(
|
|
50
|
+
stripIndent`
|
|
51
|
+
<PrismicText field={${dotPath(fieldPath)}} />
|
|
52
|
+
`,
|
|
53
|
+
helpers,
|
|
54
|
+
),
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
case "Link": {
|
|
60
|
+
return {
|
|
61
|
+
label,
|
|
62
|
+
language: "svelte",
|
|
63
|
+
code: await format(
|
|
64
|
+
stripIndent`
|
|
65
|
+
<PrismicLink field={${dotPath(fieldPath)}}>Link</PrismicLink>
|
|
66
|
+
`,
|
|
67
|
+
helpers,
|
|
68
|
+
),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
case "Image": {
|
|
73
|
+
return {
|
|
74
|
+
label,
|
|
75
|
+
language: "svelte",
|
|
76
|
+
code: await format(
|
|
77
|
+
stripIndent`
|
|
78
|
+
<PrismicImage field={${dotPath(fieldPath)}} />
|
|
79
|
+
`,
|
|
80
|
+
helpers,
|
|
81
|
+
),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
case "Group": {
|
|
86
|
+
const code = await format(
|
|
87
|
+
stripIndent`
|
|
88
|
+
{#each ${dotPath(fieldPath)} as item}
|
|
89
|
+
<!-- Render content for item -->
|
|
90
|
+
{/each}
|
|
91
|
+
`,
|
|
92
|
+
helpers,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
label,
|
|
97
|
+
language: "svelte",
|
|
98
|
+
code,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
case "Slices": {
|
|
103
|
+
const code = await format(
|
|
104
|
+
stripIndent`
|
|
105
|
+
<SliceZone
|
|
106
|
+
slices={${dotPath(fieldPath)}}
|
|
107
|
+
{components}
|
|
108
|
+
/>
|
|
109
|
+
`,
|
|
110
|
+
helpers,
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
label,
|
|
115
|
+
language: "svelte",
|
|
116
|
+
code,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
case "GeoPoint": {
|
|
121
|
+
const code = await format(
|
|
122
|
+
stripIndent`
|
|
123
|
+
{${dotPath(fieldPath, "latitude")}}, {${dotPath(fieldPath, "longitude")}}
|
|
124
|
+
`,
|
|
125
|
+
helpers,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
label,
|
|
130
|
+
language: "svelte",
|
|
131
|
+
code,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
case "Embed": {
|
|
136
|
+
const code = await format(
|
|
137
|
+
stripIndent`
|
|
138
|
+
{@html ${dotPath(fieldPath, "html")}}
|
|
139
|
+
`,
|
|
140
|
+
helpers,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
label,
|
|
145
|
+
language: "svelte",
|
|
146
|
+
code,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
default: {
|
|
151
|
+
return {
|
|
152
|
+
label,
|
|
153
|
+
language: "svelte",
|
|
154
|
+
code: await format(
|
|
155
|
+
stripIndent`
|
|
156
|
+
{${dotPath(fieldPath)}}
|
|
157
|
+
`,
|
|
158
|
+
helpers,
|
|
159
|
+
),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SliceMachineContext } from "@slicemachine/plugin-kit";
|
|
2
|
+
import { checkIsTypeScriptProject as baseCheckIsTypeScriptProject } from "@slicemachine/plugin-kit/fs";
|
|
3
|
+
|
|
4
|
+
import { PluginOptions } from "../types";
|
|
5
|
+
|
|
6
|
+
type CheckIsTypeScriptProjectArgs = {
|
|
7
|
+
helpers: SliceMachineContext<PluginOptions>["helpers"];
|
|
8
|
+
options: SliceMachineContext<PluginOptions>["options"];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const checkIsTypeScriptProject = async (
|
|
12
|
+
args: CheckIsTypeScriptProjectArgs,
|
|
13
|
+
): Promise<boolean> => {
|
|
14
|
+
return (
|
|
15
|
+
args.options.typescript ??
|
|
16
|
+
baseCheckIsTypeScriptProject({ helpers: args.helpers })
|
|
17
|
+
);
|
|
18
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SliceMachineContext } from "@slicemachine/plugin-kit";
|
|
2
|
+
|
|
3
|
+
import { PluginOptions } from "../types";
|
|
4
|
+
|
|
5
|
+
import { checkIsTypeScriptProject } from "./checkIsTypeScriptProject";
|
|
6
|
+
|
|
7
|
+
type GetJSFileExtensionArgs = Pick<
|
|
8
|
+
SliceMachineContext<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,77 @@
|
|
|
1
|
+
import { SliceMachineContext } from "@slicemachine/plugin-kit";
|
|
2
|
+
import {
|
|
3
|
+
buildSliceDirectoryPath,
|
|
4
|
+
buildSliceLibraryDirectoryPath,
|
|
5
|
+
writeProjectFile,
|
|
6
|
+
} from "@slicemachine/plugin-kit/fs";
|
|
7
|
+
import { stripIndent } from "common-tags";
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
|
|
10
|
+
import { NON_EDITABLE_FILE_BANNER } from "../constants";
|
|
11
|
+
import { PluginOptions } from "../types";
|
|
12
|
+
|
|
13
|
+
import { pascalCase } from "./pascalCase";
|
|
14
|
+
import { getJSFileExtension } from "./getJSFileExtension";
|
|
15
|
+
|
|
16
|
+
type UpsertSliceLibraryIndexFileArgs = {
|
|
17
|
+
libraryID: string;
|
|
18
|
+
} & SliceMachineContext<PluginOptions>;
|
|
19
|
+
|
|
20
|
+
export const upsertSliceLibraryIndexFile = async (
|
|
21
|
+
args: UpsertSliceLibraryIndexFileArgs,
|
|
22
|
+
): Promise<void> => {
|
|
23
|
+
const slices = await args.actions.readAllSliceModelsForLibrary({
|
|
24
|
+
libraryID: args.libraryID,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const contents = stripIndent`
|
|
28
|
+
${NON_EDITABLE_FILE_BANNER}
|
|
29
|
+
|
|
30
|
+
${(
|
|
31
|
+
await Promise.all(
|
|
32
|
+
slices.map(async (slice) => {
|
|
33
|
+
const dirName = path.basename(
|
|
34
|
+
await buildSliceDirectoryPath({
|
|
35
|
+
model: slice.model,
|
|
36
|
+
helpers: args.helpers,
|
|
37
|
+
libraryID: args.libraryID,
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
const componentName = pascalCase(slice.model.name);
|
|
41
|
+
|
|
42
|
+
return `import ${componentName} from "./${dirName}/index.svelte";`;
|
|
43
|
+
}),
|
|
44
|
+
)
|
|
45
|
+
).join("\n")}
|
|
46
|
+
|
|
47
|
+
export const components = {
|
|
48
|
+
${slices
|
|
49
|
+
.map((slice) => {
|
|
50
|
+
const id = slice.model.id;
|
|
51
|
+
const componentName = pascalCase(slice.model.name);
|
|
52
|
+
|
|
53
|
+
return `${id}: ${componentName},`;
|
|
54
|
+
})
|
|
55
|
+
.join("\n")}
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
const extension = await getJSFileExtension({
|
|
60
|
+
helpers: args.helpers,
|
|
61
|
+
options: args.options,
|
|
62
|
+
});
|
|
63
|
+
const filePath = path.join(
|
|
64
|
+
buildSliceLibraryDirectoryPath({
|
|
65
|
+
libraryID: args.libraryID,
|
|
66
|
+
helpers: args.helpers,
|
|
67
|
+
}),
|
|
68
|
+
`index.${extension}`,
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
await writeProjectFile({
|
|
72
|
+
filename: filePath,
|
|
73
|
+
contents,
|
|
74
|
+
format: args.options.format,
|
|
75
|
+
helpers: args.helpers,
|
|
76
|
+
});
|
|
77
|
+
};
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { defineSliceMachinePlugin } from "@slicemachine/plugin-kit";
|
|
2
|
+
import {
|
|
3
|
+
deleteCustomTypeDirectory,
|
|
4
|
+
deleteCustomTypeFile,
|
|
5
|
+
deleteSliceDirectory,
|
|
6
|
+
deleteSliceFile,
|
|
7
|
+
readCustomTypeFile,
|
|
8
|
+
readCustomTypeLibrary,
|
|
9
|
+
readCustomTypeModel,
|
|
10
|
+
readSliceFile,
|
|
11
|
+
readSliceLibrary,
|
|
12
|
+
readSliceModel,
|
|
13
|
+
renameSlice,
|
|
14
|
+
upsertGlobalTypeScriptTypes,
|
|
15
|
+
writeCustomTypeFile,
|
|
16
|
+
writeCustomTypeModel,
|
|
17
|
+
writeSliceFile,
|
|
18
|
+
writeSliceModel,
|
|
19
|
+
} from "@slicemachine/plugin-kit/fs";
|
|
20
|
+
|
|
21
|
+
import { rejectIfNecessary } from "./lib/rejectIfNecessary";
|
|
22
|
+
import { upsertSliceLibraryIndexFile } from "./lib/upsertSliceLibraryIndexFile";
|
|
23
|
+
|
|
24
|
+
import { name as pkgName } from "../package.json";
|
|
25
|
+
import { PluginOptions } from "./types";
|
|
26
|
+
|
|
27
|
+
import { projectInit } from "./hooks/project-init";
|
|
28
|
+
import { sliceCreate } from "./hooks/slice-create";
|
|
29
|
+
import { sliceSimulatorSetupRead } from "./hooks/sliceSimulator-setup-read";
|
|
30
|
+
import { snippetRead } from "./hooks/snippet-read";
|
|
31
|
+
|
|
32
|
+
export const plugin = defineSliceMachinePlugin<PluginOptions>({
|
|
33
|
+
meta: {
|
|
34
|
+
name: pkgName,
|
|
35
|
+
},
|
|
36
|
+
defaultOptions: {
|
|
37
|
+
format: true,
|
|
38
|
+
generatedTypesFilePath: "./src/prismicio-types.d.ts",
|
|
39
|
+
},
|
|
40
|
+
setup({ hook }) {
|
|
41
|
+
////////////////////////////////////////////////////////////////
|
|
42
|
+
// project:*
|
|
43
|
+
////////////////////////////////////////////////////////////////
|
|
44
|
+
|
|
45
|
+
hook("project:init", projectInit);
|
|
46
|
+
|
|
47
|
+
////////////////////////////////////////////////////////////////
|
|
48
|
+
// slice:*
|
|
49
|
+
////////////////////////////////////////////////////////////////
|
|
50
|
+
|
|
51
|
+
hook("slice:create", sliceCreate);
|
|
52
|
+
hook("slice:update", async (data, context) => {
|
|
53
|
+
await writeSliceModel({
|
|
54
|
+
libraryID: data.libraryID,
|
|
55
|
+
model: data.model,
|
|
56
|
+
...context,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await upsertGlobalTypeScriptTypes({
|
|
60
|
+
filename: context.options.generatedTypesFilePath,
|
|
61
|
+
format: context.options.format,
|
|
62
|
+
...context,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
hook("slice:rename", async (data, context) => {
|
|
66
|
+
await renameSlice({
|
|
67
|
+
libraryID: data.libraryID,
|
|
68
|
+
model: data.model,
|
|
69
|
+
format: context.options.format,
|
|
70
|
+
...context,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
rejectIfNecessary(
|
|
74
|
+
await Promise.allSettled([
|
|
75
|
+
upsertSliceLibraryIndexFile({
|
|
76
|
+
libraryID: data.libraryID,
|
|
77
|
+
...context,
|
|
78
|
+
}),
|
|
79
|
+
upsertGlobalTypeScriptTypes({
|
|
80
|
+
filename: context.options.generatedTypesFilePath,
|
|
81
|
+
format: context.options.format,
|
|
82
|
+
...context,
|
|
83
|
+
}),
|
|
84
|
+
]),
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
hook("slice:delete", async (data, context) => {
|
|
88
|
+
await deleteSliceDirectory({
|
|
89
|
+
libraryID: data.libraryID,
|
|
90
|
+
model: data.model,
|
|
91
|
+
...context,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
rejectIfNecessary(
|
|
95
|
+
await Promise.allSettled([
|
|
96
|
+
upsertSliceLibraryIndexFile({
|
|
97
|
+
libraryID: data.libraryID,
|
|
98
|
+
...context,
|
|
99
|
+
}),
|
|
100
|
+
upsertGlobalTypeScriptTypes({
|
|
101
|
+
filename: context.options.generatedTypesFilePath,
|
|
102
|
+
format: context.options.format,
|
|
103
|
+
...context,
|
|
104
|
+
}),
|
|
105
|
+
]),
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
hook("slice:read", async (data, context) => {
|
|
109
|
+
return await readSliceModel({
|
|
110
|
+
libraryID: data.libraryID,
|
|
111
|
+
sliceID: data.sliceID,
|
|
112
|
+
...context,
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
hook("slice:asset:update", async (data, context) => {
|
|
116
|
+
await writeSliceFile({
|
|
117
|
+
libraryID: data.libraryID,
|
|
118
|
+
sliceID: data.sliceID,
|
|
119
|
+
filename: data.asset.id,
|
|
120
|
+
contents: data.asset.data,
|
|
121
|
+
...context,
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
hook("slice:asset:delete", async (data, context) => {
|
|
125
|
+
await deleteSliceFile({
|
|
126
|
+
libraryID: data.libraryID,
|
|
127
|
+
sliceID: data.sliceID,
|
|
128
|
+
filename: data.assetID,
|
|
129
|
+
...context,
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
hook("slice:asset:read", async (data, context) => {
|
|
133
|
+
const file = await readSliceFile({
|
|
134
|
+
libraryID: data.libraryID,
|
|
135
|
+
sliceID: data.sliceID,
|
|
136
|
+
filename: data.assetID,
|
|
137
|
+
...context,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
data: file,
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
////////////////////////////////////////////////////////////////
|
|
146
|
+
// slice-library:*
|
|
147
|
+
////////////////////////////////////////////////////////////////
|
|
148
|
+
|
|
149
|
+
hook("slice-library:read", async (data, context) => {
|
|
150
|
+
return await readSliceLibrary({
|
|
151
|
+
libraryID: data.libraryID,
|
|
152
|
+
...context,
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
////////////////////////////////////////////////////////////////
|
|
157
|
+
// custom-type:*
|
|
158
|
+
////////////////////////////////////////////////////////////////
|
|
159
|
+
|
|
160
|
+
hook("custom-type:create", async (data, context) => {
|
|
161
|
+
await writeCustomTypeModel({
|
|
162
|
+
model: data.model,
|
|
163
|
+
format: context.options.format,
|
|
164
|
+
...context,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
await upsertGlobalTypeScriptTypes({
|
|
168
|
+
filename: context.options.generatedTypesFilePath,
|
|
169
|
+
format: context.options.format,
|
|
170
|
+
...context,
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
hook("custom-type:update", async (data, context) => {
|
|
174
|
+
await writeCustomTypeModel({
|
|
175
|
+
model: data.model,
|
|
176
|
+
format: context.options.format,
|
|
177
|
+
...context,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
await upsertGlobalTypeScriptTypes({
|
|
181
|
+
filename: context.options.generatedTypesFilePath,
|
|
182
|
+
format: context.options.format,
|
|
183
|
+
...context,
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
hook("custom-type:rename", async (data, context) => {
|
|
187
|
+
await writeCustomTypeModel({
|
|
188
|
+
model: data.model,
|
|
189
|
+
format: context.options.format,
|
|
190
|
+
...context,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
await upsertGlobalTypeScriptTypes({
|
|
194
|
+
filename: context.options.generatedTypesFilePath,
|
|
195
|
+
format: context.options.format,
|
|
196
|
+
...context,
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
hook("custom-type:delete", async (data, context) => {
|
|
200
|
+
await deleteCustomTypeDirectory({
|
|
201
|
+
customTypeID: data.model.id,
|
|
202
|
+
...context,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
await upsertGlobalTypeScriptTypes({
|
|
206
|
+
filename: context.options.generatedTypesFilePath,
|
|
207
|
+
format: context.options.format,
|
|
208
|
+
...context,
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
hook("custom-type:read", async (data, context) => {
|
|
212
|
+
return await readCustomTypeModel({
|
|
213
|
+
customTypeID: data.id,
|
|
214
|
+
...context,
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
hook("custom-type:asset:update", async (data, context) => {
|
|
218
|
+
await writeCustomTypeFile({
|
|
219
|
+
customTypeID: data.customTypeID,
|
|
220
|
+
filename: data.asset.id,
|
|
221
|
+
contents: data.asset.data,
|
|
222
|
+
...context,
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
hook("custom-type:asset:delete", async (data, context) => {
|
|
226
|
+
await deleteCustomTypeFile({
|
|
227
|
+
customTypeID: data.customTypeID,
|
|
228
|
+
filename: data.assetID,
|
|
229
|
+
...context,
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
hook("custom-type:asset:read", async (data, context) => {
|
|
233
|
+
const file = await readCustomTypeFile({
|
|
234
|
+
customTypeID: data.customTypeID,
|
|
235
|
+
filename: data.assetID,
|
|
236
|
+
...context,
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
data: file,
|
|
241
|
+
};
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
////////////////////////////////////////////////////////////////
|
|
245
|
+
// custom-type-library:*
|
|
246
|
+
////////////////////////////////////////////////////////////////
|
|
247
|
+
|
|
248
|
+
hook("custom-type-library:read", async (_data, context) => {
|
|
249
|
+
return await readCustomTypeLibrary({
|
|
250
|
+
helpers: context.helpers,
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
////////////////////////////////////////////////////////////////
|
|
255
|
+
// snippet:*
|
|
256
|
+
////////////////////////////////////////////////////////////////
|
|
257
|
+
|
|
258
|
+
hook("snippet:read", snippetRead);
|
|
259
|
+
|
|
260
|
+
////////////////////////////////////////////////////////////////
|
|
261
|
+
// documentation:*
|
|
262
|
+
////////////////////////////////////////////////////////////////
|
|
263
|
+
|
|
264
|
+
hook("documentation:read", async (_data, _context) => {
|
|
265
|
+
// TODO
|
|
266
|
+
return [];
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
////////////////////////////////////////////////////////////////
|
|
270
|
+
// slice-simulator:*
|
|
271
|
+
////////////////////////////////////////////////////////////////
|
|
272
|
+
|
|
273
|
+
hook("slice-simulator:setup:read", sliceSimulatorSetupRead);
|
|
274
|
+
},
|
|
275
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import {
|
|
3
|
+
SimulatorManager,
|
|
4
|
+
StateEventType,
|
|
5
|
+
disableEventHandler,
|
|
6
|
+
getDefaultMessage,
|
|
7
|
+
getDefaultProps,
|
|
8
|
+
getDefaultSlices,
|
|
9
|
+
onClickHandler,
|
|
10
|
+
simulatorClass,
|
|
11
|
+
simulatorRootClass,
|
|
12
|
+
} from "@prismicio/simulator/kit";
|
|
13
|
+
|
|
14
|
+
const defaultProps = getDefaultProps();
|
|
15
|
+
|
|
16
|
+
export let zIndex = defaultProps.zIndex;
|
|
17
|
+
export let background = defaultProps.background;
|
|
18
|
+
|
|
19
|
+
let slices = getDefaultSlices();
|
|
20
|
+
let message = getDefaultMessage();
|
|
21
|
+
|
|
22
|
+
if (typeof window !== "undefined") {
|
|
23
|
+
const simulatorManager = new SimulatorManager();
|
|
24
|
+
|
|
25
|
+
simulatorManager.state.on(
|
|
26
|
+
StateEventType.Slices,
|
|
27
|
+
(newSlices) => {
|
|
28
|
+
slices = newSlices;
|
|
29
|
+
},
|
|
30
|
+
"simulator-slices",
|
|
31
|
+
);
|
|
32
|
+
simulatorManager.state.on(
|
|
33
|
+
StateEventType.Message,
|
|
34
|
+
(newMessage) => {
|
|
35
|
+
message = newMessage;
|
|
36
|
+
},
|
|
37
|
+
"simulator-message",
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
simulatorManager.init();
|
|
41
|
+
}
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<div
|
|
45
|
+
class="{simulatorClass} {$$props.class}"
|
|
46
|
+
style="z-index: {zIndex}; position: fixed; top: 0; left: 0; width: 100%; height: 100vh; overflow: auto; background: {background}"
|
|
47
|
+
>
|
|
48
|
+
{#if message}
|
|
49
|
+
<article>
|
|
50
|
+
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
|
|
51
|
+
{@html message}
|
|
52
|
+
</article>
|
|
53
|
+
{:else if slices.length}
|
|
54
|
+
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
|
55
|
+
<div
|
|
56
|
+
id="root"
|
|
57
|
+
class={simulatorRootClass}
|
|
58
|
+
on:click={onClickHandler}
|
|
59
|
+
on:submit={disableEventHandler}
|
|
60
|
+
on:keypress={disableEventHandler}
|
|
61
|
+
>
|
|
62
|
+
<slot {slices} />
|
|
63
|
+
</div>
|
|
64
|
+
{/if}
|
|
65
|
+
</div>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as SliceSimulator } from "./SliceSimulator.svelte";
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
* The filepath at which generated TypeScript types will be saved.
|
|
11
|
+
*
|
|
12
|
+
* @defaultValue `prismicio-types.d.ts`
|
|
13
|
+
*/
|
|
14
|
+
generatedTypesFilePath?: string;
|
|
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
|
+
};
|