Files
bus-capacitor-calculator/scripts/build.cjs
T
gaochengyu 73ab36f7ff feat: 开源母线电容计算器并接入独立自动部署
整理 Buck 与 PMSM 完整源码、离线构建、校核测试和使用说明,采用 MIT 许可。
增加页首开源链接,支持推送 main 后自动测试、构建并发布到原计算器网址。
2026-11-27 04:38:28 +08:00

81 lines
4.2 KiB
JavaScript

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 = '<style>body>header{padding-block:14px}body>header h1{font-size:clamp(19px,3vw,26px);margin:3px 0 4px}body>header p{font-size:13px}</style>';
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(/(<html[^>]*>)/i, '$1\n<!--\n' + notice.replace(/--/g, '- -') + '\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('</script')) throw Error('Unsafe inline script: ' + file);
page = replaceOnce(page, '/*__' + key + '__*/', code);
}
if (withKatex) {
let css = read(path.join(vendor, 'katex.min.css'));
const expected = (css.match(/@font-face/g) || []).length;
let count = 0;
css = css.replace(/src:\s*url\(["']?(fonts\/[^)"']+\.woff2)["']?\)[^;}]*;?/g, (_m, file) => {
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, '<!--__HARNESS_PANEL__-->', read(path.join(source, 'buck-harness-panel.html')));
page = replaceOnce(page, '<!--__VISUAL_PANEL__-->', read(path.join(source, 'buck-visual-panel.html')));
}
if (/\/\*__[A-Z_]+__\*\/|<!--__[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('</head>', compact + '</head>');
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));