76 lines
2.5 KiB
JavaScript
76 lines
2.5 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
class ConfigManager {
|
|
constructor(relPath = '.') {
|
|
this.configDir = path.resolve(__dirname, relPath);
|
|
}
|
|
|
|
/**
|
|
* Load a configuration file by name
|
|
* @param {string} configName - Name of the config file (without .json extension)
|
|
* @returns {Object} Parsed configuration object
|
|
*/
|
|
getConfig(configName) {
|
|
try {
|
|
const configPath = path.resolve(this.configDir, `${configName}.json`);
|
|
const configData = fs.readFileSync(configPath, 'utf8');
|
|
return JSON.parse(configData);
|
|
} catch (error) {
|
|
throw new Error(`Failed to load config '${configName}': ${error.message}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get list of available configuration files
|
|
* @returns {Array<string>} Array of config names (without .json extension)
|
|
*/
|
|
getAvailableConfigs() {
|
|
try {
|
|
const resolvedDir = path.resolve(this.configDir);
|
|
const files = fs.readdirSync(resolvedDir);
|
|
return files
|
|
.filter(file => file.endsWith('.json'))
|
|
.map(file => path.basename(file, '.json'));
|
|
} catch (error) {
|
|
throw new Error(`Failed to read config directory: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a specific config exists
|
|
* @param {string} configName - Name of the config file
|
|
* @returns {boolean} True if config exists
|
|
*/
|
|
hasConfig(configName) {
|
|
const configPath = path.resolve(this.configDir, `${configName}.json`);
|
|
return fs.existsSync(configPath);
|
|
}
|
|
|
|
createEndpoint(nodeName) {
|
|
try {
|
|
// Load the config for this node
|
|
const config = this.getConfig(nodeName);
|
|
|
|
// Convert config to JSON
|
|
const configJSON = JSON.stringify(config, null, 2);
|
|
|
|
// Assemble the complete script
|
|
return `
|
|
// Create the namespace structure
|
|
window.EVOLV = window.EVOLV || {};
|
|
window.EVOLV.nodes = window.EVOLV.nodes || {};
|
|
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
|
|
|
|
// Inject the pre-loaded config data directly into the namespace
|
|
window.EVOLV.nodes.${nodeName}.config = ${configJSON};
|
|
|
|
console.log('${nodeName} config loaded and endpoint created');
|
|
`;
|
|
} catch (error) {
|
|
throw new Error(`Failed to create endpoint for '${nodeName}': ${error.message}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = ConfigManager; |