Last active
May 19, 2023 21:14
-
-
Save angeloashmore/a38c7f222bb8df40bdeaa91d97c3c916 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const { createSliceMachineManager } = require("@slicemachine/manager"); | |
/** | |
* @param {import("@slicemachine/manager").SliceMachineManager} manager | |
*/ | |
async function loginIfNecessary(manager) { | |
const isLoggedIn = await manager.user.checkIsLoggedIn(); | |
if (!isLoggedIn) { | |
const loginSessionInfo = await manager.user.getLoginSessionInfo(); | |
console.log( | |
`\nPlease log in to Prismic here to continue: ${loginSessionInfo.url}` | |
); | |
await manager.user.nodeLoginSession({ port: loginSessionInfo.port }); | |
} | |
} | |
/** | |
* @param {import("@slicemachine/manager").SliceMachineManager} manager | |
* @param {import("@prismicio/types").CustomTypeModel} model | |
*/ | |
async function createCustomType(manager, model) { | |
console.log(`- Creating "${model.label}" Custom Type...`); | |
await manager.customTypes.createCustomType({ model }); | |
const config = await manager.project.getSliceMachineConfig(); | |
console.log( | |
`- Pushing "${model.label}" Custom Type to the "${config.repositoryName}" Prismic repository...` | |
); | |
await loginIfNecessary(manager); | |
try { | |
await manager.prismicRepository.pushChanges({ | |
changes: [ | |
{ | |
type: "CustomType", | |
id: model.id, | |
status: "NEW", | |
}, | |
], | |
confirmDeleteDocuments: false, | |
}); | |
} catch { | |
// Pushing may have failed if the custom type already exists. Try replacing it instead. | |
await manager.prismicRepository.pushChanges({ | |
changes: [ | |
{ | |
type: "CustomType", | |
id: model.id, | |
status: "MODIFIED", | |
}, | |
], | |
confirmDeleteDocuments: false, | |
}); | |
} | |
console.log( | |
`\nSuccessfully created a Custom Type named "${model.label}" in your project!\n\nHead back to the tutorial to continue.` | |
); | |
} | |
/** | |
* @param {import("@slicemachine/manager").SliceMachineManager} manager | |
* @param {import("@prismicio/types").SharedSliceModel} model | |
*/ | |
async function createSlice(manager, model) { | |
const { libraries } = await manager.slices.readAllSliceLibraries(); | |
const library = libraries[0]; | |
console.log(`Creating "${model.name}" Slice...`); | |
await manager.slices.createSlice({ libraryID: library.libraryID, model }); | |
const config = await manager.project.getSliceMachineConfig(); | |
console.log( | |
`Pushing "${model.name}" Slice to the "${config.repositoryName}" Prismic repository...` | |
); | |
await loginIfNecessary(manager); | |
try { | |
await manager.prismicRepository.pushChanges({ | |
changes: [ | |
{ | |
type: "Slice", | |
libraryID: library.libraryID, | |
id: model.id, | |
status: "NEW", | |
}, | |
], | |
confirmDeleteDocuments: false, | |
}); | |
} catch { | |
// Pushing may have failed if the Slice already exists. Try replacing it instead. | |
await manager.prismicRepository.pushChanges({ | |
changes: [ | |
{ | |
type: "Slice", | |
libraryID: library.libraryID, | |
id: model.id, | |
status: "MODIFIED", | |
}, | |
], | |
confirmDeleteDocuments: false, | |
}); | |
} | |
console.log( | |
`\nSuccessfully created a Slice named "${model.name}" in your project!\nHead back to the tutorial to continue.` | |
); | |
} | |
async function run() { | |
const command = process.argv.slice(2)[0]; | |
if (!command) { | |
console.error( | |
"Missing a command! Please run the commands as provided in the tutorial. :)" | |
); | |
return; | |
} | |
const manager = createSliceMachineManager(); | |
await manager.plugins.initPlugins(); | |
switch (command) { | |
case "homepage": { | |
await createCustomType(manager, { | |
id: "homepage", | |
label: "Homepage", | |
repeatable: false, | |
status: true, | |
json: { | |
Main: { | |
title: { | |
type: "StructuredText", | |
config: { | |
label: "Title", | |
placeholder: "Title of the document", | |
allowTargetBlank: true, | |
single: "heading1", | |
}, | |
}, | |
meta_description: { | |
type: "Text", | |
config: { | |
label: "Meta Description", | |
placeholder: "Description used for SEO", | |
}, | |
}, | |
slices: { | |
type: "Slices", | |
fieldset: "Slice Zone", | |
config: { | |
choices: { | |
hero: { type: "SharedSlice" }, | |
text: { type: "SharedSlice" }, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}); | |
break; | |
} | |
case "page": { | |
await createCustomType(manager, { | |
id: "page", | |
label: "Page", | |
repeatable: true, | |
status: true, | |
json: { | |
Main: { | |
uid: { | |
type: "UID", | |
config: { | |
label: "UID", | |
placeholder: "Identifier used in the page's URL", | |
}, | |
}, | |
title: { | |
type: "StructuredText", | |
config: { | |
label: "Title", | |
placeholder: "Title of the page", | |
allowTargetBlank: true, | |
single: "heading1", | |
}, | |
}, | |
meta_description: { | |
type: "Text", | |
config: { | |
label: "Meta Description", | |
placeholder: "Description used for SEO", | |
}, | |
}, | |
slices: { | |
type: "Slices", | |
fieldset: "Slice Zone", | |
config: { | |
choices: { | |
hero: { type: "SharedSlice" }, | |
text: { type: "SharedSlice" }, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}); | |
break; | |
} | |
case "settings": { | |
await createCustomType(manager, { | |
id: "settings", | |
label: "Settings", | |
repeatable: false, | |
status: true, | |
json: { | |
Main: { | |
site_title: { | |
type: "StructuredText", | |
config: { | |
label: "Site TItle", | |
placeholder: "Global title of the site", | |
allowTargetBlank: true, | |
single: "heading3", | |
}, | |
}, | |
navigation: { | |
type: "Group", | |
config: { | |
label: "Navigation", | |
fields: { | |
label: { | |
type: "Text", | |
config: { | |
label: "Label", | |
placeholder: "Label for the link", | |
}, | |
}, | |
link: { | |
type: "Link", | |
config: { | |
label: "Link", | |
placeholder: "", | |
allowTargetBlank: true, | |
select: null, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}); | |
break; | |
} | |
case "hero": { | |
await createSlice(manager, { | |
id: "hero", | |
type: "SharedSlice", | |
name: "Hero", | |
description: "Hero", | |
variations: [ | |
{ | |
id: "default", | |
name: "Default", | |
docURL: "...", | |
version: "initial", | |
description: "Default", | |
imageUrl: "", | |
primary: { | |
text: { | |
type: "StructuredText", | |
config: { | |
label: "Text", | |
placeholder: "Text displayed adjacent to image", | |
allowTargetBlank: true, | |
multi: "paragraph,heading1,heading3,strong,em", | |
}, | |
}, | |
image: { | |
type: "Image", | |
config: { label: "Image", constraint: {}, thumbnails: [] }, | |
}, | |
}, | |
items: { | |
button_link: { | |
type: "Link", | |
config: { | |
label: "Button Link", | |
placeholder: "Link for the button", | |
allowTargetBlank: true, | |
select: null, | |
}, | |
}, | |
button_label: { | |
type: "Text", | |
config: { | |
label: "Button Label", | |
placeholder: "Label for the button", | |
}, | |
}, | |
}, | |
}, | |
], | |
}); | |
break; | |
} | |
case "text": { | |
await createSlice(manager, { | |
id: "text", | |
type: "SharedSlice", | |
name: "Text", | |
description: "Text", | |
variations: [ | |
{ | |
id: "default", | |
name: "Default", | |
docURL: "...", | |
version: "initial", | |
description: "Default", | |
imageUrl: "", | |
primary: { | |
text: { | |
type: "StructuredText", | |
config: { | |
label: "Text", | |
placeholder: "Text with rich formatting", | |
allowTargetBlank: true, | |
multi: | |
"paragraph,preformatted,heading1,heading3,strong,em,hyperlink,image,embed,list-item,o-list-item,rtl", | |
}, | |
}, | |
}, | |
items: {}, | |
}, | |
], | |
}); | |
break; | |
} | |
default: { | |
console.error( | |
`${command} is not a valid command. Please run the commands as provided in the tutorial. :)` | |
); | |
process.exit(1); | |
} | |
} | |
} | |
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "prismic-next-app-router-tutorial-scripts", | |
"version": "1.0.0", | |
"bin": "./index.js", | |
"dependencies": { | |
"@slicemachine/manager": "^0.2.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment