@interopio/desktop-cli
Version:
io.Connect Desktop Seed Repository CLI Tools
170 lines • 7.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultComponentRegistry = exports.ComponentRegistryImpl = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const utils_1 = require("../utils");
/**
* Implementation of ComponentRegistry
*/
class ComponentRegistryImpl {
constructor() {
this.components = new Map();
// Discover and register all available components
const availableComponents = this.discoverAvailableComponents();
for (const componentName of availableComponents) {
this.register(componentName);
}
}
register(componentName) {
if (this.components.has(componentName)) {
utils_1.Logger.debug(`Component already registered: ${componentName}`);
return;
}
const processor = this.createProcessorByName(componentName);
if (!processor) {
utils_1.Logger.error(`Failed to create processor for component: ${componentName}`);
return;
}
this.components.set(componentName, processor);
utils_1.Logger.debug(`Registered component processor: ${processor.config.displayName} (${componentName})`);
}
get(componentName) {
return this.components.get(componentName);
}
getAll() {
return Array.from(this.components.values());
}
getComponentNames() {
return Array.from(this.components.keys());
}
has(componentName) {
return this.components.has(componentName);
}
unregister(componentName) {
const existed = this.components.has(componentName);
this.components.delete(componentName);
if (existed) {
utils_1.Logger.debug(`Unregistered component processor: ${componentName}`);
}
return existed;
}
/**
* Get all components that have modifications available
*/
async getComponentsWithModifications() {
const result = [];
for (const processor of this.components.values()) {
if (await processor.hasModifications()) {
result.push(processor);
}
}
return result;
}
/**
* Apply modifications for all registered components
*/
async applyAllModifications() {
const componentsWithMods = await this.getComponentsWithModifications();
if (componentsWithMods.length === 0) {
utils_1.Logger.info('No components with modifications found');
return;
}
utils_1.Logger.info(`Applying modifications for ${componentsWithMods.length} component(s)...`);
for (const processor of componentsWithMods) {
await processor.applyModifications();
}
}
/**
* Validate modifications for all registered components
*/
async validateAllModifications() {
let allValid = true;
for (const processor of this.components.values()) {
const isValid = await processor.validateModifications();
if (!isValid) {
allValid = false;
utils_1.Logger.error(`Validation failed for component: ${processor.config.displayName}`);
}
}
return allValid;
}
createProcessorByName(name) {
switch (name) {
case 'iocd':
return new utils_1.IocdComponentProcessor();
default:
utils_1.Logger.warning(`No processor implementation found for component: ${name}`);
return undefined;
}
}
/**
* Discover available components by scanning the components directory
* and looking for component processor implementations
*/
discoverAvailableComponents() {
try {
const componentsDir = path_1.default.join(__dirname);
if (!fs_extra_1.default.existsSync(componentsDir)) {
utils_1.Logger.debug('Components directory not found, using fallback list');
return ['iocd'];
}
const entries = fs_extra_1.default.readdirSync(componentsDir, { withFileTypes: true });
const componentNames = [];
for (const entry of entries) {
if (entry.isDirectory()) {
const componentName = entry.name;
// Skip special directories
if (componentName === 'base' || componentName.startsWith('.')) {
continue;
}
const componentDir = path_1.default.join(componentsDir, componentName);
// Check for various component processor patterns
const possibleProcessorFiles = [
path_1.default.join(componentDir, `${componentName}-component-processor.ts`),
path_1.default.join(componentDir, 'index.ts'),
path_1.default.join(componentDir, `${componentName}.ts`)
];
// Component is valid if any of the processor files exist
const hasProcessor = possibleProcessorFiles.some(filePath => fs_extra_1.default.existsSync(filePath));
if (hasProcessor) {
componentNames.push(componentName);
utils_1.Logger.debug(`Discovered component: ${componentName}`);
}
}
}
// Also check for standalone processor files in the components directory
const standaloneProcessors = entries.filter(entry => entry.isFile() &&
entry.name.endsWith('-component-processor.ts') &&
entry.name !== 'base-component-processor.ts');
for (const file of standaloneProcessors) {
const componentName = file.name.replace('-component-processor.ts', '');
if (!componentNames.includes(componentName)) {
componentNames.push(componentName);
utils_1.Logger.debug(`Discovered standalone component processor: ${componentName}`);
}
}
// Fallback to known components if directory scanning finds nothing
if (componentNames.length === 0) {
utils_1.Logger.debug('No components discovered from filesystem, using fallback list');
return ['iocd'];
}
utils_1.Logger.debug(`Successfully discovered ${componentNames.length} component(s): ${componentNames.join(', ')}`);
return componentNames;
}
catch (error) {
utils_1.Logger.warning(`Failed to discover components from filesystem: ${error}`);
utils_1.Logger.debug('Using fallback component list');
return ['iocd'];
}
}
}
exports.ComponentRegistryImpl = ComponentRegistryImpl;
// Backward compatibility alias
exports.DefaultComponentRegistry = ComponentRegistryImpl;
// Default export for easier importing
exports.default = ComponentRegistryImpl;
//# sourceMappingURL=component-registry.js.map