1
0
Fork 0
freesewing/sites/shared/prebuild/lab.mjs

79 lines
2.4 KiB
JavaScript
Raw Normal View History

import fs_ from 'fs'
2022-02-09 18:58:16 +01:00
import path from 'path'
2022-08-29 08:29:55 +02:00
import { capitalize } from '../utils.mjs'
import { designsByType, plugins, designs } from '../../../config/software/index.mjs'
2022-02-09 18:58:16 +01:00
const fs = fs_.promises
2022-02-09 18:58:16 +01:00
const header = `/*
*
* This page was auto-generated by the prebuild script
2022-02-09 18:58:16 +01:00
* Any changes you make to it will be lost on the next (pre)build.
*
* If you want to make changes, update the pageTemplate in:
*
2022-06-17 12:02:09 +02:00
* sites/shared/prebuild/lab.mjs
2022-02-09 18:58:16 +01:00
*
2022-08-29 08:29:55 +02:00
*/`
const pageTemplate = (design) => `${header}
2022-08-29 08:29:55 +02:00
import { ${capitalize(design)} } from 'designs/${design}/src/index.mjs'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { WorkbenchPage } from 'site/page-templates/workbench.mjs'
const Page = (props) => <WorkbenchPage {...props} design={${capitalize(design)}} version="next"/>
export default Page
export async function getStaticProps({ locale }) {
return {
props: {
...(await serverSideTranslations(locale)),
}
}
}
`
2022-02-09 18:58:16 +01:00
/*
* Main method that does what needs doing
*/
2022-02-09 18:58:16 +01:00
export const prebuildLab = async (site) => {
const promises = []
2022-06-17 12:02:09 +02:00
for (const section in designsByType) {
// Iterate over sections
2022-06-17 12:02:09 +02:00
console.log(`Generating pages for ${section} designs`)
for (const design in designsByType[section]) {
// Generate pattern pages for next
2022-02-09 18:58:16 +01:00
console.log(` - ${design}`)
const page = pageTemplate(design)
2022-06-17 12:02:09 +02:00
const pages = ['..', 'lab', 'pages']
await fs.mkdir(path.resolve(...pages, 'v', 'next'), { recursive: true })
promises.push(
fs.writeFile(path.resolve(...pages, `${design}.mjs`), page),
fs.writeFile(path.resolve(...pages, section, `${design}.mjs`), page)
)
2022-02-09 18:58:16 +01:00
}
}
// Write designs file
const header =
'// This file is auto-generated by the prebuild script | Any changes will be overwritten\n'
const nl = '\n'
promises.push(
fs.writeFile(
path.resolve('..', 'lab', 'prebuild', 'designs.mjs'),
`${header}export const designs = ${JSON.stringify(Object.keys(designs))}${nl}`
),
fs.writeFile(
path.resolve('..', 'lab', 'prebuild', 'plugins.mjs'),
`${header}export const plugins = ${JSON.stringify(Object.keys(plugins))}${nl}`
),
fs.writeFile(
path.resolve('..', 'lab', 'prebuild', 'designs-by-type.mjs'),
`${header}export const designsByType = ${JSON.stringify(designsByType)}${nl}`
)
)
await Promise.all(promises)
2022-02-09 18:58:16 +01:00
}