import fs from "fs-extra"; import path from "path"; import { DeploymentFactory } from "./deployment-factory"; import { DeploymentConfig, DeploymentResult, DeploymentStatus, DeploymentHistoryItem, RollbackResult, } from "./platform-adapter"; import { logger } from "../utils/logger"; /** * Deployment service for deploying applications */ export class DeploymentService { private config: DeploymentConfig; /** * Create a new deployment service * @param config Deployment configuration */ constructor(config: Partial = {}) { // Set default configuration this.config = { platform: "vercel", environment: "production", ...config, }; // Load configuration from file this.loadConfig(); } /** * Deploy the application * @returns Deployment result */ async deploy(): Promise { try { // Create platform adapter const adapter = DeploymentFactory.createAdapter(this.config); // Deploy application const result = await adapter.deploy(this.config); // Save deployment history if (result.success) { await this.saveDeploymentHistory({ id: result.deploymentId || new Date().toISOString(), url: result.url || "", status: "deployed", createdAt: new Date().toLocaleString(), }); } return result; } catch (error) { logger.error(`Deployment error: ${(error as Error).message}`); return { success: false, error: `Failed to deploy: ${(error as Error).message}`, }; } } /** * Get deployment status * @param deploymentId Deployment ID * @returns Deployment status */ async getStatus(deploymentId?: string): Promise { try { // Create platform adapter const adapter = DeploymentFactory.createAdapter(this.config); // Get deployment status return await adapter.getStatus(deploymentId); } catch (error) { logger.error(`Status error: ${(error as Error).message}`); throw new Error( `Failed to get deployment status: ${(error as Error).message}` ); } } /** * Get deployment history * @returns Deployment history */ async getHistory(): Promise { try { // Load deployment history from file const historyPath = path.join( process.cwd(), ".rsdt", "deployment-history.json" ); if (fs.existsSync(historyPath)) { const history = await fs.readJson(historyPath); return history; } // If no history file exists, get history from platform const adapter = DeploymentFactory.createAdapter(this.config); return await adapter.getHistory(); } catch (error) { logger.error(`History error: ${(error as Error).message}`); throw new Error( `Failed to get deployment history: ${(error as Error).message}` ); } } /** * Roll back to a previous deployment * @param deploymentId Deployment ID * @returns Rollback result */ async rollback(deploymentId: string): Promise { try { // Create platform adapter const adapter = DeploymentFactory.createAdapter(this.config); // Roll back deployment const result = await adapter.rollback(deploymentId); // Update deployment history if (result.success) { await this.updateDeploymentHistory(deploymentId, "rolled back"); } return result; } catch (error) { logger.error(`Rollback error: ${(error as Error).message}`); return { success: false, message: `Failed to roll back: ${(error as Error).message}`, }; } } /** * Configure a custom domain * @param domain Custom domain * @returns Success status */ async configureDomain(domain: string): Promise { try { // Create platform adapter const adapter = DeploymentFactory.createAdapter(this.config); // Configure domain return await adapter.configureDomain(domain); } catch (error) { logger.error( `Domain configuration error: ${(error as Error).message}` ); return false; } } /** * Load configuration from file */ private loadConfig(): void { try { // Check if configuration file exists const configPath = path.join(process.cwd(), "rsdt.config.js"); if (fs.existsSync(configPath)) { // Load configuration const config = require(configPath); // Merge with current configuration if (config.deployment) { this.config = { ...this.config, ...config.deployment, }; } } } catch (error) { logger.warn( `Failed to load configuration: ${(error as Error).message}` ); } } /** * Save deployment history * @param deployment Deployment history item */ private async saveDeploymentHistory( deployment: DeploymentHistoryItem ): Promise { try { // Create .rsdt directory if it doesn't exist const rsdtDir = path.join(process.cwd(), ".rsdt"); await fs.ensureDir(rsdtDir); // Load existing history const historyPath = path.join(rsdtDir, "deployment-history.json"); let history: DeploymentHistoryItem[] = []; if (fs.existsSync(historyPath)) { history = await fs.readJson(historyPath); } // Add new deployment to history history.unshift(deployment); // Limit history to 10 items if (history.length > 10) { history = history.slice(0, 10); } // Save history await fs.writeJson(historyPath, history, { spaces: 2 }); } catch (error) { logger.warn( `Failed to save deployment history: ${(error as Error).message}` ); } } /** * Update deployment history * @param deploymentId Deployment ID * @param status Deployment status */ private async updateDeploymentHistory( deploymentId: string, status: string ): Promise { try { // Create .rsdt directory if it doesn't exist const rsdtDir = path.join(process.cwd(), ".rsdt"); await fs.ensureDir(rsdtDir); // Load existing history const historyPath = path.join(rsdtDir, "deployment-history.json"); if (!fs.existsSync(historyPath)) { return; } // Load history const history: DeploymentHistoryItem[] = await fs.readJson(historyPath); // Update deployment status const updatedHistory = history.map((item) => { if (item.id === deploymentId) { return { ...item, status, }; } return item; }); // Save history await fs.writeJson(historyPath, updatedHistory, { spaces: 2 }); } catch (error) { logger.warn( `Failed to update deployment history: ${(error as Error).message}` ); } } }