/* M02 「전국」 자재 줄(361-2) 헬퍼 — `Labor_National.nationalRows` 를 트랜스파일해 Node 로 돌림 · * `el` 대역 = 나무 꼴 {tag, className, text, attrs, children} · stdin = {origin} · stdout = 나무 JSON. */ const fs = require("fs"); const path = require("path"); const { makeLoader } = require("./helper_ts_stub_loader.cjs"); const ROOT = path.join(__dirname, "..", ".."); const el = (tag, o = {}) => ({ tag, ...o, children: o.children ?? [] }); const STUBS = { "@ui/ui_template_elements": { el, createSelectField: () => ({ root: el("select") }) }, "@ui/ui_template_locale": { currentLanguageIndex: 0 }, "@config/config_frontend": { API_BASE_URL: "/api" }, }; const { loadTs } = makeLoader({ root: ROOT, requireFor: (full) => (request) => { if (STUBS[request]) return STUBS[request]; if (request.startsWith(".") && !request.endsWith(".css")) return loadTs(`${path.resolve(path.dirname(full), request)}.ts`); return new Proxy({}, { get: () => () => ({}) }); }, }); const input = JSON.parse(fs.readFileSync(0, "utf8")); const national = loadTs( path.join(ROOT, "M02_MasterTemplete", "M02_MasterTemplete_Labor_National.ts"), ); const text = (n) => (n.text ?? "") + (n.children ?? []).map(text).join(" "); const find = (n, tag) => [ ...(n.tag === tag ? [n] : []), ...(n.children ?? []).flatMap((c) => find(c, tag)), ]; const out = input.origins.map((origin) => { const got = national.nationalRows(origin); const table = got.flatMap((n) => find(n, "table"))[0]; return { count: got.length, title: got[0]?.text ?? null, heads: table ? find(table, "th").map((h) => h.text) : [], rows: table ? find(table, "tr") .slice(1) .map((r) => r.children.map((c) => text(c).trim())) : [], buttons: got.flatMap((n) => [...find(n, "button"), ...find(n, "select")]).length, }; }); process.stdout.write(JSON.stringify(out));