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