UNPKG

@kumologica/builder

Version:
54 lines (39 loc) 1.65 kB
const codegen = require('../build/codegen'); const path = require('path'); const fs = require('fs-extra'); function generate(lang, argv) { if (!["html"].includes(lang)) { throw new Error(`Unsupported value for language: <${lang}>, valid values are: html`); } console.log(`Generating ${lang} documentation`); if (lang == 'html') { let projectDir = argv["project-directory"] || process.cwd(); let buildDir = codegen.getBuildDir(projectDir); let projectFlowName = argv["flow-file-name"] || codegen.findFlowFile(projectDir); if (!projectFlowName) { throw new Error(`Unable to find flow file in project directory: ${projectDir}`); } const nodes = codegen.loadJsonFile(path.join(projectDir, projectFlowName)); // generate index.html of docs in target directory const assetsDir = path.join(__dirname, "..", "..", "assets"); const destinationDir = path.join(buildDir, argv["documentation-directory"]); //console.log(`assetsDir: ${assetsDir}`); //console.log(`destinationDir: ${destinationDir}`); const assets = fs.readdirSync(assetsDir).filter(f => !f.endsWith('.html')); assets.forEach(f => { fs.copySync( path.join(assetsDir, f), path.join(path.join(destinationDir, 'assets'), f) ); }); let index = fs.readFileSync(path.join(assetsDir, 'index.html'), 'utf8') .replace('REPLACE_FLOW_HERE', JSON.stringify(nodes)); fs.writeFileSync(path.join(destinationDir, 'index.html'), index); return destinationDir; } else { throw new Error(`lang: ${lang} not supported yet`); } } module.exports = { generate }