-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhelpers.ts
More file actions
132 lines (118 loc) · 3.62 KB
/
helpers.ts
File metadata and controls
132 lines (118 loc) · 3.62 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
import { fileURLToPath } from 'node:url';
import { x } from 'tinyexec';
export const STORYBOOK_DIR = fileURLToPath(new URL('..', import.meta.url));
type StorybookProcess = ReturnType<typeof x>;
export function createMCPRequestBody(method: string, params: any = {}, id: number = 1) {
return {
jsonrpc: '2.0',
id,
method,
params,
};
}
export async function parseMCPResponse(response: Response) {
const text = await response.text();
const dataLine = text.split('\n').find((line) => line.startsWith('data: '));
const jsonText = dataLine!.replace(/^data: /, '').trim();
return JSON.parse(jsonText);
}
export async function waitForMcpEndpoint(
endpoint: string,
options: {
maxAttempts?: number;
interval?: number;
acceptStatuses?: number[];
storybookProcess?: StorybookProcess | null;
} = {},
): Promise<void> {
const { maxAttempts = 120, interval = 500, acceptStatuses = [], storybookProcess } = options;
const { promise, resolve, reject } = Promise.withResolvers<void>();
let attempts = 0;
let lastStatus: number | null = null;
let lastErrorMessage: string | null = null;
const intervalId = setInterval(async () => {
attempts++;
try {
const storybookPid = storybookProcess?.process?.pid;
const storybookExitCode = storybookProcess?.process?.exitCode;
if (storybookPid && storybookExitCode !== null) {
clearInterval(intervalId);
reject(
new Error(
`Storybook exited before MCP became ready (pid=${storybookPid}, exitCode=${storybookExitCode})`,
),
);
return;
}
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(createMCPRequestBody('tools/list')),
});
lastStatus = response.status;
if (response.ok || acceptStatuses.includes(response.status)) {
clearInterval(intervalId);
resolve();
return;
}
} catch (error) {
lastErrorMessage = error instanceof Error ? error.message : String(error);
}
if (attempts >= maxAttempts) {
clearInterval(intervalId);
reject(
new Error(
`MCP endpoint failed to start in time (attempts=${attempts}, lastStatus=${lastStatus ?? 'none'}, lastError=${lastErrorMessage ?? 'none'})`,
),
);
}
}, interval);
return promise;
}
export async function killPort(port: number): Promise<void> {
try {
if (process.platform === 'win32') {
await x('npx', ['kill-port', String(port)]);
} else {
const { stdout } = await x('lsof', ['-ti', `:${port}`]);
if (stdout.trim()) {
await x('kill', ['-9', ...stdout.trim().split('\n')]);
}
}
await new Promise((resolve) => setTimeout(resolve, 1000));
} catch {
// No process on port, continue
}
}
export function startStorybook(configDir: string, port: number): ReturnType<typeof x> {
return x('pnpm', ['storybook', '--config-dir', configDir, '--port', String(port)], {
nodeOptions: {
cwd: STORYBOOK_DIR,
},
});
}
export async function stopStorybook(storybookProcess: ReturnType<typeof x> | null): Promise<void> {
if (!storybookProcess || !storybookProcess.process) {
return;
}
const processToStop = storybookProcess.process;
if (processToStop.exitCode !== null || !processToStop.pid) {
return;
}
const waitForExit = Promise.withResolvers<void>();
processToStop.once('exit', () => waitForExit.resolve());
storybookProcess.kill('SIGTERM');
const timeout = setTimeout(async () => {
try {
if (process.platform === 'win32') {
await x('taskkill', ['/pid', String(processToStop.pid), '/t', '/f']);
} else {
processToStop.kill('SIGKILL');
}
} catch {
// Process may already be gone.
}
}, 5_000);
await waitForExit.promise;
clearTimeout(timeout);
}