UNPKG

@elsikora/setup-wizard

Version:

Setup Wizard - CLI scaffolding utility

76 lines (73 loc) 3.59 kB
#!/usr/bin/env node import { ConfigMapper } from '../../application/mapper/config.mapper.js'; import { EModule } from '../../domain/enum/module.enum.js'; import { ModuleServiceMapper } from '../mapper/module-service.mapper.js'; import { CosmicConfigService } from '../service/cosmi-config-config.service.js'; /** * Command responsible for initializing and installing selected modules. * Implements the ICommand interface to provide standard command execution. */ class InitCommand { /** CLI interface service for user interaction */ CLI_INTERFACE_SERVICE; /** Configuration service for reading and writing config */ CONFIG_SERVICE; /** File system service for file operations */ FILE_SYSTEM_SERVICE; /** Properties defining which modules to install */ PROPERTIES; /** * Initializes a new instance of the InitCommand. * @param properties - Properties defining which modules to install * @param cliInterfaceService - Service for CLI user interactions * @param fileSystemService - Service for file system operations */ constructor(properties, cliInterfaceService, fileSystemService) { this.PROPERTIES = properties; this.CLI_INTERFACE_SERVICE = cliInterfaceService; this.FILE_SYSTEM_SERVICE = fileSystemService; this.CONFIG_SERVICE = new CosmicConfigService(fileSystemService); } /** * Executes the initialization command. * Reads existing configuration if available, determines which modules to install, * installs selected modules, and updates the configuration. * @returns Promise that resolves when execution is complete */ async execute() { let properties = this.PROPERTIES; // eslint-disable-next-line @elsikora/typescript/naming-convention if (Object.values(properties).every((value) => !value) && (await this.CONFIG_SERVICE.exists())) { const config = await this.CONFIG_SERVICE.get(); properties = ConfigMapper.fromConfigToInitCommandProperties(config); // eslint-disable-next-line @elsikora/typescript/naming-convention if (Object.values(properties).every((value) => !value)) { this.CLI_INTERFACE_SERVICE.info(`Configuration was found but no modules were enabled.\n\nPlease edit the configuration file to enable modules or:\n- pass the --all flag to enable all modules\n- pass command flags to enable specific modules`); return; } } const moduleServiceMapper = new ModuleServiceMapper(this.CLI_INTERFACE_SERVICE, this.FILE_SYSTEM_SERVICE); this.CLI_INTERFACE_SERVICE.clear(); // eslint-disable-next-line @elsikora/typescript/naming-convention const shouldInstallAll = Object.values(properties).every((value) => !value); const modulesToInstall = []; const setupResults = {}; if (shouldInstallAll) { modulesToInstall.push(...Object.values(EModule)); } else { for (const [module, shouldInstall] of Object.entries(properties)) { if (shouldInstall) { modulesToInstall.push(module); } } } for (const module of modulesToInstall) { const moduleService = moduleServiceMapper.getModuleService(module); setupResults[module] = await moduleService.install(); } await this.CONFIG_SERVICE.merge(ConfigMapper.fromSetupResultsToConfig(setupResults)); } } export { InitCommand }; //# sourceMappingURL=init.command.js.map