Add shorts_yt_quota module, Shorts UI quota banner, overview tweaks (hide upcoming opens, align events panel height), Electron desktop client scaffold, and deploy scripts; update README. Co-authored-by: Cursor <cursoragent@cursor.com>
297 lines
7.8 KiB
TypeScript
297 lines
7.8 KiB
TypeScript
import { ChildProcess, spawn, SpawnOptions } from "node:child_process";
|
|
import fs from "node:fs";
|
|
import http from "node:http";
|
|
import net from "node:net";
|
|
import path from "node:path";
|
|
import { app } from "electron";
|
|
|
|
export type ServiceName = "pocketbase" | "api" | "youtube";
|
|
|
|
type Running = {
|
|
name: ServiceName;
|
|
proc: ChildProcess;
|
|
cwd: string;
|
|
};
|
|
|
|
const children: Running[] = [];
|
|
|
|
function isPackaged(): boolean {
|
|
return app.isPackaged;
|
|
}
|
|
|
|
export function resolveRuntimeRoot(): string {
|
|
if (isPackaged()) {
|
|
return path.join(process.resourcesPath, "runtime");
|
|
}
|
|
return path.resolve(__dirname, "..", "..");
|
|
}
|
|
|
|
export function resolvePaths() {
|
|
const root = resolveRuntimeRoot();
|
|
if (isPackaged()) {
|
|
return {
|
|
root,
|
|
backend: path.join(root, "backend"),
|
|
pb: path.join(root, "pb"),
|
|
youtube: path.join(root, "youtube-svc"),
|
|
};
|
|
}
|
|
return {
|
|
root,
|
|
backend: path.join(root, "backend"),
|
|
pb: path.join(root, "pb"),
|
|
youtube: path.join(root, "youtube-svc"),
|
|
};
|
|
}
|
|
|
|
function spawnLogged(
|
|
name: ServiceName,
|
|
command: string,
|
|
args: string[],
|
|
opts: SpawnOptions,
|
|
): ChildProcess {
|
|
const proc = spawn(command, args, {
|
|
...opts,
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
windowsHide: true,
|
|
});
|
|
const prefix = `[${name}]`;
|
|
let lines = 0;
|
|
const onChunk = (buf: Buffer, isErr: boolean) => {
|
|
if (lines > 30) return;
|
|
const line = String(buf).trim();
|
|
if (!line) return;
|
|
lines += 1;
|
|
if (isErr || lines <= 6) {
|
|
(isErr ? console.error : console.log)(prefix, line.slice(0, 220));
|
|
}
|
|
};
|
|
proc.stdout?.on("data", (b) => onChunk(b, false));
|
|
proc.stderr?.on("data", (b) => onChunk(b, true));
|
|
proc.on("exit", (code, signal) => {
|
|
console.log(prefix, `exited code=${code} signal=${signal}`);
|
|
});
|
|
children.push({ name, proc, cwd: String(opts.cwd || "") });
|
|
return proc;
|
|
}
|
|
|
|
function portOpen(port: number, host = "127.0.0.1"): Promise<boolean> {
|
|
return new Promise((resolve) => {
|
|
const sock = net.connect({ port, host });
|
|
sock.setTimeout(500);
|
|
sock.on("connect", () => {
|
|
sock.destroy();
|
|
resolve(true);
|
|
});
|
|
sock.on("timeout", () => {
|
|
sock.destroy();
|
|
resolve(false);
|
|
});
|
|
sock.on("error", () => resolve(false));
|
|
});
|
|
}
|
|
|
|
function probeHttp(url: string, timeoutMs = 1000): Promise<boolean> {
|
|
return new Promise((resolve) => {
|
|
const req = http.get(url, { timeout: timeoutMs }, (res) => {
|
|
res.resume();
|
|
resolve((res.statusCode || 500) < 500);
|
|
});
|
|
req.on("timeout", () => {
|
|
req.destroy();
|
|
resolve(false);
|
|
});
|
|
req.on("error", () => resolve(false));
|
|
});
|
|
}
|
|
|
|
function waitPort(port: number, timeoutMs = 45000, host = "127.0.0.1"): Promise<void> {
|
|
const start = Date.now();
|
|
return new Promise((resolve, reject) => {
|
|
let settled = false;
|
|
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
let sock: net.Socket | null = null;
|
|
|
|
const finish = (err?: Error) => {
|
|
if (settled) return;
|
|
settled = true;
|
|
if (timer) clearTimeout(timer);
|
|
try {
|
|
sock?.destroy();
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
if (err) reject(err);
|
|
else resolve();
|
|
};
|
|
|
|
const tick = () => {
|
|
if (settled) return;
|
|
if (Date.now() - start > timeoutMs) {
|
|
finish(new Error(`timeout waiting port ${host}:${port}`));
|
|
return;
|
|
}
|
|
try {
|
|
sock?.destroy();
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
sock = net.connect({ port, host });
|
|
sock.setTimeout(800);
|
|
sock.once("connect", () => finish());
|
|
sock.once("timeout", () => {
|
|
try {
|
|
sock?.destroy();
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
timer = setTimeout(tick, 400);
|
|
});
|
|
sock.once("error", () => {
|
|
timer = setTimeout(tick, 400);
|
|
});
|
|
};
|
|
|
|
tick();
|
|
});
|
|
}
|
|
|
|
function findPython(): string {
|
|
return process.platform === "win32" ? "python" : "python3";
|
|
}
|
|
|
|
export async function startLocalServices(ports: {
|
|
apiPort: number;
|
|
pbPort: number;
|
|
ytPort: number;
|
|
}): Promise<{ apiBase: string; status: Record<string, string> }> {
|
|
const paths = resolvePaths();
|
|
const status: Record<string, string> = {};
|
|
const apiBase = `http://127.0.0.1:${ports.apiPort}`;
|
|
|
|
// Fast path: ports already open (avoid HTTP probes that can hang a wedged stack)
|
|
if (await portOpen(ports.apiPort)) {
|
|
status.api = "ok";
|
|
status.pocketbase = (await portOpen(ports.pbPort)) ? "ok" : "down";
|
|
status.youtube = (await portOpen(ports.ytPort)) ? "ok" : "optional";
|
|
console.log("[boot] reuse open ports", status);
|
|
return { apiBase, status };
|
|
}
|
|
|
|
// --- PocketBase ---
|
|
if (!(await portOpen(ports.pbPort))) {
|
|
const pbExe = path.join(paths.pb, process.platform === "win32" ? "pocketbase.exe" : "pocketbase");
|
|
if (!fs.existsSync(pbExe)) {
|
|
status.pocketbase = `missing: ${pbExe}`;
|
|
} else {
|
|
spawnLogged("pocketbase", pbExe, ["serve", `--http=127.0.0.1:${ports.pbPort}`], {
|
|
cwd: paths.pb,
|
|
env: { ...process.env },
|
|
});
|
|
try {
|
|
await waitPort(ports.pbPort, 20000);
|
|
status.pocketbase = "ok";
|
|
} catch (e) {
|
|
status.pocketbase = String((e as Error).message);
|
|
}
|
|
}
|
|
} else {
|
|
status.pocketbase = "ok";
|
|
}
|
|
|
|
// --- youtube-svc (optional) ---
|
|
if (!(await portOpen(ports.ytPort))) {
|
|
const ytPkg = path.join(paths.youtube, "package.json");
|
|
if (!fs.existsSync(ytPkg)) {
|
|
status.youtube = "skipped";
|
|
} else {
|
|
const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
|
|
spawnLogged("youtube", npmCmd, ["start"], {
|
|
cwd: paths.youtube,
|
|
env: { ...process.env, YT_SVC_PORT: String(ports.ytPort) },
|
|
shell: process.platform === "win32",
|
|
});
|
|
status.youtube = "optional";
|
|
void waitPort(ports.ytPort, 20000)
|
|
.then(() => {
|
|
status.youtube = "ok";
|
|
})
|
|
.catch(() => {
|
|
status.youtube = "optional";
|
|
});
|
|
}
|
|
} else {
|
|
status.youtube = "ok";
|
|
}
|
|
|
|
// --- FastAPI ---
|
|
if (await portOpen(ports.apiPort)) {
|
|
status.api = "ok";
|
|
} else {
|
|
const python = findPython();
|
|
const env = {
|
|
...process.env,
|
|
DY_PB_URL: `http://127.0.0.1:${ports.pbPort}`,
|
|
DY_YOUTUBE_SVC_URL: `http://127.0.0.1:${ports.ytPort}`,
|
|
DY_API_HOST: "127.0.0.1",
|
|
DY_API_PORT: String(ports.apiPort),
|
|
DY_AUTH_ENABLED: "false",
|
|
DY_DISABLE_LIVE_MAP_LOOP: "true",
|
|
// Keep event loop free for UI clicks on Windows desktop
|
|
DY_DESKTOP_LIGHT: "true",
|
|
DY_CORS_ORIGINS: [
|
|
"http://127.0.0.1:5173",
|
|
"http://localhost:5173",
|
|
"http://127.0.0.1:18574",
|
|
"http://localhost:18574",
|
|
].join(","),
|
|
DY_S3_ENABLED: process.env.DY_S3_ENABLED || "false",
|
|
TZ: "Asia/Shanghai",
|
|
PYTHONUTF8: "1",
|
|
};
|
|
const proc = spawnLogged(
|
|
"api",
|
|
python,
|
|
["run_desktop_api.py"],
|
|
{ cwd: paths.backend, env },
|
|
);
|
|
let spawnFailed = false;
|
|
proc.once("exit", (code) => {
|
|
if (code && code !== 0) spawnFailed = true;
|
|
});
|
|
try {
|
|
await waitPort(ports.apiPort, 45000);
|
|
status.api = "ok";
|
|
} catch (e) {
|
|
status.api = spawnFailed
|
|
? "api process exited (port busy?)"
|
|
: String((e as Error).message);
|
|
}
|
|
}
|
|
|
|
console.log("[boot] services", status);
|
|
return { apiBase, status };
|
|
}
|
|
|
|
export function stopLocalServices(): void {
|
|
for (const child of [...children].reverse()) {
|
|
try {
|
|
if (process.platform === "win32" && child.proc.pid) {
|
|
spawn("taskkill", ["/pid", String(child.proc.pid), "/T", "/F"], {
|
|
stdio: "ignore",
|
|
windowsHide: true,
|
|
});
|
|
} else {
|
|
child.proc.kill("SIGTERM");
|
|
}
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
children.length = 0;
|
|
}
|
|
|
|
export function listServicePids(): { name: ServiceName; pid?: number }[] {
|
|
return children.map((c) => ({ name: c.name, pid: c.proc.pid }));
|
|
}
|