|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'fs' |
| 4 | +import path from 'path' |
| 5 | +import { execSync } from 'child_process' |
| 6 | +import fpt from '../../lib/non-enterprise-default-version.js' |
| 7 | +import { allVersionKeys } from '../../lib/all-versions.js' |
| 8 | +import { liquid } from '../../lib/render-content/index.js' |
| 9 | +import contextualize from '../../middleware/context.js' |
| 10 | + |
| 11 | +const layoutFilename = path.posix.join(process.cwd(), 'script/dev-toc/layout.html') |
| 12 | +const layout = fs.readFileSync(layoutFilename, 'utf8') |
| 13 | + |
| 14 | +const staticDirName = 'script/dev-toc/static' |
| 15 | +const staticDir = path.posix.join(process.cwd(), staticDirName) |
| 16 | +if (!fs.existsSync(staticDir)) fs.mkdirSync(staticDir) |
| 17 | + |
| 18 | +main() |
| 19 | + |
| 20 | +async function main() { |
| 21 | + const next = () => {} |
| 22 | + const res = {} |
| 23 | + const req = { language: 'en', cookies: {} } |
| 24 | + |
| 25 | + for (const version of allVersionKeys) { |
| 26 | + req.pagePath = version === fpt ? '/' : `/${version}` |
| 27 | + |
| 28 | + // Create a subdir for the version if one doesn't exist yet. |
| 29 | + const versionStaticDir = path.posix.join(staticDir, version) |
| 30 | + if (!fs.existsSync(versionStaticDir)) fs.mkdirSync(versionStaticDir) |
| 31 | + |
| 32 | + // Create a versioned filename. |
| 33 | + const filename = path.posix.join(versionStaticDir, 'dev-toc.html') |
| 34 | + |
| 35 | + // Create a minimal context object. |
| 36 | + await contextualize(req, res, next) |
| 37 | + |
| 38 | + // Add the tree to the req.context. |
| 39 | + req.context.currentEnglishTree = req.context.siteTree.en[req.context.currentVersion] |
| 40 | + |
| 41 | + // Parse the layout in script/dev-toc/layout.html with the context we created above. |
| 42 | + const outputHtml = await liquid.parseAndRender(layout, Object.assign({}, req.context)) |
| 43 | + |
| 44 | + // Write a static file for each version. |
| 45 | + fs.writeFileSync(filename, outputHtml) |
| 46 | + } |
| 47 | + |
| 48 | + // Default to FPT for the file to open. |
| 49 | + const fptFile = path.posix.join(staticDirName, fpt, 'dev-toc.html') |
| 50 | + |
| 51 | + execSync(`open ${fptFile}`) |
| 52 | + |
| 53 | + console.log(`\nCreated the TOC! If it doesn't open automatically, open the following file in your browser to view it:\n |
| 54 | +${fptFile}`) |
| 55 | +} |
0 commit comments