UNPKG

@tricoteuses/senat

Version:

Handle French Sénat's open data

63 lines (62 loc) 2.17 kB
import fs from "fs-extra"; import path from "path"; import commandLineArgs from "command-line-args"; import { convertSenatXmlToHtml } from "../conversion_textes"; const optionDefinitions = [ { name: "input", alias: "i", type: String, defaultOption: true }, { name: "output", alias: "o", type: String }, { name: "help", alias: "h", type: Boolean }, ]; async function main() { let options; try { options = commandLineArgs(optionDefinitions, { stopAtFirstUnknown: true }); } catch (err) { console.error(`Error: ${err.message}`); process.exit(1); } // Handle positional arguments if not using flags const argv = options["_unknown"] || []; if (!options["output"] && argv.length > 0) { options["output"] = argv[0]; } if (options["help"] || !options["input"]) { console.log("Usage: npx tsx src/scripts/convert_xml_to_html.ts <input_xml_path> [output_html_path]"); console.log("Options:"); console.log(" -i, --input <path> Input XML file path (default option)"); console.log(" -o, --output <path> Output HTML file path"); console.log(" -h, --help Show this help message"); return; } const inputPath = path.resolve(options["input"]); let outputPath = options["output"]; if (!outputPath) { outputPath = inputPath.replace(/\.xml$/, ".html"); if (outputPath === inputPath) { outputPath += ".html"; } } else { outputPath = path.resolve(outputPath); } if (!fs.existsSync(inputPath)) { console.error(`Error: Input file not found: ${inputPath}`); process.exit(1); } try { const xmlContent = await fs.readFile(inputPath, "utf-8"); await convertSenatXmlToHtml(xmlContent, outputPath); console.log("Successfully converted:"); console.log(` From: ${inputPath}`); console.log(` To: ${outputPath}`); } catch (error) { console.error(`Error during conversion: ${error.message}`); process.exit(1); } } main().catch((error) => { console.error(error); process.exit(1); });