/* 855-C 2-C4 거울 헬퍼 — 산출근거 글 규칙(TS)을 Node 로 돌림(`master_doc_text.py` 짝). * stdin = {수: [값], 단위: [글], 금액줄: [{줄, 단가변수, 부름}], 꺼짐: [{문서, 값}], 원: [수]} * stdout = {eq: [eqText], one: [one], won: [won], 단위: [unitText], 금액줄: [moneyUnits], * 꺼짐: [[offLine …] …], 비목: COST_SHOW, 항: LONG_SUM} * `one` · `won` · `COST_SHOW` · `LONG_SUM` = `line_stack_out.ts` 안 정의를 그대로 읽어 돌림(내보내지 않는 값). */ const fs = require("fs"); const path = require("path"); const ts = require(path.join(__dirname, "..", "..", "config", "node_modules", "typescript")); const cache = new Map(); function load(at) { if (cache.has(at)) return cache.get(at); const js = ts.transpileModule(fs.readFileSync(at, "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, }).outputText; const box = { exports: {} }; cache.set(at, box.exports); const need = (p) => { if (p.endsWith(".css")) return {}; return p.startsWith(".") ? load(path.resolve(path.dirname(at), `${p}.ts`)) : require(p); }; new Function("exports", "module", "require", js)(box.exports, box, need); return box.exports; } const ROOT = path.join(__dirname, "..", ".."); const eq = load(path.join(ROOT, "ui_template", "ui_template_equation.ts")); const unit = load(path.join(ROOT, "common_util", "common_util_line_stack_unit.ts")); const view = load(path.join(ROOT, "common_util", "common_util_formula_doc_view.ts")); const stack = load(path.join(ROOT, "common_util", "common_util_line_stack.ts")); // line_stack_out.ts 안 정의(내보내지 않음) — 그 줄 글을 그대로 떼어 돌림 const out = fs.readFileSync(path.join(ROOT, "A00_Common", "line_stack_out.ts"), "utf8"); const grab = (re) => { const m = out.match(re); if (!m) throw new Error(`line_stack_out.ts 에서 못 찾음: ${re}`); return m[0]; }; const defs = [ grab(/const COST_SHOW: Record = \{[^}]*\};/), grab(/const won = \(n: number\): string => [^;]*;/), grab(/const one = \(n: number\): string =>\s*n\.toLocaleString\([^;]*;/), grab(/const LONG_SUM = \d+;/), ].join("\n"); const js = ts.transpileModule(`${defs}\nreturn { COST_SHOW, won, one, LONG_SUM };`, { compilerOptions: { target: ts.ScriptTarget.ES2020 }, }).outputText; const local = new Function("eqText", js)(eq.eqText); const input = JSON.parse(fs.readFileSync(0, "utf8")); const res = { eq: (input.수 || []).map((v) => eq.eqText(v)), one: (input.원 || []).map((v) => local.one(v)), won: (input.원 || []).map((v) => local.won(v)), 단위: (input.단위 || []).map((u) => unit.unitText(u)), 금액줄: (input.금액줄 || []).map((x) => unit.moneyUnits( x.줄, view.tryParse(x.줄.단가), view.tryParse(x.줄.수량), x.단가변수 || undefined, x.부름 || null, ), ), 기호: (input.금액줄 || []).map((x) => stack.priceSym(x.줄)), 꺼짐: (input.꺼짐 || []).map((x) => (x.문서.금액 || []).map((line) => view.offLine(line, x.값, view.lineMap(x.문서))), ), 비목: local.COST_SHOW, 항: local.LONG_SUM, }; process.stdout.write(JSON.stringify(res));