import { Command } from 'commander' import kleur from 'kleur' import { isForm, type Form, type FormInstance, type FormExtraction } from '@asterdoc/core' import { LocalFileSystem } from '../../utils/local-fs.js' import { readTextInput, resolveArtifactTarget } from '../../utils/io.js' import { loadFormFile } from '../../utils/load-form.js' import { loadValidatedArtifact } from '../../utils/artifact-file.js' interface ExtractOptions { layer?: string out?: string } type FileResult = | ({ file: string } & FormExtraction) | { file: string; error: { code: string; message: string } } function errorOf(error: unknown): { code: string; message: string } { const message = error instanceof Error ? error.message : String(error) const code = error && typeof error === 'object' && 'code' in error && typeof error.code === 'string' ? error.code : 'extract_error' return { code, message } } async function loadForm(target: string): Promise> { const { raw, baseDir } = await readTextInput(await resolveArtifactTarget(target)) const { artifact } = loadValidatedArtifact(raw) if (!isForm(artifact)) { const kind = artifact.kind throw new Error(`Expected form artifact, but received kind "${kind}".`) } return loadFormFile(artifact as Form, baseDir) } /** * Create the 'extract' command: read filled PDF forms back into form data. */ export function createExtractCommand(): Command { const extract = new Command('extract') extract .argument('', 'Form artifact file (JSON/YAML)') .argument('', 'Filled PDF file, or a directory of filled PDFs') .description('Extract form data from filled PDF forms (AcroForm fields) as JSON') .option('--layer ', 'PDF layer to read against (required when the form has several)') .option('--out ', 'Write the JSON result to a file instead of stdout') .action(async (formTarget: string, pdfTarget: string, options: ExtractOptions) => { try { if (!formTarget || formTarget === '-') throw new Error('Form file path required.') const storage = new LocalFileSystem() const instance = await loadForm(formTarget) const pdfPath = storage.getAbsolutePath(pdfTarget) if (!(await storage.exists(pdfPath))) throw new Error(`File not found: ${pdfTarget}`) const isDirectory = (await storage.stat(pdfPath)).isDirectory const extractFile = async (file: string, label: string): Promise => { try { const pdf = new Uint8Array(await storage.readFile(file, 'binary')) return { file: label, ...(await instance.extract(pdf, { layer: options.layer })) } } catch (error) { return { file: label, error: errorOf(error) } } } let output: unknown let failed: boolean if (isDirectory) { const names = (await storage.listFiles(pdfPath)).filter((name) => name.toLowerCase().endsWith('.pdf')).sort() if (names.length === 0) throw new Error(`No PDF files found in ${pdfTarget}`) const results: FileResult[] = [] for (const name of names) results.push(await extractFile(storage.joinPath(pdfPath, name), name)) output = { results } failed = results.some((result) => 'error' in result) } else { const result = await extractFile(pdfPath, pdfTarget) if ('error' in result) throw Object.assign(new Error(result.error.message), { code: result.error.code }) output = result failed = false } const json = `${JSON.stringify(output, null, 2)}\n` if (options.out) { await storage.writeFile(options.out, json) console.error(kleur.green(`✓ Extraction written to ${options.out}`)) } else { process.stdout.write(json) } process.exitCode = failed ? 1 : 0 } catch (error) { const { code, message } = errorOf(error) console.error(kleur.red(`Error${code === 'extract_error' ? '' : ` (${code})`}: ${message}`)) process.exitCode = 1 } }) return extract }