|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { parseArgs as nodeParseArgs } from 'node:util'; |
| 3 | +import { resolve } from 'node:path'; |
| 4 | +import { writeFile } from 'node:fs/promises'; |
| 5 | +import { extractWord } from './word.ts'; |
| 6 | +import { extractSuperDoc } from './superdoc.ts'; |
| 7 | +import { normalizeSuperDoc, normalizeWord } from './normalize.ts'; |
| 8 | +import { diffParagraphs } from './differ.ts'; |
| 9 | +import { formatJson, formatMarkdown } from './format.ts'; |
| 10 | +import type { CompareReport, Finding } from './types.ts'; |
| 11 | + |
| 12 | +type Args = { |
| 13 | + input: string; |
| 14 | + output?: string; |
| 15 | + format: 'json' | 'md'; |
| 16 | + pipeline: 'presentation' | 'headless'; |
| 17 | + cache: boolean; |
| 18 | +}; |
| 19 | + |
| 20 | +const USAGE = `compare-rendering — diff Word vs SuperDoc rendering (paragraph-only scope) |
| 21 | +
|
| 22 | +Usage: |
| 23 | + pnpm compare-rendering -- --input <docx> [options] |
| 24 | +
|
| 25 | +Options: |
| 26 | + --input <path> Required. Path to a .docx file. |
| 27 | + --output <path> Write the report to a file (default: stdout). |
| 28 | + --format json|md Output format (default: json). |
| 29 | + --pipeline presentation|headless SuperDoc layout pipeline (default: presentation). |
| 30 | + --no-cache Bypass the Word extraction cache. |
| 31 | + -h, --help Show this help. |
| 32 | +
|
| 33 | +Env: |
| 34 | + WORD_MCP_URL HTTP endpoint of the word-mcp worker. |
| 35 | + WORD_MCP_TOKEN Bearer token for the worker. |
| 36 | +
|
| 37 | +Exit codes: |
| 38 | + 0 — ran; findings are at most visible/cosmetic. |
| 39 | + 1 — tool error (network, missing file, bad args). |
| 40 | + 2 — ran; emitted at least one blocking finding.`; |
| 41 | + |
| 42 | +function parseArgs(argv: string[]): Args { |
| 43 | + const { values } = nodeParseArgs({ |
| 44 | + args: argv, |
| 45 | + options: { |
| 46 | + input: { type: 'string' }, |
| 47 | + output: { type: 'string' }, |
| 48 | + format: { type: 'string', default: 'json' }, |
| 49 | + pipeline: { type: 'string', default: 'presentation' }, |
| 50 | + 'no-cache': { type: 'boolean', default: false }, |
| 51 | + help: { type: 'boolean', short: 'h', default: false }, |
| 52 | + }, |
| 53 | + strict: true, |
| 54 | + allowPositionals: false, |
| 55 | + }); |
| 56 | + |
| 57 | + if (values.help) { |
| 58 | + console.log(USAGE); |
| 59 | + process.exit(0); |
| 60 | + } |
| 61 | + |
| 62 | + if (!values.input) throw new Error('--input <docx> is required'); |
| 63 | + if (values.format !== 'json' && values.format !== 'md') { |
| 64 | + throw new Error(`--format must be json or md, got "${values.format}"`); |
| 65 | + } |
| 66 | + if (values.pipeline !== 'presentation' && values.pipeline !== 'headless') { |
| 67 | + throw new Error(`--pipeline must be presentation or headless, got "${values.pipeline}"`); |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + input: values.input, |
| 72 | + output: values.output, |
| 73 | + format: values.format, |
| 74 | + pipeline: values.pipeline, |
| 75 | + cache: !values['no-cache'], |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +function hasBlocking(findings: Finding[]): boolean { |
| 80 | + return findings.some((f) => f.severity === 'blocking'); |
| 81 | +} |
| 82 | + |
| 83 | +const log = (msg: string) => console.error(`[compare-rendering] ${msg}`); |
| 84 | + |
| 85 | +async function main(): Promise<void> { |
| 86 | + const args = parseArgs(process.argv.slice(2)); |
| 87 | + const docxPath = resolve(args.input); |
| 88 | + |
| 89 | + log(`word: extracting ${docxPath}`); |
| 90 | + const wordStart = Date.now(); |
| 91 | + const { extraction: wordExtraction, sha, cached } = await extractWord(docxPath, { cache: args.cache }); |
| 92 | + log(`word: ${cached ? 'cached' : 'fresh'} extraction in ${Date.now() - wordStart}ms (sha=${sha.slice(0, 12)})`); |
| 93 | + |
| 94 | + if (!wordExtraction.supported) { |
| 95 | + const report: CompareReport = { |
| 96 | + docxPath, |
| 97 | + docxSha: sha, |
| 98 | + wordSupported: false, |
| 99 | + unsupportedReason: wordExtraction.unsupportedReason, |
| 100 | + counts: { |
| 101 | + wordParagraphs: 0, |
| 102 | + superdocParagraphs: 0, |
| 103 | + wordPages: wordExtraction.pageCount, |
| 104 | + superdocPages: 0, |
| 105 | + }, |
| 106 | + findings: [ |
| 107 | + { |
| 108 | + category: 'unsupported', |
| 109 | + severity: 'cosmetic', |
| 110 | + paragraphOrdinal: 0, |
| 111 | + word: wordExtraction.unsupportedReason, |
| 112 | + superdoc: null, |
| 113 | + message: `Document skipped: ${wordExtraction.unsupportedReason ?? 'unsupported'}`, |
| 114 | + }, |
| 115 | + ], |
| 116 | + }; |
| 117 | + await emit(report, args); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + log('superdoc: running layout:export-one'); |
| 122 | + const sdStart = Date.now(); |
| 123 | + const sdExtraction = await extractSuperDoc(docxPath, { pipeline: args.pipeline }); |
| 124 | + log(`superdoc: extracted in ${Date.now() - sdStart}ms`); |
| 125 | + |
| 126 | + const wordParas = normalizeWord(wordExtraction); |
| 127 | + const sdParas = normalizeSuperDoc(sdExtraction); |
| 128 | + |
| 129 | + const findings = diffParagraphs(wordParas, sdParas); |
| 130 | + |
| 131 | + const report: CompareReport = { |
| 132 | + docxPath, |
| 133 | + docxSha: sha, |
| 134 | + wordSupported: true, |
| 135 | + counts: { |
| 136 | + wordParagraphs: wordParas.length, |
| 137 | + superdocParagraphs: sdParas.length, |
| 138 | + wordPages: wordExtraction.pageCount, |
| 139 | + superdocPages: sdExtraction.pageCount, |
| 140 | + }, |
| 141 | + findings, |
| 142 | + }; |
| 143 | + |
| 144 | + await emit(report, args); |
| 145 | + if (hasBlocking(findings)) process.exitCode = 2; |
| 146 | +} |
| 147 | + |
| 148 | +async function emit(report: CompareReport, args: Args): Promise<void> { |
| 149 | + const out = args.format === 'md' ? formatMarkdown(report) : formatJson(report); |
| 150 | + if (args.output) { |
| 151 | + await writeFile(resolve(args.output), out, 'utf8'); |
| 152 | + log(`wrote ${resolve(args.output)}`); |
| 153 | + } else { |
| 154 | + process.stdout.write(out); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +main().catch((e) => { |
| 159 | + console.error(`[compare-rendering] error: ${(e as Error).message}`); |
| 160 | + process.exit(1); |
| 161 | +}); |
0 commit comments