1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { program } = require('commander');
  5. program
  6. .version('1.0.0')
  7. .description('Executes text files for internal use.')
  8. .argument('[filePath]', 'Path to the text file to execute')
  9. .action((filePath) => {
  10. try {
  11. // Validate file extension
  12. if (!path.extname(filePath).endsWith('.txt')) {
  13. throw new Error('File must have a .txt extension.');
  14. }
  15. // Read the file content
  16. const fileContent = fs.readFileSync(filePath, 'utf8');
  17. // Execute the file content (using eval - use with caution!)
  18. try {
  19. eval(fileContent);
  20. } catch (error) {
  21. console.error('Error executing file:', error.message);
  22. }
  23. } catch (error) {
  24. console.error('Error:', error.message);
  25. }
  26. });
  27. program.parse(process.argv);

Add your comment