1
0
Fork 0
freesewing/scripts/addpatterndocs.js

162 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-10-16 16:00:47 +02:00
/*
* This will create (SVG) images for all options of all patterns
* To do that, it will load the configuration from:
*
* config/pattern-options.js
*
* Based on that, it will generate SVG images for each option and store them
* in the relevant folder:
*
* markdown/org/docs/patterns/[pattern]/[option]/[pattern]_[option]_sample.svg
*
*/
const fs = require('fs')
const path = require('path')
const core = require('../packages/core/dist')
const theme = require('../packages/plugin-theme/dist')
const pi = require('../packages/pattern-info/dist')
const models = require('../packages/models/dist')
const wb32 = models.withBreasts.size32
const i18n = require('../packages/i18n/dist')
const capitalize = require('../packages/utils/capitalize')
const missing = []
const lacking = []
const extra = []
2021-10-16 16:00:47 +02:00
const file = 'en.md'
const subpages = [
'cutting',
'fabric',
'instructions',
'options',
'measurements',
'needs'
]
2021-10-17 09:24:17 +02:00
const patternDocsPage = pattern => `---
2021-10-16 16:00:47 +02:00
---
<PatternDocs pattern='${pattern}' />
`
2021-10-17 09:24:17 +02:00
const fixme = `---
---
2021-10-16 16:00:47 +02:00
<Fixme>
This documentation page is yet to be written.
Sorry for the inconvenience.
</Fixme>
`
const component = (comp, pattern) => `---
---
<Pattern${capitalize(comp)} pattern='${pattern}' />
`
const patternDocsSubPage = (pattern, sub) => {
switch (sub) {
case 'measurements':
case 'options':
return component(sub, pattern)
break;
default:
return fixme
}
}
2021-10-17 09:24:17 +02:00
const optionDocsPage = (pattern, option) => `---
title: ` + i18n.strings.en[`options.${pattern}.${option}.title`] +
"\n---\n\n" +
2021-10-16 16:10:42 +02:00
i18n.strings.en[`options.${pattern}.${option}.description`] + "\n"
2021-10-16 16:00:47 +02:00
const present = folder => {
try {
2021-11-13 14:55:04 +01:00
if (fs.readFileSync(path.join(folder, file))) return true
2021-10-16 16:00:47 +02:00
}
catch (err) {
return false
}
2021-11-13 14:55:04 +01:00
return false
2021-10-16 16:00:47 +02:00
}
const getSubFolders = folder => fs
.readdirSync(folder, {withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
2021-10-16 16:00:47 +02:00
const checkOptionDocs = () => {
const steps = ['markdown', 'org', 'docs', 'patterns']
for (const pattern of pi.list) {
2021-11-13 14:55:04 +01:00
// Index page
2021-10-16 16:00:47 +02:00
const folder = path.join(...steps, pattern)
const subFolders = getSubFolders(folder)
for (const page of subpages) {
if (subFolders.indexOf(page) === -1) lacking.push(path.join(folder, page))
}
for (const sub of subFolders) {
if (subpages.indexOf(sub) === -1) extra.push(path.join(folder, sub))
}
2021-10-16 16:00:47 +02:00
if (!present(folder)) {
fs.mkdirSync(folder, { recursive: true })
fs.writeFileSync(
path.join(folder, file),
patternDocsPage(pattern),
)
}
// Sub pages
for (const sub of subpages) {
const folder = path.join(...steps, pattern, sub)
if (!present(folder)) {
fs.mkdirSync(folder, { recursive: true })
fs.writeFileSync(
path.join(folder, file),
patternDocsSubPage(pattern, sub),
)
}
}
// Options
let optionFolders = getSubFolders(path.join(...steps, pattern, 'options'))
2021-10-16 16:00:47 +02:00
for (const option of pi.options[pattern]) {
// Remove this from the folder list
const i = optionFolders.indexOf(option.toLowerCase())
optionFolders.splice(i, 1)
const folder = path.join(...steps, pattern, 'options', option.toLowerCase())
2021-10-16 16:00:47 +02:00
if (!present(folder)) {
missing.push(path.join(folder, file))
fs.mkdirSync(folder, { recursive: true })
fs.writeFileSync(
path.join(folder, file),
optionDocsPage(pattern, option),
)
}
}
// Now check for extra folders
for (const subfolder of optionFolders) {
extra.push(path.join(...steps, pattern, 'options', subfolder))
}
2021-10-16 16:00:47 +02:00
}
if (missing.length < 1 && extra.length < 1 && lacking.length < 1) {
console.log("\n 🎉 Everything looks fine 😀\n")
} else {
if (missing.length > 0) {
console.log("\n", 'Added documenation pages for the following options:', "\n\n")
for (const line of missing) console.log(line)
}
if (extra.length > 0) {
console.log("\n", 'Found extra folders that should not be there:', "\n\n")
for (const line of extra) console.log(line)
}
2021-10-16 16:00:47 +02:00
}
}
checkOptionDocs()