project.ts
1.67 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
import type { Route } from './context';
import { loadProjectProfile } from '../config';
/** 目标项目 dev server:状态、启动、停止。 */
export const projectRoutes: Route[] = [
// ---- GET /api/project/run:查询目标项目 dev-server 状态 ----
{
method: 'GET',
path: '/api/project/run',
handle: ({ ctx, res }) => {
ctx.sendJson(res, 200, ctx.projectRunner.getStatus());
},
},
// ---- POST /api/project/run:启动目标项目 dev server ----
{
method: 'POST',
path: '/api/project/run',
handle: async ({ ctx, req, res }) => {
let body: { dir?: string } = {};
try {
body = await ctx.readJsonBody<{ dir?: string }>(req);
} catch {
/* 忽略解析失败 */
}
// 启动目录取请求体 dir,否则取项目画像的 rootDir
const dir = (body.dir || loadProjectProfile()?.rootDir || '').trim();
if (!dir) {
ctx.sendJson(res, 400, {
ok: false,
error: '未配置工作目录(请先在「项目接入」中选择目标项目根)',
});
return;
}
const r = ctx.projectRunner.start(dir);
ctx.sendJson(res, r.ok ? 200 : 409, { ok: r.ok, error: r.error });
},
},
// ---- POST /api/project/run/stop:停止目标项目 dev server ----
{
method: 'POST',
path: '/api/project/run/stop',
handle: ({ ctx, res }) => {
ctx.projectRunner.stop();
ctx.sendJson(res, 200, { ok: true });
},
},
];