const fs = require('node:fs'); const path = require('node:path'); const root = path.resolve(__dirname, '..'); const source = path.join(root, 'src'); const vendor = path.join(root, 'vendor', 'katex'); const outFlag = process.argv.indexOf('--out-dir'); const output = outFlag >= 0 ? path.resolve(process.argv[outFlag + 1]) : path.join(root, 'dist'); const read = (p) => fs.readFileSync(p, 'utf8').replace(/\r\n/g, '\n'); const compact = ''; const buck = { ENGINE: 'buck-calculator-engine.js', VALIDATION_DATA: 'calculator-validation-data.js', VALIDATION_UI: 'calculator-validation.js', ACCURACY_ENGINE: 'buck-accuracy-engine.js', CONTROLS: 'calculator-controls.js', CHART: 'motor-bus-chart.js', HARNESS_ENGINE: 'buck-harness-engine.js', WIRE_AMPACITY: 'buck-wire-ampacity.js', HARNESS_UI: 'buck-harness-ui.js', SENSITIVITY_ENGINE: 'buck-sensitivity-engine.js', SENSITIVITY_UI: 'buck-sensitivity-ui.js', VISUAL_ENGINE: 'buck-visual-engine.js', VISUAL_UI: 'buck-visual-ui.js' }; const motor = { CORE: 'buck-calculator-engine.js', CONTROLS: 'calculator-controls.js', HARNESS: 'buck-harness-engine.js', AMPACITY: 'buck-wire-ampacity.js', SUPPLY: 'motor-bus-supply.js', MOTOR: 'motor-bus-engine.js', RECOMMEND: 'motor-bus-recommend.js', WORKER: 'motor-bus-worker.js', CHART: 'motor-bus-chart.js', UI: 'motor-bus-ui.js' }; function replaceOnce(page, marker, value) { if (page.split(marker).length !== 2) throw Error('Missing or repeated marker: ' + marker); return page.replace(marker, () => value); } function license(page, withKatex) { const notice = read(path.join(root, 'LICENSE')) + (withKatex ? '\nKaTeX license:\n' + read(path.join(vendor, 'LICENSE')) : ''); return page.replace(/(]*>)/i, '$1\n'); } function render(template, inputs, withKatex) { let page = read(path.join(source, template)); for (const [key, file] of Object.entries(inputs)) { const code = read(path.join(source, file)); if (code.toLowerCase().includes(' { const resolved = path.resolve(vendor, file); if (!resolved.startsWith(vendor + path.sep)) throw Error('Invalid font path'); count++; return 'src:url(data:font/woff2;base64,' + fs.readFileSync(resolved).toString('base64') + ') format("woff2");'; }); if (count !== expected || /url\(["']?fonts\//.test(css)) throw Error('Fonts were not fully embedded'); page = replaceOnce(page, '/*__KATEX_CSS__*/', css); page = replaceOnce(page, '/*__KATEX_JS__*/', read(path.join(vendor, 'katex.min.js'))); page = replaceOnce(page, '', read(path.join(source, 'buck-harness-panel.html'))); page = replaceOnce(page, '', read(path.join(source, 'buck-visual-panel.html'))); } if (/\/\*__[A-Z_]+__\*\/|/.test(page)) throw Error('Unresolved template marker'); return license(page, withKatex); } fs.mkdirSync(output, { recursive: true }); const buckPage = render('buck-calculator-page.html', buck, true); const motorPage = render('motor-impedance-page.html', motor, false); let hub = read(path.join(source, 'calculator-hub.html')); for (const [key, page] of [['BUCK', buckPage], ['MOTOR', motorPage]]) { const embedded = page.replace('', compact + ''); hub = replaceOnce(hub, '__' + key + '_PAYLOAD__', Buffer.from(embedded).toString('base64')); } hub = license(hub, true); for (const [file, content] of [ ['impedance-calculators.html', hub], ['buck-rlc-calculator.html', buckPage], ['motor-impedance-calculator.html', motorPage] ]) { fs.writeFileSync(path.join(output, file), content); console.log('Built ' + file + ' (' + Buffer.byteLength(content) + ' bytes)'); } for (const file of ['LICENSE', 'THIRD_PARTY_NOTICES.md']) fs.copyFileSync(path.join(root, file), path.join(output, file));