|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { ChildProcess } from 'child_process'; |
| 10 | +import { Host } from './host'; |
| 11 | + |
| 12 | +const BUILD_SUCCEEDED_MESSAGE = 'Application bundle generation complete.'; |
| 13 | +const BUILD_FAILED_MESSAGE = 'Application bundle generation failed.'; |
| 14 | +const WAITING_FOR_CHANGES_MESSAGE = 'Watch mode enabled. Watching for file changes...'; |
| 15 | +const CHANGES_DETECTED_START_MESSAGE = '❯ Changes detected. Rebuilding...'; |
| 16 | +const CHANGES_DETECTED_SUCCESS_MESSAGE = '✔ Changes detected. Rebuilding...'; |
| 17 | + |
| 18 | +const BUILD_START_MESSAGES = [CHANGES_DETECTED_START_MESSAGE]; |
| 19 | +const BUILD_END_MESSAGES = [ |
| 20 | + BUILD_SUCCEEDED_MESSAGE, |
| 21 | + BUILD_FAILED_MESSAGE, |
| 22 | + WAITING_FOR_CHANGES_MESSAGE, |
| 23 | + CHANGES_DETECTED_SUCCESS_MESSAGE, |
| 24 | +]; |
| 25 | + |
| 26 | +export type BuildStatus = 'success' | 'failure' | 'unknown'; |
| 27 | + |
| 28 | +/** |
| 29 | + * An Angular development server managed by the MCP server. |
| 30 | + */ |
| 31 | +export interface DevServer { |
| 32 | + /** |
| 33 | + * Launches the dev server and returns immediately. |
| 34 | + * |
| 35 | + * Throws if this server is already running. |
| 36 | + */ |
| 37 | + start(): void; |
| 38 | + |
| 39 | + /** |
| 40 | + * If the dev server is running, stops it. |
| 41 | + */ |
| 42 | + stop(): void; |
| 43 | + |
| 44 | + /** |
| 45 | + * Gets all the server logs so far (stdout + stderr). |
| 46 | + */ |
| 47 | + getServerLogs(): string[]; |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets all the server logs from the latest build. |
| 51 | + */ |
| 52 | + getMostRecentBuild(): { status: BuildStatus; logs: string[] }; |
| 53 | + |
| 54 | + /** |
| 55 | + * Whether the dev server is currently being built, or is awaiting further changes. |
| 56 | + */ |
| 57 | + isBuilding(): boolean; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * A local Angular development server managed by the MCP server. |
| 62 | + */ |
| 63 | +export class LocalDevServer implements DevServer { |
| 64 | + readonly host: Host; |
| 65 | + readonly port?: number; |
| 66 | + readonly project?: string; |
| 67 | + |
| 68 | + private devServerProcess: ChildProcess | null = null; |
| 69 | + private serverLogs: string[] = []; |
| 70 | + private buildInProgress = false; |
| 71 | + private latestBuildLogStartIndex = 0; |
| 72 | + private latestBuildLogEndIndex = 0; |
| 73 | + private latestBuildStatus?: BuildStatus; |
| 74 | + |
| 75 | + constructor({ host, port, project }: { host: Host; port?: number; project?: string }) { |
| 76 | + this.host = host; |
| 77 | + this.project = project; |
| 78 | + this.port = port; |
| 79 | + } |
| 80 | + |
| 81 | + start() { |
| 82 | + if (this.devServerProcess) { |
| 83 | + throw Error('Dev server already started.'); |
| 84 | + } |
| 85 | + |
| 86 | + const args = ['serve']; |
| 87 | + if (this.project) { |
| 88 | + args.push(this.project); |
| 89 | + } |
| 90 | + if (this.port) { |
| 91 | + args.push(`--port=${this.port}`); |
| 92 | + } |
| 93 | + |
| 94 | + this.devServerProcess = this.host.spawn('ng', args, { stdio: 'pipe' }); |
| 95 | + this.devServerProcess.stdout?.on('data', (data) => { |
| 96 | + this.serverLogs.push(data.toString()); |
| 97 | + }); |
| 98 | + this.devServerProcess.stderr?.on('data', (data) => { |
| 99 | + this.serverLogs.push(data.toString()); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private addLog(log: string) { |
| 104 | + this.serverLogs.push(log); |
| 105 | + |
| 106 | + if (BUILD_START_MESSAGES.some((message) => log.startsWith(message))) { |
| 107 | + this.buildInProgress = true; |
| 108 | + this.latestBuildLogStartIndex = this.serverLogs.length; |
| 109 | + } else if (BUILD_END_MESSAGES.some((message) => log.startsWith(message))) { |
| 110 | + this.buildInProgress = false; |
| 111 | + this.latestBuildLogEndIndex = this.serverLogs.length; |
| 112 | + this.latestBuildStatus = log.startsWith(BUILD_FAILED_MESSAGE) ? 'success' : 'failure'; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + stop() { |
| 117 | + this.devServerProcess?.kill(); |
| 118 | + this.devServerProcess = null; |
| 119 | + } |
| 120 | + |
| 121 | + getServerLogs(): string[] { |
| 122 | + return [...this.serverLogs]; |
| 123 | + } |
| 124 | + |
| 125 | + getMostRecentBuild() { |
| 126 | + return { |
| 127 | + status: this.latestBuildStatus ?? 'unknown', |
| 128 | + logs: this.serverLogs.slice(this.latestBuildLogStartIndex, this.latestBuildLogEndIndex), |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + isBuilding() { |
| 133 | + return this.buildInProgress; |
| 134 | + } |
| 135 | +} |
0 commit comments