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()
console.log("Starting Blockly Export")
console.log("\nStarting Blockly Export")
console.log("=======================")
// clear the export directory

View file

@ -1,37 +1,4 @@
import fs from 'fs'
// 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 { cleanDir, copyDir, write, totalBytesWritten } from "./export_util.js"
import toBlockMarkdown from "#src/docs/render_block.js"
import { allBlockDefinitionsAndPaths } from './src/importer/block_importer.js'
@ -45,64 +12,71 @@ const
const pretty = jsObject => JSON.stringify(jsObject, null, 2) + "\n"
// import DefinitionSet from '#src/definition_set.js'
/** Begin Export Script */
const startTime = Date.now()
let totalBytes = 0
console.log("")
console.log("Starting Documentation Export")
console.log("\nStarting Documentation Export")
console.log("=======================")
totalBytes += await withCleanDir("docs/blockly", () => {
fs.cpSync("export", "docs/blockly", { recursive: true })
})
// const definitionSet = await DefinitionSet.load()
totalBytes += await withCleanDir("docs/blocks", async write => {
const blockSidebar = {
text: 'Blocks',
items: map(categoryBlocksMap, (blocks, categoryName) => {
return {
text: categoryName,
collapsed: true,
items: []
}
})
cleanDir("docs/blockly")
// TODO: instead, export exactly what we need
copyDir("export", "docs/blockly")
cleanDir("docs/blocks")
// INIT SIDEBAR
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 }) => {
// skip disabled blocks
if(definition.disabled) { return }
// mirror the blocks/**/*.js path structure
const docPath = path.replace(/.js$/, '.md')
write(`docs/blocks/${docPath}`, toBlockMarkdown(definition)) // EXPORT MARKDOWN
// set a default name if missing
if(!definition.name) {
definition.name = capitalize(definition.type.replaceAll("_", " ").replace(/^io /, ""))
// APPEND TO SIDEBAR
const
blockSidebarPath = `/blocks/${docPath.slice(0, -3)}`,
sidebarEntry = {
text: capitalize(definition.name),
link: blockSidebarPath
}
// mirror the blocks/**/*.js path structure
const docPath = path.replace(/.js$/, '.md')
write(docPath, toBlockMarkdown(definition))
const
blockSidebarPath = `/blocks/${docPath.slice(0, -3)}`,
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)
}
})
// definition.categories
// 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
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`)
},
copyDir = (from, to) => {
fs.cpSync(from, to, { recursive: true })
console.log(`/${from}/* copied to /${to}/*`)
},
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)