@slicemachine/manager 0.11.4-dev-next-release.1 → 0.11.4-dev-next-release.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/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.cjs +3 -2
- package/dist/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.cjs.map +1 -1
- package/dist/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.js +3 -2
- package/dist/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.js.map +1 -1
- package/package.json +3 -3
- package/src/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.ts +8 -2
|
@@ -26,11 +26,12 @@ const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
|
|
|
26
26
|
const readHookCodec = t__namespace.type({
|
|
27
27
|
templates: t__namespace.array(t__namespace.type({
|
|
28
28
|
model: customtypes.SharedSlice,
|
|
29
|
-
|
|
29
|
+
componentContentsTemplate: t__namespace.string,
|
|
30
30
|
mocks: t__namespace.array(content.SharedSliceContent),
|
|
31
31
|
screenshots: t__namespace.record(t__namespace.string, t__namespace.any)
|
|
32
32
|
}))
|
|
33
33
|
});
|
|
34
|
+
const replacePascalNameToReplace = (template, name) => template.replaceAll("PascalNameToReplace", name);
|
|
34
35
|
class SliceTemplateLibraryManager extends BaseManager.BaseManager {
|
|
35
36
|
async readLibrary(args) {
|
|
36
37
|
assertPluginsInitialized.assertPluginsInitialized(this.sliceMachinePluginRunner);
|
|
@@ -93,7 +94,7 @@ class SliceTemplateLibraryManager extends BaseManager.BaseManager {
|
|
|
93
94
|
return this.slices.createSlice({
|
|
94
95
|
libraryID: targetLibrary.libraryID,
|
|
95
96
|
model: slice.model,
|
|
96
|
-
componentContents: slice.
|
|
97
|
+
componentContents: replacePascalNameToReplace(slice.componentContentsTemplate, slice.model.name)
|
|
97
98
|
});
|
|
98
99
|
});
|
|
99
100
|
const creationResults = await Promise.all(creationPromises);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SliceTemplateLibraryManager.cjs","sources":["../../../../src/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport { HookError } from \"@slicemachine/plugin-kit\";\nimport { BaseManager } from \"../BaseManager\";\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport { SharedSlice } from \"@prismicio/types-internal/lib/customtypes\";\nimport { SharedSliceContent } from \"@prismicio/types-internal/lib/content\";\n\ntype SliceTemplateLibraryManagerReadLibraryReturnType = {\n\ttemplates: {\n\t\tmodel: SharedSlice;\n\t\tscreenshots: Record<string, Buffer>;\n\t}[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceTemplateLibraryManagerCreateSlicesReturnType = {\n\tdata?: {\n\t\tsliceIDs: string[];\n\t};\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceTemplateLibraryReadLibraryData = {\n\ttemplateIDs?: string[];\n};\n\ntype SliceTemplateLibraryCreateSlicesData = SliceTemplateLibraryReadLibraryData;\n\nconst readHookCodec = t.type({\n\ttemplates: t.array(\n\t\tt.type({\n\t\t\tmodel: SharedSlice,\n\t\t\
|
|
1
|
+
{"version":3,"file":"SliceTemplateLibraryManager.cjs","sources":["../../../../src/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport { HookError } from \"@slicemachine/plugin-kit\";\nimport { BaseManager } from \"../BaseManager\";\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport { SharedSlice } from \"@prismicio/types-internal/lib/customtypes\";\nimport { SharedSliceContent } from \"@prismicio/types-internal/lib/content\";\n\ntype SliceTemplateLibraryManagerReadLibraryReturnType = {\n\ttemplates: {\n\t\tmodel: SharedSlice;\n\t\tscreenshots: Record<string, Buffer>;\n\t}[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceTemplateLibraryManagerCreateSlicesReturnType = {\n\tdata?: {\n\t\tsliceIDs: string[];\n\t};\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceTemplateLibraryReadLibraryData = {\n\ttemplateIDs?: string[];\n};\n\ntype SliceTemplateLibraryCreateSlicesData = SliceTemplateLibraryReadLibraryData;\n\nconst readHookCodec = t.type({\n\ttemplates: t.array(\n\t\tt.type({\n\t\t\tmodel: SharedSlice,\n\t\t\tcomponentContentsTemplate: t.string,\n\t\t\tmocks: t.array(SharedSliceContent),\n\t\t\tscreenshots: t.record(t.string, t.any),\n\t\t}),\n\t),\n});\n\nconst replacePascalNameToReplace = (template: string, name: string) =>\n\ttemplate.replaceAll(\"PascalNameToReplace\", name);\n\nexport class SliceTemplateLibraryManager extends BaseManager {\n\tasync readLibrary(\n\t\targs: SliceTemplateLibraryReadLibraryData,\n\t): Promise<SliceTemplateLibraryManagerReadLibraryReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"slice-template-library:read\",\n\t\t\targs,\n\t\t);\n\n\t\tconst { data, errors } = decodeHookResult(readHookCodec, hookResult);\n\n\t\treturn {\n\t\t\ttemplates: data.flat().flatMap((item) =>\n\t\t\t\titem.templates.map((t) => ({\n\t\t\t\t\tmodel: t.model,\n\t\t\t\t\tscreenshots: t.screenshots,\n\t\t\t\t})),\n\t\t\t),\n\t\t\terrors,\n\t\t};\n\t}\n\tasync createSlices(\n\t\targs: SliceTemplateLibraryCreateSlicesData,\n\t): Promise<SliceTemplateLibraryManagerCreateSlicesReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\t// Reading all available slice templates\n\t\tconst hookReadResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"slice-template-library:read\",\n\t\t\targs,\n\t\t);\n\t\tconst { data: readData, errors: readErrors } = decodeHookResult(\n\t\t\treadHookCodec,\n\t\t\thookReadResult,\n\t\t);\n\t\tconst templates = readData.flat().flatMap((item) => item.templates);\n\t\tif (readErrors.length > 0) {\n\t\t\treturn {\n\t\t\t\terrors: readErrors,\n\t\t\t};\n\t\t}\n\n\t\t// Extract all existing slice IDs into an array\n\t\tconst { models: allSlices, errors: readAllSlicesErrors } =\n\t\t\tawait this.slices.readAllSlices();\n\t\tif (readAllSlicesErrors.length > 0) {\n\t\t\treturn {\n\t\t\t\terrors: readAllSlicesErrors,\n\t\t\t};\n\t\t}\n\t\tconst existingIds: string[] = allSlices.map((slice) => slice.model.id);\n\n\t\t// Create a function to get the next available ID based on the baseId\n\t\tconst getNextAvailableSlice = (baseId: string) => {\n\t\t\tlet nextId = baseId;\n\t\t\tlet counter = 1;\n\t\t\twhile (existingIds.includes(nextId)) {\n\t\t\t\tcounter++;\n\t\t\t\tnextId = `${baseId}_${counter}`;\n\t\t\t}\n\t\t\texistingIds.push(nextId);\n\n\t\t\treturn { id: nextId, counter };\n\t\t};\n\n\t\t// Extract the slices to create from the template ids given in args\n\t\tconst slicesToCreate = templates.map((template) => {\n\t\t\tconst { id, counter } = getNextAvailableSlice(template.model.id);\n\n\t\t\treturn {\n\t\t\t\t...template,\n\t\t\t\tmodel: {\n\t\t\t\t\t...template.model,\n\t\t\t\t\tid,\n\t\t\t\t\tname:\n\t\t\t\t\t\tcounter > 1 ? template.model.name + counter : template.model.name,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\n\t\t// Get target library\n\t\t// Note: We only support adding template to the first library at the moment\n\t\tconst { libraries, errors: readAllSliceLibrariesErrors } =\n\t\t\tawait this.slices.readAllSliceLibraries();\n\t\tif (readAllSliceLibrariesErrors.length > 0) {\n\t\t\treturn {\n\t\t\t\terrors: readAllSliceLibrariesErrors,\n\t\t\t};\n\t\t}\n\t\tconst targetLibrary = libraries[0];\n\n\t\t// Initiate the slice creation process for all slices\n\t\tconst creationPromises = slicesToCreate.map((slice) => {\n\t\t\treturn this.slices.createSlice({\n\t\t\t\tlibraryID: targetLibrary.libraryID,\n\t\t\t\tmodel: slice.model,\n\t\t\t\tcomponentContents: replacePascalNameToReplace(\n\t\t\t\t\tslice.componentContentsTemplate,\n\t\t\t\t\tslice.model.name,\n\t\t\t\t),\n\t\t\t});\n\t\t});\n\n\t\t// Wait for all slices to be created\n\t\tconst creationResults = await Promise.all(creationPromises);\n\t\t// Check for any errors in the creation results\n\t\tconst creationErrors = creationResults.flatMap((result) => result.errors);\n\t\tif (creationErrors.length > 0) {\n\t\t\treturn {\n\t\t\t\terrors: creationErrors,\n\t\t\t};\n\t\t}\n\n\t\tconst mocksPromises = slicesToCreate.map((slice) => {\n\t\t\treturn this.slices.updateSliceMocks({\n\t\t\t\tlibraryID: targetLibrary.libraryID,\n\t\t\t\tsliceID: slice.model.id,\n\t\t\t\tmocks: slice.mocks,\n\t\t\t});\n\t\t});\n\n\t\tconst mocksResults = await Promise.all(mocksPromises);\n\n\t\tconst sliceScreenshotsPromises = slicesToCreate.map((slice) => {\n\t\t\tconst screenshotPromises = Object.entries(slice.screenshots).map(\n\t\t\t\t([variationID, data]) => {\n\t\t\t\t\treturn this.slices.updateSliceScreenshot({\n\t\t\t\t\t\tlibraryID: targetLibrary.libraryID,\n\t\t\t\t\t\tsliceID: slice.model.id,\n\t\t\t\t\t\tvariationID,\n\t\t\t\t\t\tdata,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t);\n\n\t\t\treturn Promise.all(screenshotPromises);\n\t\t});\n\n\t\tconst sliceScreenshotsResults = await Promise.all(sliceScreenshotsPromises);\n\t\tconst mocksErrors = mocksResults.flatMap((result) => result.errors);\n\t\tconst screenshotErrors = sliceScreenshotsResults.flatMap((result) =>\n\t\t\tresult.flat().flatMap((r) => r.errors),\n\t\t);\n\n\t\t// Extract the slice IDs from the creation results (assuming each result has an ID)\n\t\tconst sliceIDs = slicesToCreate.map((slice) => slice.model.id);\n\n\t\treturn {\n\t\t\terrors: [...mocksErrors, ...screenshotErrors],\n\t\t\tdata: {\n\t\t\t\tsliceIDs,\n\t\t\t},\n\t\t};\n\t}\n}\n"],"names":["t","SharedSlice","SharedSliceContent","BaseManager","assertPluginsInitialized","decodeHookResult"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,MAAM,gBAAgBA,aAAE,KAAK;AAAA,EAC5B,WAAWA,aAAE,MACZA,aAAE,KAAK;AAAA,IACN,OAAOC,YAAA;AAAA,IACP,2BAA2BD,aAAE;AAAA,IAC7B,OAAOA,aAAE,MAAME,0BAAkB;AAAA,IACjC,aAAaF,aAAE,OAAOA,aAAE,QAAQA,aAAE,GAAG;AAAA,EAAA,CACrC,CAAC;AAEH,CAAA;AAED,MAAM,6BAA6B,CAAC,UAAkB,SACrD,SAAS,WAAW,uBAAuB,IAAI;AAE1C,MAAO,oCAAoCG,YAAAA,YAAW;AAAA,EAC3D,MAAM,YACL,MAAyC;AAEzCC,sDAAyB,KAAK,wBAAwB;AAEtD,UAAM,aAAa,MAAM,KAAK,yBAAyB,SACtD,+BACA,IAAI;AAGL,UAAM,EAAE,MAAM,OAAA,IAAWC,iBAAAA,iBAAiB,eAAe,UAAU;AAE5D,WAAA;AAAA,MACN,WAAW,KAAK,KAAO,EAAA,QAAQ,CAAC,SAC/B,KAAK,UAAU,IAAI,CAACL,QAAO;AAAA,QAC1B,OAAOA,GAAE;AAAA,QACT,aAAaA,GAAE;AAAA,QACd,CAAC;AAAA,MAEJ;AAAA,IAAA;AAAA,EAEF;AAAA,EACA,MAAM,aACL,MAA0C;AAE1CI,sDAAyB,KAAK,wBAAwB;AAGtD,UAAM,iBAAiB,MAAM,KAAK,yBAAyB,SAC1D,+BACA,IAAI;AAEC,UAAA,EAAE,MAAM,UAAU,QAAQ,eAAeC,iBAAA,iBAC9C,eACA,cAAc;AAET,UAAA,YAAY,SAAS,KAAI,EAAG,QAAQ,C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
8
|
templates: t.array(t.type({
|
|
9
9
|
model: SharedSlice,
|
|
10
|
-
|
|
10
|
+
componentContentsTemplate: t.string,
|
|
11
11
|
mocks: t.array(SharedSliceContent),
|
|
12
12
|
screenshots: t.record(t.string, t.any)
|
|
13
13
|
}))
|
|
14
14
|
});
|
|
15
|
+
const replacePascalNameToReplace = (template, name) => template.replaceAll("PascalNameToReplace", name);
|
|
15
16
|
class SliceTemplateLibraryManager extends BaseManager {
|
|
16
17
|
async readLibrary(args) {
|
|
17
18
|
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
|
@@ -74,7 +75,7 @@ class SliceTemplateLibraryManager extends BaseManager {
|
|
|
74
75
|
return this.slices.createSlice({
|
|
75
76
|
libraryID: targetLibrary.libraryID,
|
|
76
77
|
model: slice.model,
|
|
77
|
-
componentContents: slice.
|
|
78
|
+
componentContents: replacePascalNameToReplace(slice.componentContentsTemplate, slice.model.name)
|
|
78
79
|
});
|
|
79
80
|
});
|
|
80
81
|
const creationResults = await Promise.all(creationPromises);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SliceTemplateLibraryManager.js","sources":["../../../../src/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport { HookError } from \"@slicemachine/plugin-kit\";\nimport { BaseManager } from \"../BaseManager\";\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport { SharedSlice } from \"@prismicio/types-internal/lib/customtypes\";\nimport { SharedSliceContent } from \"@prismicio/types-internal/lib/content\";\n\ntype SliceTemplateLibraryManagerReadLibraryReturnType = {\n\ttemplates: {\n\t\tmodel: SharedSlice;\n\t\tscreenshots: Record<string, Buffer>;\n\t}[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceTemplateLibraryManagerCreateSlicesReturnType = {\n\tdata?: {\n\t\tsliceIDs: string[];\n\t};\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceTemplateLibraryReadLibraryData = {\n\ttemplateIDs?: string[];\n};\n\ntype SliceTemplateLibraryCreateSlicesData = SliceTemplateLibraryReadLibraryData;\n\nconst readHookCodec = t.type({\n\ttemplates: t.array(\n\t\tt.type({\n\t\t\tmodel: SharedSlice,\n\t\t\
|
|
1
|
+
{"version":3,"file":"SliceTemplateLibraryManager.js","sources":["../../../../src/managers/sliceTemplateLibrary/SliceTemplateLibraryManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport { HookError } from \"@slicemachine/plugin-kit\";\nimport { BaseManager } from \"../BaseManager\";\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport { SharedSlice } from \"@prismicio/types-internal/lib/customtypes\";\nimport { SharedSliceContent } from \"@prismicio/types-internal/lib/content\";\n\ntype SliceTemplateLibraryManagerReadLibraryReturnType = {\n\ttemplates: {\n\t\tmodel: SharedSlice;\n\t\tscreenshots: Record<string, Buffer>;\n\t}[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceTemplateLibraryManagerCreateSlicesReturnType = {\n\tdata?: {\n\t\tsliceIDs: string[];\n\t};\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceTemplateLibraryReadLibraryData = {\n\ttemplateIDs?: string[];\n};\n\ntype SliceTemplateLibraryCreateSlicesData = SliceTemplateLibraryReadLibraryData;\n\nconst readHookCodec = t.type({\n\ttemplates: t.array(\n\t\tt.type({\n\t\t\tmodel: SharedSlice,\n\t\t\tcomponentContentsTemplate: t.string,\n\t\t\tmocks: t.array(SharedSliceContent),\n\t\t\tscreenshots: t.record(t.string, t.any),\n\t\t}),\n\t),\n});\n\nconst replacePascalNameToReplace = (template: string, name: string) =>\n\ttemplate.replaceAll(\"PascalNameToReplace\", name);\n\nexport class SliceTemplateLibraryManager extends BaseManager {\n\tasync readLibrary(\n\t\targs: SliceTemplateLibraryReadLibraryData,\n\t): Promise<SliceTemplateLibraryManagerReadLibraryReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"slice-template-library:read\",\n\t\t\targs,\n\t\t);\n\n\t\tconst { data, errors } = decodeHookResult(readHookCodec, hookResult);\n\n\t\treturn {\n\t\t\ttemplates: data.flat().flatMap((item) =>\n\t\t\t\titem.templates.map((t) => ({\n\t\t\t\t\tmodel: t.model,\n\t\t\t\t\tscreenshots: t.screenshots,\n\t\t\t\t})),\n\t\t\t),\n\t\t\terrors,\n\t\t};\n\t}\n\tasync createSlices(\n\t\targs: SliceTemplateLibraryCreateSlicesData,\n\t): Promise<SliceTemplateLibraryManagerCreateSlicesReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\t// Reading all available slice templates\n\t\tconst hookReadResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"slice-template-library:read\",\n\t\t\targs,\n\t\t);\n\t\tconst { data: readData, errors: readErrors } = decodeHookResult(\n\t\t\treadHookCodec,\n\t\t\thookReadResult,\n\t\t);\n\t\tconst templates = readData.flat().flatMap((item) => item.templates);\n\t\tif (readErrors.length > 0) {\n\t\t\treturn {\n\t\t\t\terrors: readErrors,\n\t\t\t};\n\t\t}\n\n\t\t// Extract all existing slice IDs into an array\n\t\tconst { models: allSlices, errors: readAllSlicesErrors } =\n\t\t\tawait this.slices.readAllSlices();\n\t\tif (readAllSlicesErrors.length > 0) {\n\t\t\treturn {\n\t\t\t\terrors: readAllSlicesErrors,\n\t\t\t};\n\t\t}\n\t\tconst existingIds: string[] = allSlices.map((slice) => slice.model.id);\n\n\t\t// Create a function to get the next available ID based on the baseId\n\t\tconst getNextAvailableSlice = (baseId: string) => {\n\t\t\tlet nextId = baseId;\n\t\t\tlet counter = 1;\n\t\t\twhile (existingIds.includes(nextId)) {\n\t\t\t\tcounter++;\n\t\t\t\tnextId = `${baseId}_${counter}`;\n\t\t\t}\n\t\t\texistingIds.push(nextId);\n\n\t\t\treturn { id: nextId, counter };\n\t\t};\n\n\t\t// Extract the slices to create from the template ids given in args\n\t\tconst slicesToCreate = templates.map((template) => {\n\t\t\tconst { id, counter } = getNextAvailableSlice(template.model.id);\n\n\t\t\treturn {\n\t\t\t\t...template,\n\t\t\t\tmodel: {\n\t\t\t\t\t...template.model,\n\t\t\t\t\tid,\n\t\t\t\t\tname:\n\t\t\t\t\t\tcounter > 1 ? template.model.name + counter : template.model.name,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\n\t\t// Get target library\n\t\t// Note: We only support adding template to the first library at the moment\n\t\tconst { libraries, errors: readAllSliceLibrariesErrors } =\n\t\t\tawait this.slices.readAllSliceLibraries();\n\t\tif (readAllSliceLibrariesErrors.length > 0) {\n\t\t\treturn {\n\t\t\t\terrors: readAllSliceLibrariesErrors,\n\t\t\t};\n\t\t}\n\t\tconst targetLibrary = libraries[0];\n\n\t\t// Initiate the slice creation process for all slices\n\t\tconst creationPromises = slicesToCreate.map((slice) => {\n\t\t\treturn this.slices.createSlice({\n\t\t\t\tlibraryID: targetLibrary.libraryID,\n\t\t\t\tmodel: slice.model,\n\t\t\t\tcomponentContents: replacePascalNameToReplace(\n\t\t\t\t\tslice.componentContentsTemplate,\n\t\t\t\t\tslice.model.name,\n\t\t\t\t),\n\t\t\t});\n\t\t});\n\n\t\t// Wait for all slices to be created\n\t\tconst creationResults = await Promise.all(creationPromises);\n\t\t// Check for any errors in the creation results\n\t\tconst creationErrors = creationResults.flatMap((result) => result.errors);\n\t\tif (creationErrors.length > 0) {\n\t\t\treturn {\n\t\t\t\terrors: creationErrors,\n\t\t\t};\n\t\t}\n\n\t\tconst mocksPromises = slicesToCreate.map((slice) => {\n\t\t\treturn this.slices.updateSliceMocks({\n\t\t\t\tlibraryID: targetLibrary.libraryID,\n\t\t\t\tsliceID: slice.model.id,\n\t\t\t\tmocks: slice.mocks,\n\t\t\t});\n\t\t});\n\n\t\tconst mocksResults = await Promise.all(mocksPromises);\n\n\t\tconst sliceScreenshotsPromises = slicesToCreate.map((slice) => {\n\t\t\tconst screenshotPromises = Object.entries(slice.screenshots).map(\n\t\t\t\t([variationID, data]) => {\n\t\t\t\t\treturn this.slices.updateSliceScreenshot({\n\t\t\t\t\t\tlibraryID: targetLibrary.libraryID,\n\t\t\t\t\t\tsliceID: slice.model.id,\n\t\t\t\t\t\tvariationID,\n\t\t\t\t\t\tdata,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t);\n\n\t\t\treturn Promise.all(screenshotPromises);\n\t\t});\n\n\t\tconst sliceScreenshotsResults = await Promise.all(sliceScreenshotsPromises);\n\t\tconst mocksErrors = mocksResults.flatMap((result) => result.errors);\n\t\tconst screenshotErrors = sliceScreenshotsResults.flatMap((result) =>\n\t\t\tresult.flat().flatMap((r) => r.errors),\n\t\t);\n\n\t\t// Extract the slice IDs from the creation results (assuming each result has an ID)\n\t\tconst sliceIDs = slicesToCreate.map((slice) => slice.model.id);\n\n\t\treturn {\n\t\t\terrors: [...mocksErrors, ...screenshotErrors],\n\t\t\tdata: {\n\t\t\t\tsliceIDs,\n\t\t\t},\n\t\t};\n\t}\n}\n"],"names":["t"],"mappings":";;;;;;AA8BA,MAAM,gBAAgB,EAAE,KAAK;AAAA,EAC5B,WAAW,EAAE,MACZ,EAAE,KAAK;AAAA,IACN,OAAO;AAAA,IACP,2BAA2B,EAAE;AAAA,IAC7B,OAAO,EAAE,MAAM,kBAAkB;AAAA,IACjC,aAAa,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG;AAAA,EAAA,CACrC,CAAC;AAEH,CAAA;AAED,MAAM,6BAA6B,CAAC,UAAkB,SACrD,SAAS,WAAW,uBAAuB,IAAI;AAE1C,MAAO,oCAAoC,YAAW;AAAA,EAC3D,MAAM,YACL,MAAyC;AAEzC,6BAAyB,KAAK,wBAAwB;AAEtD,UAAM,a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kBAAkB;AAAA,IAAA,CACrC;AAED,UAAM,0BAA0B,MAAM,QAAQ,IAAI,wBAAwB;AAC1E,UAAM,cAAc,aAAa,QAAQ,CAAC,WAAW,OAAO,MAAM;AAClE,UAAM,mBAAmB,wBAAwB,QAAQ,CAAC,WACzD,OAAO,KAAM,EAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;AAIvC,UAAM,WAAW,eAAe,IAAI,CAAC,UAAU,MAAM,MAAM,EAAE;AAEtD,WAAA;AAAA,MACN,QAAQ,CAAC,GAAG,aAAa,GAAG,gBAAgB;AAAA,MAC5C,MAAM;AAAA,QACL;AAAA,MACA;AAAA,IAAA;AAAA,EAEH;AACA;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slicemachine/manager",
|
|
3
|
-
"version": "0.11.4-dev-next-release.
|
|
3
|
+
"version": "0.11.4-dev-next-release.3",
|
|
4
4
|
"description": "Manage all aspects of a Slice Machine project.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"@prismicio/custom-types-client": "^1.2.0-alpha.0",
|
|
69
69
|
"@prismicio/mocks": "^2.0.0-alpha.2",
|
|
70
70
|
"@prismicio/types-internal": "^2.0.0",
|
|
71
|
-
"@slicemachine/plugin-kit": "^0.4.18-dev-next-release.
|
|
71
|
+
"@slicemachine/plugin-kit": "^0.4.18-dev-next-release.3",
|
|
72
72
|
"@wooorm/starry-night": "^1.6.0",
|
|
73
73
|
"analytics-node": "^6.2.0",
|
|
74
74
|
"cookie": "^0.5.0",
|
|
@@ -134,5 +134,5 @@
|
|
|
134
134
|
"engines": {
|
|
135
135
|
"node": ">=14.15.0"
|
|
136
136
|
},
|
|
137
|
-
"gitHead": "
|
|
137
|
+
"gitHead": "d83db8b71e3778796a7beba1c69d0202d2775571"
|
|
138
138
|
}
|
|
@@ -32,13 +32,16 @@ const readHookCodec = t.type({
|
|
|
32
32
|
templates: t.array(
|
|
33
33
|
t.type({
|
|
34
34
|
model: SharedSlice,
|
|
35
|
-
|
|
35
|
+
componentContentsTemplate: t.string,
|
|
36
36
|
mocks: t.array(SharedSliceContent),
|
|
37
37
|
screenshots: t.record(t.string, t.any),
|
|
38
38
|
}),
|
|
39
39
|
),
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
+
const replacePascalNameToReplace = (template: string, name: string) =>
|
|
43
|
+
template.replaceAll("PascalNameToReplace", name);
|
|
44
|
+
|
|
42
45
|
export class SliceTemplateLibraryManager extends BaseManager {
|
|
43
46
|
async readLibrary(
|
|
44
47
|
args: SliceTemplateLibraryReadLibraryData,
|
|
@@ -137,7 +140,10 @@ export class SliceTemplateLibraryManager extends BaseManager {
|
|
|
137
140
|
return this.slices.createSlice({
|
|
138
141
|
libraryID: targetLibrary.libraryID,
|
|
139
142
|
model: slice.model,
|
|
140
|
-
componentContents:
|
|
143
|
+
componentContents: replacePascalNameToReplace(
|
|
144
|
+
slice.componentContentsTemplate,
|
|
145
|
+
slice.model.name,
|
|
146
|
+
),
|
|
141
147
|
});
|
|
142
148
|
});
|
|
143
149
|
|