UNPKG

ruch

Version:

Revolutionary React TypeScript CLI with hexagonal architecture & AI-powered development assistance. Create maintainable, scalable applications with domain-driven design and integrated AI tooling.

181 lines 9.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateServiceContext = exports.generateServiceContext = exports.initServiceContext = void 0; const fs_extra_1 = __importDefault(require("fs-extra")); const readline_1 = __importDefault(require("readline")); const file_operations_1 = require("../utils/file-operations"); const logging_1 = require("../utils/logging"); const domain_analyzer_1 = require("../utils/domain-analyzer"); const context_1 = require("../templates/context"); /** * Pure function to validate context creation preconditions */ const validateContextCreationPreconditions = async (fileSystem, contextFilePath) => { const contextExists = await (0, file_operations_1.validateFileExists)(fileSystem, contextFilePath); if (contextExists) { return { canCreate: false, reason: 'Service context already exists' }; } return { canCreate: true }; }; /** * Pure function to validate context update preconditions */ const validateContextUpdatePreconditions = async (fileSystem, contextFilePath) => { const contextExists = await (0, file_operations_1.validateFileExists)(fileSystem, contextFilePath); if (!contextExists) { return { canUpdate: false, reason: 'Service context does not exist. Use "context init" or "context generate" first.' }; } return { canUpdate: true }; }; /** * Pure function to create context files */ const createContextFiles = async (fileSystem, contextPath, contextFilePath, testFilePath, contextContent, testContent) => { await (0, file_operations_1.createDirectory)(fileSystem, contextPath); await (0, file_operations_1.writeFile)(fileSystem, contextFilePath, contextContent); await (0, file_operations_1.writeFile)(fileSystem, testFilePath, testContent); }; /** * Pure function to log context creation success */ const logContextCreationSuccess = (log, contextFilePath, domains) => { (0, logging_1.logSuccess)(log, 'Service context created successfully!'); (0, logging_1.logInfo)(log, `Location: ${contextFilePath}`); if (domains.length > 0) { (0, logging_1.logInfo)(log, `Integrated ${domains.length} domain(s):`); domains.forEach(domain => (0, logging_1.logInfo)(log, ` - ${domain.name}`)); } (0, logging_1.logInfo)(log, '💡 Import with: import { useServices } from \'./context/ServiceContext\''); }; /** * Pure function to ask for user confirmation */ const askForConfirmation = (question) => { return new Promise((resolve) => { const rl = readline_1.default.createInterface({ input: process.stdin, output: process.stdout }); rl.question(`${question} (y/N): `, (answer) => { rl.close(); resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes'); }); }); }; /** * Initialize empty service context */ const initServiceContext = async (fileSystem = fs_extra_1.default, log = require('../utils/logger').logger, projectRoot = process.cwd()) => { const contextPath = (0, domain_analyzer_1.buildContextPath)(projectRoot); const contextFilePath = (0, domain_analyzer_1.buildServiceContextFilePath)(projectRoot); const testFilePath = (0, domain_analyzer_1.buildServiceContextTestFilePath)(projectRoot); try { const validation = await validateContextCreationPreconditions(fileSystem, contextFilePath); if (!validation.canCreate) { (0, logging_1.logWarning)(log, validation.reason || 'Cannot create service context'); return; } (0, logging_1.logInfo)(log, 'Initializing empty service context...'); const contextContent = (0, context_1.getEmptyServiceContextTemplate)(); const testContent = (0, context_1.getServiceContextTestTemplate)([]); await createContextFiles(fileSystem, contextPath, contextFilePath, testFilePath, contextContent, testContent); logContextCreationSuccess(log, contextFilePath, []); } catch (error) { (0, logging_1.logError)(log, `Error initializing service context: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; exports.initServiceContext = initServiceContext; /** * Generate service context with all existing domains */ const generateServiceContext = async (fileSystem = fs_extra_1.default, log = require('../utils/logger').logger, projectRoot = process.cwd()) => { const contextPath = (0, domain_analyzer_1.buildContextPath)(projectRoot); const contextFilePath = (0, domain_analyzer_1.buildServiceContextFilePath)(projectRoot); const testFilePath = (0, domain_analyzer_1.buildServiceContextTestFilePath)(projectRoot); try { const contextExists = await (0, file_operations_1.validateFileExists)(fileSystem, contextFilePath); if (contextExists) { (0, logging_1.logWarning)(log, 'Service context already exists. Use "context update" to add new domains.'); return; } (0, logging_1.logInfo)(log, 'Generating service context with existing domains...'); const domains = await (0, domain_analyzer_1.getExistingDomains)(fileSystem, projectRoot); if (domains.length === 0) { (0, logging_1.logWarning)(log, 'No domains found. Creating empty service context.'); await (0, exports.initServiceContext)(fileSystem, log, projectRoot); return; } const contextContent = (0, context_1.getServiceContextTemplate)(domains); const testContent = (0, context_1.getServiceContextTestTemplate)(domains); await createContextFiles(fileSystem, contextPath, contextFilePath, testFilePath, contextContent, testContent); logContextCreationSuccess(log, contextFilePath, domains); } catch (error) { (0, logging_1.logError)(log, `Error generating service context: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; exports.generateServiceContext = generateServiceContext; /** * Update service context with missing domains */ const updateServiceContext = async (fileSystem = fs_extra_1.default, log = require('../utils/logger').logger, projectRoot = process.cwd(), skipConfirmation = false) => { const contextFilePath = (0, domain_analyzer_1.buildServiceContextFilePath)(projectRoot); const testFilePath = (0, domain_analyzer_1.buildServiceContextTestFilePath)(projectRoot); try { const validation = await validateContextUpdatePreconditions(fileSystem, contextFilePath); if (!validation.canUpdate) { (0, logging_1.logError)(log, validation.reason || 'Cannot update service context'); return; } (0, logging_1.logInfo)(log, 'Checking for missing domains...'); const missingDomains = await (0, domain_analyzer_1.findMissingDomains)(fileSystem, projectRoot); if (missingDomains.length === 0) { (0, logging_1.logInfo)(log, 'Service context is already up to date. No missing domains found.'); return; } (0, logging_1.logInfo)(log, `Found ${missingDomains.length} missing domain(s):`); missingDomains.forEach(domain => (0, logging_1.logInfo)(log, ` - ${domain.name}`)); // Create backup before overwriting const backupPath = `${contextFilePath}.backup.${Date.now()}`; const existingContent = await (0, file_operations_1.readFile)(fileSystem, contextFilePath, 'utf8'); await (0, file_operations_1.writeFile)(fileSystem, backupPath, existingContent); (0, logging_1.logWarning)(log, '⚠️ WARNING: This will overwrite your existing ServiceContext.tsx'); (0, logging_1.logInfo)(log, `📁 Backup created at: ${backupPath}`); (0, logging_1.logInfo)(log, '💡 Review the backup if you had custom modifications'); // Ask for confirmation unless skipped (for tests) if (!skipConfirmation) { const confirmed = await askForConfirmation('Do you want to continue?'); if (!confirmed) { (0, logging_1.logInfo)(log, '❌ Operation cancelled by user'); // Remove the backup since we're not proceeding await fileSystem.remove(backupPath); return; } } // Get all domains (existing + missing) for regeneration const allDomains = await (0, domain_analyzer_1.getExistingDomains)(fileSystem, projectRoot); const contextContent = (0, context_1.getServiceContextTemplate)(allDomains); const testContent = (0, context_1.getServiceContextTestTemplate)(allDomains); await (0, file_operations_1.writeFile)(fileSystem, contextFilePath, contextContent); await (0, file_operations_1.writeFile)(fileSystem, testFilePath, testContent); (0, logging_1.logSuccess)(log, 'Service context updated successfully!'); (0, logging_1.logInfo)(log, `Added ${missingDomains.length} domain(s) to service context`); (0, logging_1.logInfo)(log, `Total domains: ${allDomains.length}`); (0, logging_1.logInfo)(log, `🔄 If you had custom code, restore it from: ${backupPath}`); } catch (error) { (0, logging_1.logError)(log, `Error updating service context: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; exports.updateServiceContext = updateServiceContext; //# sourceMappingURL=context.js.map