43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import process from "node:process";
|
|
import { anonymize1cXml, decodeXmlBuffer } from "../src/utils/anonymize1cXml.js";
|
|
|
|
const printUsage = () => {
|
|
process.stdout.write("Usage: node scripts/anonymize-1c-xml.mjs <input.xml> [output.xml]\n");
|
|
};
|
|
|
|
const buildDefaultOutputPath = (inputPath) => {
|
|
const parsed = path.parse(inputPath);
|
|
return path.join(parsed.dir, `${parsed.name}.anonymized${parsed.ext || ".xml"}`);
|
|
};
|
|
|
|
const main = async () => {
|
|
const [, , inputPathArg, outputPathArg] = process.argv;
|
|
|
|
if (!inputPathArg) {
|
|
printUsage();
|
|
process.exitCode = 1;
|
|
return;
|
|
}
|
|
|
|
const inputPath = path.resolve(process.cwd(), inputPathArg);
|
|
const outputPath = path.resolve(process.cwd(), outputPathArg || buildDefaultOutputPath(inputPath));
|
|
|
|
if (inputPath === outputPath) {
|
|
throw new Error("Output path must be different from input path.");
|
|
}
|
|
|
|
const sourceXml = decodeXmlBuffer(await fs.readFile(inputPath));
|
|
const anonymizedXml = anonymize1cXml(sourceXml);
|
|
|
|
await fs.writeFile(outputPath, anonymizedXml, "utf8");
|
|
|
|
process.stdout.write(`Anonymized XML saved to ${outputPath}\n`);
|
|
};
|
|
|
|
main().catch((error) => {
|
|
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
process.exitCode = 1;
|
|
});
|