#!/usr/bin/env node
const { program } = require('commander');
const { exec } = require('child_process');
const CLI_ARGUMENTS = {
'command1': {
command: 'my-command1',
retryInterval: 2000, // 2 seconds
retries: 3,
},
'command2': {
command: 'my-command2',
retryInterval: 1000, // 1 second
retries: 5,
},
// Add more commands here
};
program.on('command', (command) => {
const commandDetails = CLI_ARGUMENTS[command];
if (!commandDetails) {
console.error(`Command "${command}" not found.`);
process.exit(1);
}
let retryCount = 0;
const executeCommand = () => {
exec(commandDetails.command, (error, stdout, stderr) => {
if (error) {
retryCount++;
if (retryCount <= commandDetails.retries) {
console.error(`Command "${commandDetails.command}" failed. Retrying (${retryCount}/${commandDetails.retries})...`);
setTimeout(executeCommand, commandDetails.retryInterval);
} else {
console.error(`Command "${commandDetails.command}" failed after ${commandDetails.retries} retries.`);
process.exit(1);
}
} else if (stderr) {
console.error(`Command "${commandDetails.command}" stderr: ${stderr}`);
retryCount++;
if (retryCount <= commandDetails.retries) {
console.error(`Command "${commandDetails.command}" failed. Retrying (${retryCount}/${commandDetails.retries})...`);
setTimeout(executeCommand, commandDetails.retryInterval);
} else {
console.error(`Command "${commandDetails.command}" failed after ${commandDetails.retries} retries.`);
process.exit(1);
}
} else {
console.log(stdout);
}
});
};
executeCommand();
});
program.parse(process.argv);
Add your comment