@slicemachine/adapter-next 0.0.2 → 0.0.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/dist/SliceSimulator.d.ts +2 -2
- package/dist/hooks/customType-rename.cjs +6 -0
- package/dist/hooks/customType-rename.cjs.map +1 -0
- package/dist/hooks/customType-rename.d.ts +1 -0
- package/dist/hooks/customType-rename.js +6 -0
- package/dist/hooks/customType-rename.js.map +1 -0
- package/dist/hooks/customTypeLibrary-read.cjs +12 -4
- package/dist/hooks/customTypeLibrary-read.cjs.map +1 -1
- package/dist/hooks/customTypeLibrary-read.js +12 -4
- package/dist/hooks/customTypeLibrary-read.js.map +1 -1
- package/dist/hooks/slice-create.cjs +14 -24
- package/dist/hooks/slice-create.cjs.map +1 -1
- package/dist/hooks/slice-create.js +14 -24
- package/dist/hooks/slice-create.js.map +1 -1
- package/dist/hooks/slice-delete.cjs +3 -13
- package/dist/hooks/slice-delete.cjs.map +1 -1
- package/dist/hooks/slice-delete.js +3 -13
- package/dist/hooks/slice-delete.js.map +1 -1
- package/dist/hooks/slice-read.cjs +42 -6
- package/dist/hooks/slice-read.cjs.map +1 -1
- package/dist/hooks/slice-read.js +22 -6
- package/dist/hooks/slice-read.js.map +1 -1
- package/dist/hooks/slice-rename.cjs +45 -0
- package/dist/hooks/slice-rename.cjs.map +1 -0
- package/dist/hooks/slice-rename.d.ts +3 -0
- package/dist/hooks/slice-rename.js +26 -0
- package/dist/hooks/slice-rename.js.map +1 -0
- package/dist/hooks/slice-update.cjs +6 -33
- package/dist/hooks/slice-update.cjs.map +1 -1
- package/dist/hooks/slice-update.js +6 -13
- package/dist/hooks/slice-update.js.map +1 -1
- package/dist/hooks/sliceLibrary-read.cjs +12 -14
- package/dist/hooks/sliceLibrary-read.cjs.map +1 -1
- package/dist/hooks/sliceLibrary-read.js +12 -13
- package/dist/hooks/sliceLibrary-read.js.map +1 -1
- package/dist/lib/isSharedSliceModel.cjs +27 -0
- package/dist/lib/isSharedSliceModel.cjs.map +1 -0
- package/dist/lib/isSharedSliceModel.d.ts +2 -0
- package/dist/lib/isSharedSliceModel.js +8 -0
- package/dist/lib/isSharedSliceModel.js.map +1 -0
- package/dist/lib/updateSliceModelFile.cjs +33 -0
- package/dist/lib/updateSliceModelFile.cjs.map +1 -0
- package/dist/lib/updateSliceModelFile.d.ts +9 -0
- package/dist/lib/updateSliceModelFile.js +14 -0
- package/dist/lib/updateSliceModelFile.js.map +1 -0
- package/dist/lib/upsertGlobalContentTypes.d.ts +1 -1
- package/dist/lib/upsertSliceLibraryIndexFile.cjs +47 -0
- package/dist/lib/upsertSliceLibraryIndexFile.cjs.map +1 -0
- package/dist/lib/upsertSliceLibraryIndexFile.d.ts +7 -0
- package/dist/lib/upsertSliceLibraryIndexFile.js +28 -0
- package/dist/lib/upsertSliceLibraryIndexFile.js.map +1 -0
- package/dist/plugin.cjs +4 -0
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.js +4 -0
- package/dist/plugin.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +14 -12
- package/src/hooks/customType-rename.ts +3 -0
- package/src/hooks/customTypeLibrary-read.ts +13 -4
- package/src/hooks/slice-create.ts +14 -32
- package/src/hooks/slice-delete.ts +3 -21
- package/src/hooks/slice-read.ts +37 -11
- package/src/hooks/slice-rename.ts +44 -0
- package/src/hooks/slice-update.ts +7 -31
- package/src/hooks/sliceLibrary-read.ts +13 -23
- package/src/lib/isSharedSliceModel.ts +14 -0
- package/src/lib/updateSliceModelFile.ts +32 -0
- package/src/lib/{buildSliceLibraryIndexFileContents.ts → upsertSliceLibraryIndexFile.ts} +14 -9
- package/src/plugin.ts +4 -0
- package/dist/lib/buildSliceLibraryIndexFileContents.cjs +0 -23
- package/dist/lib/buildSliceLibraryIndexFileContents.cjs.map +0 -1
- package/dist/lib/buildSliceLibraryIndexFileContents.d.ts +0 -10
- package/dist/lib/buildSliceLibraryIndexFileContents.js +0 -23
- package/dist/lib/buildSliceLibraryIndexFileContents.js.map +0 -1
package/dist/plugin.js
CHANGED
|
@@ -7,6 +7,7 @@ import { customTypeCreate } from "./hooks/customType-create.js";
|
|
|
7
7
|
import { customTypeDelete } from "./hooks/customType-delete.js";
|
|
8
8
|
import { customTypeLibraryRead } from "./hooks/customTypeLibrary-read.js";
|
|
9
9
|
import { customTypeRead } from "./hooks/customType-read.js";
|
|
10
|
+
import { customTypeRename } from "./hooks/customType-rename.js";
|
|
10
11
|
import { customTypeUpdate } from "./hooks/customType-update.js";
|
|
11
12
|
import { sliceAssetDelete } from "./hooks/slice-asset-delete.js";
|
|
12
13
|
import { sliceAssetRead } from "./hooks/slice-asset-read.js";
|
|
@@ -15,6 +16,7 @@ import { sliceCreate } from "./hooks/slice-create.js";
|
|
|
15
16
|
import { sliceDelete } from "./hooks/slice-delete.js";
|
|
16
17
|
import { sliceLibraryRead } from "./hooks/sliceLibrary-read.js";
|
|
17
18
|
import { sliceRead } from "./hooks/slice-read.js";
|
|
19
|
+
import { sliceRename } from "./hooks/slice-rename.js";
|
|
18
20
|
import { sliceSimulatorSetupRead } from "./hooks/sliceSimulator-setup-read.js";
|
|
19
21
|
import { sliceUpdate } from "./hooks/slice-update.js";
|
|
20
22
|
import { snippetRead } from "./hooks/snippet-read.js";
|
|
@@ -28,6 +30,7 @@ const plugin = defineSliceMachinePlugin({
|
|
|
28
30
|
setup({ hook }) {
|
|
29
31
|
hook("slice:create", sliceCreate);
|
|
30
32
|
hook("slice:update", sliceUpdate);
|
|
33
|
+
hook("slice:rename", sliceRename);
|
|
31
34
|
hook("slice:delete", sliceDelete);
|
|
32
35
|
hook("slice:read", sliceRead);
|
|
33
36
|
hook("slice:asset:update", sliceAssetUpdate);
|
|
@@ -36,6 +39,7 @@ const plugin = defineSliceMachinePlugin({
|
|
|
36
39
|
hook("slice-library:read", sliceLibraryRead);
|
|
37
40
|
hook("custom-type:create", customTypeCreate);
|
|
38
41
|
hook("custom-type:update", customTypeUpdate);
|
|
42
|
+
hook("custom-type:rename", customTypeRename);
|
|
39
43
|
hook("custom-type:delete", customTypeDelete);
|
|
40
44
|
hook("custom-type:read", customTypeRead);
|
|
41
45
|
hook("custom-type:asset:update", customTypeAssetUpdate);
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["../src/plugin.ts"],"sourcesContent":["import { defineSliceMachinePlugin } from \"@slicemachine/plugin-kit\";\n\nimport { name as pkgName } from \"../package.json\";\nimport { PluginOptions } from \"./types\";\n\nimport { customTypeAssetDelete } from \"./hooks/customType-asset-delete\";\nimport { customTypeAssetRead } from \"./hooks/customType-asset-read\";\nimport { customTypeAssetUpdate } from \"./hooks/customType-asset-update\";\nimport { customTypeCreate } from \"./hooks/customType-create\";\nimport { customTypeDelete } from \"./hooks/customType-delete\";\nimport { customTypeLibraryRead } from \"./hooks/customTypeLibrary-read\";\nimport { customTypeRead } from \"./hooks/customType-read\";\nimport { customTypeUpdate } from \"./hooks/customType-update\";\nimport { sliceAssetDelete } from \"./hooks/slice-asset-delete\";\nimport { sliceAssetRead } from \"./hooks/slice-asset-read\";\nimport { sliceAssetUpdate } from \"./hooks/slice-asset-update\";\nimport { sliceCreate } from \"./hooks/slice-create\";\nimport { sliceDelete } from \"./hooks/slice-delete\";\nimport { sliceLibraryRead } from \"./hooks/sliceLibrary-read\";\nimport { sliceRead } from \"./hooks/slice-read\";\nimport { sliceSimulatorSetupRead } from \"./hooks/sliceSimulator-setup-read\";\nimport { sliceUpdate } from \"./hooks/slice-update\";\nimport { snippetRead } from \"./hooks/snippet-read\";\n\nexport const plugin = defineSliceMachinePlugin<PluginOptions>({\n\tmeta: {\n\t\tname: pkgName,\n\t},\n\tdefaultOptions: {\n\t\tformat: true,\n\t},\n\tsetup({ hook }) {\n\t\thook(\"slice:create\", sliceCreate);\n\t\thook(\"slice:update\", sliceUpdate);\n\t\thook(\"slice:delete\", sliceDelete);\n\t\thook(\"slice:read\", sliceRead);\n\t\thook(\"slice:asset:update\", sliceAssetUpdate);\n\t\thook(\"slice:asset:delete\", sliceAssetDelete);\n\t\thook(\"slice:asset:read\", sliceAssetRead);\n\t\thook(\"slice-library:read\", sliceLibraryRead);\n\n\t\thook(\"custom-type:create\", customTypeCreate);\n\t\thook(\"custom-type:update\", customTypeUpdate);\n\t\thook(\"custom-type:delete\", customTypeDelete);\n\t\thook(\"custom-type:read\", customTypeRead);\n\t\thook(\"custom-type:asset:update\", customTypeAssetUpdate);\n\t\thook(\"custom-type:asset:delete\", customTypeAssetDelete);\n\t\thook(\"custom-type:asset:read\", customTypeAssetRead);\n\t\thook(\"custom-type-library:read\", customTypeLibraryRead);\n\n\t\thook(\"snippet:read\", snippetRead);\n\n\t\thook(\"slice-simulator:setup:read\", sliceSimulatorSetupRead);\n\t},\n});\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["../src/plugin.ts"],"sourcesContent":["import { defineSliceMachinePlugin } from \"@slicemachine/plugin-kit\";\n\nimport { name as pkgName } from \"../package.json\";\nimport { PluginOptions } from \"./types\";\n\nimport { customTypeAssetDelete } from \"./hooks/customType-asset-delete\";\nimport { customTypeAssetRead } from \"./hooks/customType-asset-read\";\nimport { customTypeAssetUpdate } from \"./hooks/customType-asset-update\";\nimport { customTypeCreate } from \"./hooks/customType-create\";\nimport { customTypeDelete } from \"./hooks/customType-delete\";\nimport { customTypeLibraryRead } from \"./hooks/customTypeLibrary-read\";\nimport { customTypeRead } from \"./hooks/customType-read\";\nimport { customTypeRename } from \"./hooks/customType-rename\";\nimport { customTypeUpdate } from \"./hooks/customType-update\";\nimport { sliceAssetDelete } from \"./hooks/slice-asset-delete\";\nimport { sliceAssetRead } from \"./hooks/slice-asset-read\";\nimport { sliceAssetUpdate } from \"./hooks/slice-asset-update\";\nimport { sliceCreate } from \"./hooks/slice-create\";\nimport { sliceDelete } from \"./hooks/slice-delete\";\nimport { sliceLibraryRead } from \"./hooks/sliceLibrary-read\";\nimport { sliceRead } from \"./hooks/slice-read\";\nimport { sliceRename } from \"./hooks/slice-rename\";\nimport { sliceSimulatorSetupRead } from \"./hooks/sliceSimulator-setup-read\";\nimport { sliceUpdate } from \"./hooks/slice-update\";\nimport { snippetRead } from \"./hooks/snippet-read\";\n\nexport const plugin = defineSliceMachinePlugin<PluginOptions>({\n\tmeta: {\n\t\tname: pkgName,\n\t},\n\tdefaultOptions: {\n\t\tformat: true,\n\t},\n\tsetup({ hook }) {\n\t\thook(\"slice:create\", sliceCreate);\n\t\thook(\"slice:update\", sliceUpdate);\n\t\thook(\"slice:rename\", sliceRename);\n\t\thook(\"slice:delete\", sliceDelete);\n\t\thook(\"slice:read\", sliceRead);\n\t\thook(\"slice:asset:update\", sliceAssetUpdate);\n\t\thook(\"slice:asset:delete\", sliceAssetDelete);\n\t\thook(\"slice:asset:read\", sliceAssetRead);\n\t\thook(\"slice-library:read\", sliceLibraryRead);\n\n\t\thook(\"custom-type:create\", customTypeCreate);\n\t\thook(\"custom-type:update\", customTypeUpdate);\n\t\thook(\"custom-type:rename\", customTypeRename);\n\t\thook(\"custom-type:delete\", customTypeDelete);\n\t\thook(\"custom-type:read\", customTypeRead);\n\t\thook(\"custom-type:asset:update\", customTypeAssetUpdate);\n\t\thook(\"custom-type:asset:delete\", customTypeAssetDelete);\n\t\thook(\"custom-type:asset:read\", customTypeAssetRead);\n\t\thook(\"custom-type-library:read\", customTypeLibraryRead);\n\n\t\thook(\"snippet:read\", snippetRead);\n\n\t\thook(\"slice-simulator:setup:read\", sliceSimulatorSetupRead);\n\t},\n});\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA0BO,MAAM,SAAS,yBAAwC;AAAA,EAC7D,MAAM;AAAA,IACL;AAAA,EACA;AAAA,EACD,gBAAgB;AAAA,IACf,QAAQ;AAAA,EACR;AAAA,EACD,MAAM,EAAE,QAAM;AACb,SAAK,gBAAgB,WAAW;AAChC,SAAK,gBAAgB,WAAW;AAChC,SAAK,gBAAgB,WAAW;AAChC,SAAK,gBAAgB,WAAW;AAChC,SAAK,cAAc,SAAS;AAC5B,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,oBAAoB,cAAc;AACvC,SAAK,sBAAsB,gBAAgB;AAE3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,sBAAsB,gBAAgB;AAC3C,SAAK,oBAAoB,cAAc;AACvC,SAAK,4BAA4B,qBAAqB;AACtD,SAAK,4BAA4B,qBAAqB;AACtD,SAAK,0BAA0B,mBAAmB;AAClD,SAAK,4BAA4B,qBAAqB;AAEtD,SAAK,gBAAgB,WAAW;AAEhC,SAAK,8BAA8B,uBAAuB;AAAA,EAC3D;AACA,CAAA;"}
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slicemachine/adapter-next",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Slice Machine adapter for Next.js.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -44,9 +44,10 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@prismicio/slice-simulator-core": "^0.2.6",
|
|
47
|
-
"@prismicio/types": "^0.2.
|
|
48
|
-
"@slicemachine/plugin-kit": "^0.1.
|
|
47
|
+
"@prismicio/types": "^0.2.4",
|
|
48
|
+
"@slicemachine/plugin-kit": "^0.1.7",
|
|
49
49
|
"common-tags": "^1.8.2",
|
|
50
|
+
"fs-extra": "^10.1.0",
|
|
50
51
|
"pascal-case": "^3.1.2",
|
|
51
52
|
"prismic-ts-codegen": "^0.1.5"
|
|
52
53
|
},
|
|
@@ -54,14 +55,15 @@
|
|
|
54
55
|
"@prismicio/mock": "^0.1.1",
|
|
55
56
|
"@size-limit/preset-small-lib": "^8.1.0",
|
|
56
57
|
"@types/common-tags": "^1.8.1",
|
|
58
|
+
"@types/fs-extra": "^9.0.13",
|
|
57
59
|
"@types/prettier": "^2.7.1",
|
|
58
60
|
"@types/react": "^18.0.25",
|
|
59
61
|
"@types/semver": "^7.3.13",
|
|
60
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
61
|
-
"@typescript-eslint/parser": "^5.
|
|
62
|
+
"@typescript-eslint/eslint-plugin": "^5.44.0",
|
|
63
|
+
"@typescript-eslint/parser": "^5.44.0",
|
|
62
64
|
"@vitejs/plugin-react": "^2.2.0",
|
|
63
|
-
"@vitest/coverage-c8": "^0.25.
|
|
64
|
-
"eslint": "^8.
|
|
65
|
+
"@vitest/coverage-c8": "^0.25.2",
|
|
66
|
+
"eslint": "^8.28.0",
|
|
65
67
|
"eslint-config-prettier": "^8.5.0",
|
|
66
68
|
"eslint-plugin-prettier": "^4.2.1",
|
|
67
69
|
"eslint-plugin-tsdoc": "^0.2.17",
|
|
@@ -71,11 +73,11 @@
|
|
|
71
73
|
"react": "^18.2.0",
|
|
72
74
|
"size-limit": "^8.1.0",
|
|
73
75
|
"standard-version": "^9.5.0",
|
|
74
|
-
"ts-morph": "^
|
|
75
|
-
"typescript": "^4.
|
|
76
|
-
"vite": "^3.2.
|
|
77
|
-
"vite-plugin-sdk": "^0.0.
|
|
78
|
-
"vitest": "^0.25.
|
|
76
|
+
"ts-morph": "^17.0.1",
|
|
77
|
+
"typescript": "^4.9.3",
|
|
78
|
+
"vite": "^3.2.4",
|
|
79
|
+
"vite-plugin-sdk": "^0.0.6",
|
|
80
|
+
"vitest": "^0.25.2"
|
|
79
81
|
},
|
|
80
82
|
"peerDependencies": {
|
|
81
83
|
"next": "^11 || ^12 || ^13",
|
|
@@ -27,12 +27,21 @@ export const customTypeLibraryRead: CustomTypeLibraryReadHook<
|
|
|
27
27
|
const ids: string[] = [];
|
|
28
28
|
await Promise.all(
|
|
29
29
|
childDirs.map(async (childDir) => {
|
|
30
|
-
const
|
|
30
|
+
const childDirContents = await fs.readdir(path.join(dirPath, childDir), {
|
|
31
|
+
withFileTypes: true,
|
|
32
|
+
});
|
|
33
|
+
const isCustomTypeDir = childDirContents.some((entry) => {
|
|
34
|
+
return entry.isFile() && entry.name === "index.json";
|
|
35
|
+
});
|
|
31
36
|
|
|
32
|
-
|
|
37
|
+
if (isCustomTypeDir) {
|
|
38
|
+
const modelPath = path.join(dirPath, childDir, "index.json");
|
|
33
39
|
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
const modelContents = await readJSONFile(modelPath);
|
|
41
|
+
|
|
42
|
+
if (isCustomTypeModel(modelContents)) {
|
|
43
|
+
ids.push(modelContents.id);
|
|
44
|
+
}
|
|
36
45
|
}
|
|
37
46
|
}),
|
|
38
47
|
);
|
|
@@ -7,7 +7,7 @@ import { stripIndent } from "common-tags";
|
|
|
7
7
|
import * as fs from "node:fs/promises";
|
|
8
8
|
import * as path from "node:path";
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
|
|
11
11
|
import { getJSOrTSXFileExtension } from "../lib/getJSOrTSXFileExtension";
|
|
12
12
|
import { pascalCase } from "../lib/pascalCase";
|
|
13
13
|
import { rejectIfNecessary } from "../lib/rejectIfNecessary";
|
|
@@ -35,24 +35,24 @@ const createModelFile = async ({ dir, data, helpers, options }: Args) => {
|
|
|
35
35
|
const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
|
|
36
36
|
const filePath = path.join(dir, `index.${getJSOrTSXFileExtension(options)}`);
|
|
37
37
|
const model = data.model;
|
|
38
|
-
const
|
|
38
|
+
const pascalName = pascalCase(model.name);
|
|
39
39
|
|
|
40
40
|
let contents: string;
|
|
41
41
|
|
|
42
42
|
if (options.typescript) {
|
|
43
43
|
contents = stripIndent`
|
|
44
44
|
import { SliceComponentProps } from "@prismicio/react";
|
|
45
|
-
import { ${
|
|
45
|
+
import { ${pascalName}Slice } from "./types";
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
* Props for \`${
|
|
48
|
+
* Props for \`${pascalName}\`.
|
|
49
49
|
*/
|
|
50
|
-
export type ${
|
|
50
|
+
export type ${pascalName}Props = SliceComponentProps<${pascalName}Slice>;
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Component for "${model.name}" Slices.
|
|
54
54
|
*/
|
|
55
|
-
const ${
|
|
55
|
+
const ${pascalName} = ({ slice }: ${pascalName}Props): React.Element => {
|
|
56
56
|
return (
|
|
57
57
|
<section
|
|
58
58
|
data-slice-type={slice.slice_type}
|
|
@@ -63,16 +63,16 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
|
|
|
63
63
|
);
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
export default ${
|
|
66
|
+
export default ${pascalName}
|
|
67
67
|
`;
|
|
68
68
|
} else {
|
|
69
69
|
contents = stripIndent`
|
|
70
70
|
/**
|
|
71
|
-
* @typedef {import("./types").${
|
|
72
|
-
* @typedef {import("@prismicio/react").SliceComponentProps<${
|
|
73
|
-
* @param {${
|
|
71
|
+
* @typedef {import("./types").${pascalName}Slice} ${pascalName}Slice
|
|
72
|
+
* @typedef {import("@prismicio/react").SliceComponentProps<${pascalName}Slice>} ${pascalName}Props
|
|
73
|
+
* @param {${pascalName}Props}
|
|
74
74
|
*/
|
|
75
|
-
const ${
|
|
75
|
+
const ${pascalName} = ({ slice }) => {
|
|
76
76
|
return (
|
|
77
77
|
<section
|
|
78
78
|
data-slice-type={slice.slice_type}
|
|
@@ -83,7 +83,7 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
|
|
|
83
83
|
);
|
|
84
84
|
};
|
|
85
85
|
|
|
86
|
-
export default ${
|
|
86
|
+
export default ${pascalName};
|
|
87
87
|
`;
|
|
88
88
|
}
|
|
89
89
|
|
|
@@ -94,31 +94,13 @@ const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
|
|
|
94
94
|
await fs.writeFile(filePath, contents);
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
-
const upsertSliceLibraryIndexFile = async ({
|
|
98
|
-
data,
|
|
99
|
-
actions,
|
|
100
|
-
helpers,
|
|
101
|
-
project,
|
|
102
|
-
options,
|
|
103
|
-
}: Omit<Args, "dir">) => {
|
|
104
|
-
const { filePath, contents } = await buildSliceLibraryIndexFileContents({
|
|
105
|
-
libraryID: data.libraryID,
|
|
106
|
-
actions,
|
|
107
|
-
helpers,
|
|
108
|
-
project,
|
|
109
|
-
options,
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
await fs.writeFile(filePath, contents);
|
|
113
|
-
};
|
|
114
|
-
|
|
115
97
|
export const sliceCreate: SliceCreateHook<PluginOptions> = async (
|
|
116
98
|
data,
|
|
117
99
|
context,
|
|
118
100
|
) => {
|
|
119
101
|
const dir = context.helpers.joinPathFromRoot(
|
|
120
102
|
data.libraryID,
|
|
121
|
-
pascalCase(data.model.
|
|
103
|
+
pascalCase(data.model.name),
|
|
122
104
|
);
|
|
123
105
|
|
|
124
106
|
await fs.mkdir(dir, { recursive: true });
|
|
@@ -133,7 +115,7 @@ export const sliceCreate: SliceCreateHook<PluginOptions> = async (
|
|
|
133
115
|
rejectIfNecessary(
|
|
134
116
|
await Promise.allSettled([
|
|
135
117
|
upsertGlobalContentTypes(context),
|
|
136
|
-
upsertSliceLibraryIndexFile({ data, ...context }),
|
|
118
|
+
upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context }),
|
|
137
119
|
]),
|
|
138
120
|
);
|
|
139
121
|
};
|
|
@@ -5,10 +5,10 @@ import type {
|
|
|
5
5
|
} from "@slicemachine/plugin-kit";
|
|
6
6
|
import * as fs from "node:fs/promises";
|
|
7
7
|
|
|
8
|
-
import { buildSliceLibraryIndexFileContents } from "../lib/buildSliceLibraryIndexFileContents";
|
|
9
8
|
import { pascalCase } from "../lib/pascalCase";
|
|
10
9
|
import { rejectIfNecessary } from "../lib/rejectIfNecessary";
|
|
11
10
|
import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
|
|
11
|
+
import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
|
|
12
12
|
|
|
13
13
|
import type { PluginOptions } from "../types";
|
|
14
14
|
|
|
@@ -19,30 +19,12 @@ type Args = {
|
|
|
19
19
|
const deleteSliceDir = async ({ data, helpers }: Args) => {
|
|
20
20
|
const dir = helpers.joinPathFromRoot(
|
|
21
21
|
data.libraryID,
|
|
22
|
-
pascalCase(data.model.
|
|
22
|
+
pascalCase(data.model.name),
|
|
23
23
|
);
|
|
24
24
|
|
|
25
25
|
await fs.rm(dir, { recursive: true });
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
const updateSliceLibraryIndexFile = async ({
|
|
29
|
-
data,
|
|
30
|
-
actions,
|
|
31
|
-
helpers,
|
|
32
|
-
project,
|
|
33
|
-
options,
|
|
34
|
-
}: Args) => {
|
|
35
|
-
const { filePath, contents } = await buildSliceLibraryIndexFileContents({
|
|
36
|
-
libraryID: data.libraryID,
|
|
37
|
-
actions,
|
|
38
|
-
helpers,
|
|
39
|
-
project,
|
|
40
|
-
options,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
await fs.writeFile(filePath, contents);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
28
|
export const sliceDelete: SliceDeleteHook<PluginOptions> = async (
|
|
47
29
|
data,
|
|
48
30
|
context,
|
|
@@ -52,7 +34,7 @@ export const sliceDelete: SliceDeleteHook<PluginOptions> = async (
|
|
|
52
34
|
rejectIfNecessary(
|
|
53
35
|
await Promise.allSettled([
|
|
54
36
|
upsertGlobalContentTypes(context),
|
|
55
|
-
|
|
37
|
+
upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context }),
|
|
56
38
|
]),
|
|
57
39
|
);
|
|
58
40
|
};
|
package/src/hooks/slice-read.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { SliceReadHook } from "@slicemachine/plugin-kit";
|
|
2
|
-
import * as
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
3
4
|
|
|
5
|
+
import { isSharedSliceModel } from "../lib/isSharedSliceModel";
|
|
4
6
|
import { readJSONFile } from "../lib/readJSONFile";
|
|
5
|
-
import { pascalCase } from "../lib/pascalCase";
|
|
6
7
|
|
|
7
8
|
import type { PluginOptions } from "../types";
|
|
8
9
|
|
|
@@ -10,15 +11,40 @@ export const sliceRead: SliceReadHook<PluginOptions> = async (
|
|
|
10
11
|
data,
|
|
11
12
|
{ helpers },
|
|
12
13
|
) => {
|
|
13
|
-
const
|
|
14
|
-
data.libraryID,
|
|
15
|
-
pascalCase(data.sliceID),
|
|
16
|
-
"model.json",
|
|
17
|
-
);
|
|
14
|
+
const libraryDir = helpers.joinPathFromRoot(data.libraryID);
|
|
18
15
|
|
|
19
|
-
|
|
16
|
+
// Ensure the directory exists.
|
|
17
|
+
await fs.mkdir(libraryDir, { recursive: true });
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
const childDirs = await fs.readdir(libraryDir, { withFileTypes: true });
|
|
20
|
+
|
|
21
|
+
// Find the first matching model.
|
|
22
|
+
const [model] = (
|
|
23
|
+
await Promise.all(
|
|
24
|
+
childDirs.map(async (childDir) => {
|
|
25
|
+
if (childDir.isDirectory()) {
|
|
26
|
+
const modelPath = path.join(libraryDir, childDir.name, "model.json");
|
|
27
|
+
|
|
28
|
+
const modelContents = await readJSONFile(modelPath);
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
isSharedSliceModel(modelContents) &&
|
|
32
|
+
modelContents.id === data.sliceID
|
|
33
|
+
) {
|
|
34
|
+
return modelContents;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}),
|
|
38
|
+
)
|
|
39
|
+
).filter((model): model is NonNullable<typeof model> => Boolean(model));
|
|
40
|
+
|
|
41
|
+
if (model) {
|
|
42
|
+
return {
|
|
43
|
+
model,
|
|
44
|
+
};
|
|
45
|
+
} else {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Did not find a Slice model with ID "${data.sliceID}" in the "${data.libraryID}" Slice Library.`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
24
50
|
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { SliceRenameHook } from "@slicemachine/plugin-kit";
|
|
2
|
+
import * as fse from "fs-extra";
|
|
3
|
+
|
|
4
|
+
import { pascalCase } from "../lib/pascalCase";
|
|
5
|
+
import { rejectIfNecessary } from "../lib/rejectIfNecessary";
|
|
6
|
+
import { updateSliceModelFile } from "../lib/updateSliceModelFile";
|
|
7
|
+
import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
|
|
8
|
+
import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
|
|
9
|
+
|
|
10
|
+
import type { PluginOptions } from "../types";
|
|
11
|
+
|
|
12
|
+
export const sliceRename: SliceRenameHook<PluginOptions> = async (
|
|
13
|
+
data,
|
|
14
|
+
context,
|
|
15
|
+
) => {
|
|
16
|
+
const { model: currentModel } = await context.actions.readSliceModel({
|
|
17
|
+
libraryID: data.libraryID,
|
|
18
|
+
sliceID: data.model.id,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
await fse.move(
|
|
22
|
+
context.helpers.joinPathFromRoot(
|
|
23
|
+
data.libraryID,
|
|
24
|
+
pascalCase(currentModel.name),
|
|
25
|
+
),
|
|
26
|
+
context.helpers.joinPathFromRoot(
|
|
27
|
+
data.libraryID,
|
|
28
|
+
pascalCase(data.model.name),
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
await updateSliceModelFile({
|
|
33
|
+
libraryID: data.libraryID,
|
|
34
|
+
model: data.model,
|
|
35
|
+
...context,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
rejectIfNecessary(
|
|
39
|
+
await Promise.allSettled([
|
|
40
|
+
upsertGlobalContentTypes(context),
|
|
41
|
+
upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context }),
|
|
42
|
+
]),
|
|
43
|
+
);
|
|
44
|
+
};
|
|
@@ -1,43 +1,19 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
SliceMachineContext,
|
|
3
|
-
SliceUpdateHook,
|
|
4
|
-
SliceUpdateHookData,
|
|
5
|
-
} from "@slicemachine/plugin-kit";
|
|
6
|
-
import * as fs from "node:fs/promises";
|
|
7
|
-
import * as path from "node:path";
|
|
1
|
+
import type { SliceUpdateHook } from "@slicemachine/plugin-kit";
|
|
8
2
|
|
|
9
|
-
import {
|
|
3
|
+
import { updateSliceModelFile } from "../lib/updateSliceModelFile";
|
|
10
4
|
import { upsertGlobalContentTypes } from "../lib/upsertGlobalContentTypes";
|
|
11
5
|
|
|
12
6
|
import type { PluginOptions } from "../types";
|
|
13
7
|
|
|
14
|
-
type Args = {
|
|
15
|
-
dir: string;
|
|
16
|
-
data: SliceUpdateHookData;
|
|
17
|
-
} & SliceMachineContext<PluginOptions>;
|
|
18
|
-
|
|
19
|
-
const updateModelFile = async ({ dir, data, helpers, options }: Args) => {
|
|
20
|
-
const filePath = path.join(dir, "model.json");
|
|
21
|
-
|
|
22
|
-
let contents = JSON.stringify(data.model);
|
|
23
|
-
|
|
24
|
-
if (options.format) {
|
|
25
|
-
contents = await helpers.format(contents, filePath);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
await fs.writeFile(filePath, contents);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
8
|
export const sliceUpdate: SliceUpdateHook<PluginOptions> = async (
|
|
32
9
|
data,
|
|
33
10
|
context,
|
|
34
11
|
) => {
|
|
35
|
-
|
|
36
|
-
data.libraryID,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
await updateModelFile({ dir, data, ...context });
|
|
12
|
+
await updateSliceModelFile({
|
|
13
|
+
libraryID: data.libraryID,
|
|
14
|
+
model: data.model,
|
|
15
|
+
...context,
|
|
16
|
+
});
|
|
41
17
|
|
|
42
18
|
await upsertGlobalContentTypes(context);
|
|
43
19
|
};
|
|
@@ -1,48 +1,38 @@
|
|
|
1
1
|
import type { SliceLibraryReadHook } from "@slicemachine/plugin-kit";
|
|
2
|
-
import * as prismicT from "@prismicio/types";
|
|
3
2
|
import * as fs from "node:fs/promises";
|
|
4
3
|
import * as path from "node:path";
|
|
5
4
|
|
|
5
|
+
import { isSharedSliceModel } from "../lib/isSharedSliceModel";
|
|
6
6
|
import { readJSONFile } from "../lib/readJSONFile";
|
|
7
7
|
|
|
8
8
|
import type { PluginOptions } from "../types";
|
|
9
9
|
|
|
10
|
-
const isSharedSliceModel = (
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
-
input: any,
|
|
13
|
-
): input is prismicT.SharedSliceModel => {
|
|
14
|
-
return (
|
|
15
|
-
typeof input === "object" &&
|
|
16
|
-
input !== null &&
|
|
17
|
-
"type" in input &&
|
|
18
|
-
input.type === prismicT.CustomTypeModelSliceType.SharedSlice
|
|
19
|
-
);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
10
|
export const sliceLibraryRead: SliceLibraryReadHook<PluginOptions> = async (
|
|
23
11
|
data,
|
|
24
12
|
{ helpers },
|
|
25
13
|
) => {
|
|
26
|
-
const
|
|
14
|
+
const dir = helpers.joinPathFromRoot(data.libraryID);
|
|
27
15
|
|
|
28
16
|
// Ensure the directory exists.
|
|
29
|
-
await fs.mkdir(
|
|
17
|
+
await fs.mkdir(dir, { recursive: true });
|
|
30
18
|
|
|
31
|
-
const childDirs = await fs.readdir(
|
|
19
|
+
const childDirs = await fs.readdir(dir, { withFileTypes: true });
|
|
32
20
|
|
|
33
21
|
const sliceIDs: string[] = [];
|
|
34
22
|
await Promise.all(
|
|
35
23
|
childDirs.map(async (childDir) => {
|
|
36
|
-
|
|
24
|
+
if (childDir.isDirectory()) {
|
|
25
|
+
const modelPath = path.join(dir, childDir.name, "model.json");
|
|
37
26
|
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
try {
|
|
28
|
+
const modelContents = await readJSONFile(modelPath);
|
|
40
29
|
|
|
41
|
-
|
|
42
|
-
|
|
30
|
+
if (isSharedSliceModel(modelContents)) {
|
|
31
|
+
sliceIDs.push(modelContents.id);
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
// noop
|
|
43
35
|
}
|
|
44
|
-
} catch {
|
|
45
|
-
// noop
|
|
46
36
|
}
|
|
47
37
|
}),
|
|
48
38
|
);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as prismicT from "@prismicio/types";
|
|
2
|
+
|
|
3
|
+
export const isSharedSliceModel = (
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
5
|
+
input: any,
|
|
6
|
+
): input is prismicT.SharedSliceModel => {
|
|
7
|
+
return (
|
|
8
|
+
typeof input === "object" &&
|
|
9
|
+
input !== null &&
|
|
10
|
+
"type" in input &&
|
|
11
|
+
input.type === prismicT.CustomTypeModelSliceType.SharedSlice &&
|
|
12
|
+
typeof input.id === "string"
|
|
13
|
+
);
|
|
14
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { SharedSliceModel } from "@prismicio/types";
|
|
2
|
+
import { SliceMachineContext } from "@slicemachine/plugin-kit";
|
|
3
|
+
import * as fs from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
import type { PluginOptions } from "../types";
|
|
6
|
+
import { pascalCase } from "./pascalCase";
|
|
7
|
+
|
|
8
|
+
type UpdateSliceModelFileArgs = {
|
|
9
|
+
libraryID: string;
|
|
10
|
+
model: SharedSliceModel;
|
|
11
|
+
} & SliceMachineContext<PluginOptions>;
|
|
12
|
+
|
|
13
|
+
export const updateSliceModelFile = async ({
|
|
14
|
+
libraryID,
|
|
15
|
+
model,
|
|
16
|
+
helpers,
|
|
17
|
+
options,
|
|
18
|
+
}: UpdateSliceModelFileArgs): Promise<void> => {
|
|
19
|
+
const filePath = helpers.joinPathFromRoot(
|
|
20
|
+
libraryID,
|
|
21
|
+
pascalCase(model.name),
|
|
22
|
+
"model.json",
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
let contents = JSON.stringify(model);
|
|
26
|
+
|
|
27
|
+
if (options.format) {
|
|
28
|
+
contents = await helpers.format(contents, filePath);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
await fs.writeFile(filePath, contents);
|
|
32
|
+
};
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { SliceMachineContext } from "@slicemachine/plugin-kit";
|
|
2
2
|
import { stripIndent } from "common-tags";
|
|
3
|
+
import * as fs from "node:fs/promises";
|
|
3
4
|
|
|
4
5
|
import { PluginOptions } from "../types";
|
|
5
6
|
|
|
6
7
|
import { pascalCase } from "./pascalCase";
|
|
7
8
|
|
|
8
|
-
type
|
|
9
|
+
type UpsertSliceLibraryIndexFileArgs = {
|
|
9
10
|
libraryID: string;
|
|
10
11
|
} & SliceMachineContext<PluginOptions>;
|
|
11
12
|
|
|
12
|
-
export const
|
|
13
|
-
args:
|
|
14
|
-
): Promise<
|
|
13
|
+
export const upsertSliceLibraryIndexFile = async (
|
|
14
|
+
args: UpsertSliceLibraryIndexFileArgs,
|
|
15
|
+
): Promise<void> => {
|
|
15
16
|
const filePath = args.helpers.joinPathFromRoot(
|
|
16
17
|
args.libraryID,
|
|
17
18
|
args.options.typescript ? "index.ts" : "index.js",
|
|
18
19
|
);
|
|
19
|
-
|
|
20
|
+
|
|
21
|
+
const slices = await args.actions.readAllSliceModelsForLibrary({
|
|
20
22
|
libraryID: args.libraryID,
|
|
21
23
|
});
|
|
22
24
|
|
|
@@ -24,9 +26,12 @@ export const buildSliceLibraryIndexFileContents = async (
|
|
|
24
26
|
import dynamic from 'next/dynamic'
|
|
25
27
|
|
|
26
28
|
export const components = {
|
|
27
|
-
${
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
${slices.map((slice) => {
|
|
30
|
+
const id = slice.model.id;
|
|
31
|
+
const fileName = pascalCase(slice.model.name);
|
|
32
|
+
|
|
33
|
+
return `${id}: dynamic(() => import('./${fileName}')),`;
|
|
34
|
+
})}
|
|
30
35
|
}
|
|
31
36
|
`;
|
|
32
37
|
|
|
@@ -34,5 +39,5 @@ export const buildSliceLibraryIndexFileContents = async (
|
|
|
34
39
|
contents = await args.helpers.format(contents, filePath);
|
|
35
40
|
}
|
|
36
41
|
|
|
37
|
-
|
|
42
|
+
await fs.writeFile(filePath, contents);
|
|
38
43
|
};
|
package/src/plugin.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { customTypeCreate } from "./hooks/customType-create";
|
|
|
10
10
|
import { customTypeDelete } from "./hooks/customType-delete";
|
|
11
11
|
import { customTypeLibraryRead } from "./hooks/customTypeLibrary-read";
|
|
12
12
|
import { customTypeRead } from "./hooks/customType-read";
|
|
13
|
+
import { customTypeRename } from "./hooks/customType-rename";
|
|
13
14
|
import { customTypeUpdate } from "./hooks/customType-update";
|
|
14
15
|
import { sliceAssetDelete } from "./hooks/slice-asset-delete";
|
|
15
16
|
import { sliceAssetRead } from "./hooks/slice-asset-read";
|
|
@@ -18,6 +19,7 @@ import { sliceCreate } from "./hooks/slice-create";
|
|
|
18
19
|
import { sliceDelete } from "./hooks/slice-delete";
|
|
19
20
|
import { sliceLibraryRead } from "./hooks/sliceLibrary-read";
|
|
20
21
|
import { sliceRead } from "./hooks/slice-read";
|
|
22
|
+
import { sliceRename } from "./hooks/slice-rename";
|
|
21
23
|
import { sliceSimulatorSetupRead } from "./hooks/sliceSimulator-setup-read";
|
|
22
24
|
import { sliceUpdate } from "./hooks/slice-update";
|
|
23
25
|
import { snippetRead } from "./hooks/snippet-read";
|
|
@@ -32,6 +34,7 @@ export const plugin = defineSliceMachinePlugin<PluginOptions>({
|
|
|
32
34
|
setup({ hook }) {
|
|
33
35
|
hook("slice:create", sliceCreate);
|
|
34
36
|
hook("slice:update", sliceUpdate);
|
|
37
|
+
hook("slice:rename", sliceRename);
|
|
35
38
|
hook("slice:delete", sliceDelete);
|
|
36
39
|
hook("slice:read", sliceRead);
|
|
37
40
|
hook("slice:asset:update", sliceAssetUpdate);
|
|
@@ -41,6 +44,7 @@ export const plugin = defineSliceMachinePlugin<PluginOptions>({
|
|
|
41
44
|
|
|
42
45
|
hook("custom-type:create", customTypeCreate);
|
|
43
46
|
hook("custom-type:update", customTypeUpdate);
|
|
47
|
+
hook("custom-type:rename", customTypeRename);
|
|
44
48
|
hook("custom-type:delete", customTypeDelete);
|
|
45
49
|
hook("custom-type:read", customTypeRead);
|
|
46
50
|
hook("custom-type:asset:update", customTypeAssetUpdate);
|