1. #!/usr/bin/env node
  2. const { program } = require('commander');
  3. const { spawn } = require('child_process');
  4. const tasks = [];
  5. program
  6. .version('1.0.0')
  7. .description('CLI for diagnostics tasks');
  8. program
  9. .argument('<task>', 'Task to execute')
  10. .parse(process.argv);
  11. function runTask(task) {
  12. // Basic sanity check: check if the task exists
  13. if (!fs.existsSync(task)) {
  14. console.error(`Error: Task '${task}' does not exist.`);
  15. return;
  16. }
  17. tasks.push({ task, startTime: Date.now() });
  18. console.log(`Queued task: ${task}`);
  19. // Execute the task
  20. const process = spawn(task);
  21. process.stdout.on('data', (data) => {
  22. console.log(`Task ${task} stdout: ${data}`);
  23. });
  24. process.stderr.on('data', (data) => {
  25. console.error(`Task ${task} stderr: ${data}`);
  26. });
  27. process.on('close', (code) => {
  28. const endTime = Date.now();
  29. const duration = (endTime - tasks[0].startTime) / 1000;
  30. console.log(`Task ${task} completed with code ${code} in ${duration}s`);
  31. });
  32. process.on('error', (err) => {
  33. console.error(`Task ${task} failed: ${err}`);
  34. });
  35. }
  36. // Process the queued tasks
  37. if (tasks.length > 0) {
  38. tasks.forEach(taskData => {
  39. runTask(taskData.task);
  40. });
  41. }

Add your comment