1. const fs = require('fs');
  2. const path = require('path');
  3. /**
  4. * Boots up scripts from entries defined in a configuration file.
  5. * @param {string} configFilePath Path to the configuration file (JSON).
  6. * @param {object} options Optional configuration options.
  7. */
  8. async function bootstrapScripts(configFilePath, options = {}) {
  9. try {
  10. // Read the configuration file
  11. const configFile = JSON.parse(fs.readFileSync(configFilePath, 'utf-8'));
  12. // Validate configuration (basic check)
  13. if (!configFile.entries || !Array.isArray(configFile.entries)) {
  14. throw new Error('Invalid configuration: Missing or invalid "entries" array.');
  15. }
  16. // Process each entry
  17. for (const entry of configFile.entries) {
  18. if (!entry.script || typeof entry.script !== 'string') {
  19. console.warn(`Skipping entry due to missing or invalid script: ${JSON.stringify(entry)}`);
  20. continue;
  21. }
  22. const scriptPath = path.resolve(entry.script); // Resolve to absolute path
  23. try {
  24. // Execute the script
  25. console.log(`Bootstrapping script: ${scriptPath}`);
  26. await require(scriptPath)(); // Execute the script
  27. console.log(`Script ${scriptPath} completed successfully.`);
  28. } catch (error) {
  29. console.error(`Error executing script ${scriptPath}:`, error);
  30. }
  31. }
  32. } catch (error) {
  33. console.error('Error loading or processing configuration:', error);
  34. }
  35. }
  36. module.exports = bootstrapScripts;

Add your comment