1. const fs = require('fs');
  2. const path = require('path');
  3. const glob = require('glob');
  4. /**
  5. * Boots scripts from files defined in a configuration file.
  6. * @param {string} configFilePath Path to the configuration file (JSON).
  7. */
  8. async function bootstrapScripts(configFilePath) {
  9. try {
  10. const config = JSON.parse(fs.readFileSync(configFilePath, 'utf8'));
  11. if (!config || !config.scripts) {
  12. console.warn('Configuration file is invalid or missing "scripts" section.');
  13. return;
  14. }
  15. const scriptFiles = config.scripts.map(script => script.path); // Extract file paths
  16. for (const scriptFile of scriptFiles) {
  17. try {
  18. // Execute each script file
  19. const scriptPath = path.resolve(scriptFile);
  20. if (fs.existsSync(scriptPath)) {
  21. require(scriptPath); // dynamically require and execute
  22. console.log(`Executed script: ${scriptPath}`);
  23. } else {
  24. console.warn(`Script file not found: ${scriptFile}`);
  25. }
  26. } catch (error) {
  27. console.error(`Error executing script ${scriptFile}:`, error);
  28. }
  29. }
  30. } catch (error) {
  31. console.error('Error reading or parsing configuration file:', error);
  32. }
  33. }
  34. // Example usage:
  35. // bootstrapScripts('config.json');
  36. module.exports = bootstrapScripts;

Add your comment