clean doc export with helpers

This commit is contained in:
Loren Norman 2025-06-24 14:11:37 -04:00
parent 18890c837b
commit d7d4ba4b67
3 changed files with 66 additions and 80 deletions

View file

@ -4,7 +4,7 @@ import BlocklyJSExporter from '#src/blockly_js_exporter.js'
const startTime = Date.now() const startTime = Date.now()
console.log("Starting Blockly Export") console.log("\nStarting Blockly Export")
console.log("=======================") console.log("=======================")
// clear the export directory // clear the export directory

View file

@ -1,37 +1,4 @@
import fs from 'fs' import { cleanDir, copyDir, write, totalBytesWritten } from "./export_util.js"
// make a tiny DSL
const withCleanDir = async (dirName, writeFunction) => {
if(fs.existsSync(dirName)) {
fs.rmSync(dirName, { recursive: true, force: true })
}
fs.mkdirSync(dirName)
console.log(`/${dirName}: clean`)
let totalBytesWritten = 0
const write = (filename, fileContents) => {
const
exportFilename = `${dirName}/${filename}`,
exportDirname = exportFilename.split("/").slice(0, -1).join("/"),
bytesToWrite = fileContents.length/1000
// ensure dir is present before writing
if(!fs.existsSync(exportDirname)) {
fs.mkdirSync(exportDirname, { recursive: true })
}
// write the file
fs.writeFileSync(exportFilename, fileContents)
// log and remember
console.log(`/${exportFilename} (${bytesToWrite}k)`)
totalBytesWritten += bytesToWrite
}
await writeFunction(write)
return totalBytesWritten
}
import toBlockMarkdown from "#src/docs/render_block.js" import toBlockMarkdown from "#src/docs/render_block.js"
import { allBlockDefinitionsAndPaths } from './src/importer/block_importer.js' import { allBlockDefinitionsAndPaths } from './src/importer/block_importer.js'
@ -45,64 +12,71 @@ const
const pretty = jsObject => JSON.stringify(jsObject, null, 2) + "\n" const pretty = jsObject => JSON.stringify(jsObject, null, 2) + "\n"
// import DefinitionSet from '#src/definition_set.js'
/** Begin Export Script */ /** Begin Export Script */
const startTime = Date.now() const startTime = Date.now()
let totalBytes = 0 console.log("\nStarting Documentation Export")
console.log("")
console.log("Starting Documentation Export")
console.log("=======================") console.log("=======================")
totalBytes += await withCleanDir("docs/blockly", () => { // const definitionSet = await DefinitionSet.load()
fs.cpSync("export", "docs/blockly", { recursive: true })
})
totalBytes += await withCleanDir("docs/blocks", async write => { cleanDir("docs/blockly")
const blockSidebar = { // TODO: instead, export exactly what we need
text: 'Blocks', copyDir("export", "docs/blockly")
items: map(categoryBlocksMap, (blocks, categoryName) => {
return { cleanDir("docs/blocks")
text: categoryName,
collapsed: true, // INIT SIDEBAR
items: [] const blockSidebar = {
} text: 'Blocks',
}) // items: map(definitionSet.toolbox.categories, categoryName => {
items: map(categoryBlocksMap, (blocks, categoryName) => {
return {
text: categoryName,
collapsed: true,
items: []
}
})
}
// forEach(definitionSet.blocks, ({ definitionPath, definition }) => {
forEach(allBlockDefinitionsAndPaths, ({ path, definition }) => {
// skip disabled blocks
if(definition.disabled) { return }
// set a default name if missing
// TODO: should happen in definition class
if(!definition.name) {
definition.name = capitalize(definition.type.replaceAll("_", " ").replace(/^io /, ""))
} }
forEach(allBlockDefinitionsAndPaths, ({ path, definition }) => { // mirror the blocks/**/*.js path structure
// skip disabled blocks const docPath = path.replace(/.js$/, '.md')
if(definition.disabled) { return } write(`docs/blocks/${docPath}`, toBlockMarkdown(definition)) // EXPORT MARKDOWN
// set a default name if missing // APPEND TO SIDEBAR
if(!definition.name) { const
definition.name = capitalize(definition.type.replaceAll("_", " ").replace(/^io /, "")) blockSidebarPath = `/blocks/${docPath.slice(0, -3)}`,
sidebarEntry = {
text: capitalize(definition.name),
link: blockSidebarPath
} }
// mirror the blocks/**/*.js path structure // definition.categories
const docPath = path.replace(/.js$/, '.md') // add block links to the appropriate sidebar
write(docPath, toBlockMarkdown(definition)) forEach(categoryBlocksMap, (categoryBlocks, categoryName) => {
// if category contains this block, add to its sidebar
const if(categoryBlocks.includes(definition.type)) {
blockSidebarPath = `/blocks/${docPath.slice(0, -3)}`, find(blockSidebar.items, { text: categoryName }).items.push(sidebarEntry)
sidebarEntry = { }
text: capitalize(definition.name),
link: blockSidebarPath
}
// add block links to the appropriate sidebar
forEach(categoryBlocksMap, (categoryBlocks, categoryName) => {
// if category contains this block, add to its sidebar
if(categoryBlocks.includes(definition.type)) {
find(blockSidebar.items, { text: categoryName }).items.push(sidebarEntry)
}
})
}) })
write('_blocks_sidebar.json', pretty(blockSidebar))
}) })
// WRITE SIDEBAR
write('docs/blocks/_blocks_sidebar.json', pretty(blockSidebar))
const elapsed = Date.now() - startTime const elapsed = Date.now() - startTime
console.log("=======================") console.log("=======================")
console.log(`🏁 Done. Wrote ${totalBytes.toString().slice(0,5)}k in ${elapsed}ms 🏁`) console.log(`🏁 Done. Wrote ${totalBytesWritten.toFixed(3)}k in ${elapsed}ms 🏁`)

View file

@ -12,8 +12,20 @@ export const
console.log(`/${dirName}: clean`) console.log(`/${dirName}: clean`)
}, },
copyDir = (from, to) => {
fs.cpSync(from, to, { recursive: true })
console.log(`/${from}/* copied to /${to}/*`)
},
write = (filename, fileContents) => { write = (filename, fileContents) => {
const bytesToWrite = fileContents.length/1000 const
dirName = filename.split("/").slice(0, -1).join("/"),
bytesToWrite = fileContents.length/1000
// ensure dir is present before writing
if(!fs.existsSync(dirName)) {
fs.mkdirSync(dirName, { recursive: true })
}
fs.writeFileSync(filename, fileContents) fs.writeFileSync(filename, fileContents)