add clean option to CLI
This commit is contained in:
@@ -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.`
|
`Use --overwrite flag to replace it or --clean flag to remove its contents.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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');
|
||||||
|
|||||||
Reference in New Issue
Block a user