add clean option to CLI

This commit is contained in:
2026-05-12 16:05:25 +02:00
parent 1ee3b70910
commit b509637bbb
+18 -13
View File
@@ -7,7 +7,7 @@
* for better version control and diff visibility. * for better version control and diff visibility.
* *
* Usage: * Usage:
* node extract-elx.js <input-file-path> [--overwrite] * node extract-elx.js <input-file-path> [--overwrite] [--clean]
* *
* Output: * Output:
* Creates a folder named after the input file (without extension) containing: * Creates a folder named after the input file (without extension) containing:
@@ -344,16 +344,20 @@ async function readInputFile(filePath) {
/** /**
* Create output directory for extracted properties * Create output directory for extracted properties
*/ */
async function createOutputDirectory(outputPath, allowOverwrite = false) { async function createOutputDirectory(outputPath, allowOverwrite = false, cleanExisting = false) {
if (existsSync(outputPath)) { if (!allowOverwrite && !cleanExisting && existsSync(outputPath)) {
if (!allowOverwrite) { throw new Error(
throw new Error( `Output directory already exists: ${outputPath}\n` +
`Output directory already exists: ${outputPath}\n` + `Use --overwrite flag to replace it or --clean flag to remove its contents.`
`Use --overwrite flag to replace it.` );
); }
}
// Optionally remove existing directory // Optionally remove existing directory
console.log(`Removing existing directory: ${outputPath}`); if (cleanExisting) {
console.log(`Removing existing directory contents: ${outputPath}`);
await fs.rm(`${outputPath}`, { recursive: true, force: true });
} else if (allowOverwrite) {
console.log(`Overwriting existing directory: ${outputPath}`);
} }
try { try {
@@ -612,13 +616,14 @@ async function main() {
const args = process.argv.slice(2); const args = process.argv.slice(2);
if (args.length === 0) { if (args.length === 0) {
console.error('Usage: node extract-elx.js <input-file-path> [--overwrite]'); console.error('Usage: node extract-elx.js <input-file-path> [--overwrite] [--clean]');
console.error('Example: node extract-elx.js elxforms_4951.elx'); console.error('Example: node extract-elx.js elxforms_4951.elx');
process.exit(1); process.exit(1);
} }
const inputPath = args[0]; const inputPath = args[0];
const allowOverwrite = args.includes('--overwrite'); const allowOverwrite = args.includes('--overwrite');
const cleanExisting = args.includes('--clean');
// Validate input file // Validate input file
if (!existsSync(inputPath)) { if (!existsSync(inputPath)) {
@@ -640,7 +645,7 @@ async function main() {
// Create output directory // Create output directory
console.log(`\n📂 Creating output: ${outputPath}`); console.log(`\n📂 Creating output: ${outputPath}`);
await createOutputDirectory(outputPath, allowOverwrite); await createOutputDirectory(outputPath, allowOverwrite, cleanExisting);
// Process each root property // Process each root property
console.log('\n⚙️ Processing properties:\n'); console.log('\n⚙️ Processing properties:\n');