feat: dev.py 新增 --stop 并支持安装完成后自动切换到开发模式
- 本地 dev 保持前台模型、不引入 PM2;--stop 复用 kill_project_servers 一键停止 - 无 .env 时监听根 .env 出现作为安装成功信号,自动停安装 API 并以正式模式重启 api/client,避开安装中重启 - 安装 API 在 dev 模式下不再退出、迁移失败透传真实原因;前端提交后统一轮询就绪再进后台
This commit is contained in:
@@ -225,6 +225,41 @@ function goAdmin(): void {
|
||||
location.href = '/admin';
|
||||
}
|
||||
|
||||
interface ApiBody {
|
||||
code: number;
|
||||
message?: string;
|
||||
data?: {
|
||||
installed?: boolean;
|
||||
needsDevRestart?: boolean;
|
||||
message?: string;
|
||||
restartRequired?: boolean;
|
||||
defaults?: Record<string, string | number>;
|
||||
};
|
||||
}
|
||||
|
||||
async function parseApiBody(res: Response): Promise<ApiBody | null> {
|
||||
const text = await res.text();
|
||||
if (!text.trim()) return null;
|
||||
try {
|
||||
return JSON.parse(text) as ApiBody;
|
||||
} catch {
|
||||
throw new Error(tt('invalidResponse'));
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchInstallStatus(): Promise<{ installed: boolean; needsDevRestart: boolean }> {
|
||||
try {
|
||||
const res = await fetch('/api/install/status');
|
||||
const body = await parseApiBody(res);
|
||||
return {
|
||||
installed: Boolean(body?.data?.installed),
|
||||
needsDevRestart: Boolean(body?.data?.needsDevRestart),
|
||||
};
|
||||
} catch {
|
||||
return { installed: false, needsDevRestart: false };
|
||||
}
|
||||
}
|
||||
|
||||
async function post(path: string, payload: Record<string, unknown>): Promise<{ ok: boolean; msg: string }> {
|
||||
try {
|
||||
const res = await fetch(path, {
|
||||
@@ -232,7 +267,8 @@ async function post(path: string, payload: Record<string, unknown>): Promise<{ o
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const body = await res.json();
|
||||
const body = await parseApiBody(res);
|
||||
if (!body) throw new Error(tt('emptyResponse'));
|
||||
if (!res.ok || body.code !== 0) throw new Error(body.message || tt('testFailed'));
|
||||
return { ok: true, msg: body.data?.message || tt('testOk') };
|
||||
} catch (e) {
|
||||
@@ -300,7 +336,8 @@ async function onNext(): Promise<void> {
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/install/status');
|
||||
const body = await res.json();
|
||||
const body = await parseApiBody(res);
|
||||
if (!body) throw new Error(tt('emptyResponse'));
|
||||
installed.value = Boolean(body.data?.installed);
|
||||
const d = body.data?.defaults || {};
|
||||
form.mysql.host = d.mysqlHost || form.mysql.host;
|
||||
@@ -318,18 +355,13 @@ onMounted(async () => {
|
||||
}
|
||||
});
|
||||
|
||||
async function pollUntilReady(): Promise<void> {
|
||||
async function pollUntilReady(): Promise<boolean> {
|
||||
for (let i = 0; i < 30; i += 1) {
|
||||
await new Promise((r) => setTimeout(r, 2000));
|
||||
try {
|
||||
const res = await fetch('/api/install/status');
|
||||
const body = await res.json();
|
||||
if (body.data?.installed) break;
|
||||
} catch {
|
||||
// 服务重启期间请求失败属正常
|
||||
}
|
||||
const st = await fetchInstallStatus();
|
||||
if (st.installed && !st.needsDevRestart) return true;
|
||||
}
|
||||
goAdmin();
|
||||
return false;
|
||||
}
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
@@ -345,10 +377,22 @@ async function submit(): Promise<void> {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(form),
|
||||
});
|
||||
const body = await res.json();
|
||||
if (!res.ok || body.code !== 0) throw new Error(body.message || tt('failed'));
|
||||
const body = await parseApiBody(res);
|
||||
// body 为空 = 安装成功后连接被 API 重启掐断(生产 PM2/Docker 或本地 dev.py 自动切换),视为已提交;
|
||||
// body 有值且非 0 = 安装校验失败(如 MySQL 连接失败),抛出真实原因。
|
||||
if (body && (!res.ok || body.code !== 0)) {
|
||||
throw new Error(body.message || tt('failed'));
|
||||
}
|
||||
installed.value = true;
|
||||
success.value = tt('done');
|
||||
await pollUntilReady();
|
||||
// 统一轮询等待系统就绪:本地 dev.py 会自动停掉安装 API 并以正式模式重启,
|
||||
// 生产由 PM2/Docker 按重启策略拉起;就绪后进入后台。
|
||||
if (await pollUntilReady()) {
|
||||
goAdmin();
|
||||
} else {
|
||||
// 超时兜底:多为 --install-mode 调试模式(写临时 .env,不会自动切换)
|
||||
success.value = tt('doneLocalDev');
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = (e as Error).message;
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user