-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathapp-log-process.ts
More file actions
111 lines (100 loc) · 3.35 KB
/
Copy pathapp-log-process.ts
File metadata and controls
111 lines (100 loc) · 3.35 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
import fs from 'node:fs';
import path from 'node:path';
import { readProcessCommand, readProcessStartTime } from '../utils/process-identity.ts';
export const APP_LOG_PID_FILENAME = 'app-log.pid';
export type AppLogState = 'active' | 'recovering' | 'failed';
export type AppLogResult = {
backend: 'ios-simulator' | 'ios-device' | 'android' | 'macos';
getState: () => AppLogState;
startedAt: number;
stop: () => Promise<void>;
wait: Promise<{ stdout: string; stderr: string; exitCode: number }>;
};
type StoredAppLogProcessMeta = {
pid: number;
startTime?: string;
command?: string;
};
function parsePidFile(raw: string): StoredAppLogProcessMeta | null {
const trimmed = raw.trim();
if (!trimmed) return null;
if (/^\d+$/.test(trimmed)) {
return { pid: Number.parseInt(trimmed, 10) };
}
try {
const parsed = JSON.parse(trimmed) as StoredAppLogProcessMeta;
if (!Number.isInteger(parsed.pid) || parsed.pid <= 0) return null;
return parsed;
} catch {
return null;
}
}
function isManagedAppLogCommand(command: string): boolean {
const normalized = command.toLowerCase().replaceAll('\\', '/');
return (
normalized.includes('log stream') ||
normalized.includes('logcat') ||
normalized.includes('devicectl device log stream')
);
}
function shouldTerminateStoredProcess(meta: StoredAppLogProcessMeta): boolean {
const currentStartTime = readProcessStartTime(meta.pid);
if (!currentStartTime) return false;
if (meta.startTime && currentStartTime !== meta.startTime) return false;
const currentCommand = readProcessCommand(meta.pid);
if (!currentCommand || !isManagedAppLogCommand(currentCommand)) return false;
if (meta.command && currentCommand !== meta.command) return false;
return true;
}
export function readStoredAppLogProcessMeta(
pidPath: string | undefined,
): StoredAppLogProcessMeta | null {
if (!pidPath || !fs.existsSync(pidPath)) return null;
try {
return parsePidFile(fs.readFileSync(pidPath, 'utf8'));
} catch {
return null;
}
}
export function writePidFile(pidPath: string | undefined, pid: number): void {
if (!pidPath) return;
const dir = path.dirname(pidPath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
const metadata: StoredAppLogProcessMeta = {
pid,
startTime: readProcessStartTime(pid) ?? undefined,
command: readProcessCommand(pid) ?? undefined,
};
fs.writeFileSync(pidPath, `${JSON.stringify(metadata)}\n`);
}
export function clearPidFile(pidPath: string | undefined): void {
if (!pidPath || !fs.existsSync(pidPath)) return;
try {
fs.unlinkSync(pidPath);
} catch {
// best-effort cleanup
}
}
export function cleanupStaleAppLogProcesses(sessionsDir: string): void {
if (!fs.existsSync(sessionsDir)) return;
const entries = fs.readdirSync(sessionsDir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const pidPath = path.join(sessionsDir, entry.name, APP_LOG_PID_FILENAME);
if (!fs.existsSync(pidPath)) continue;
try {
const meta = parsePidFile(fs.readFileSync(pidPath, 'utf8'));
if (meta && shouldTerminateStoredProcess(meta)) {
try {
process.kill(meta.pid, 'SIGTERM');
} catch {
// process already gone
}
}
} catch {
// ignore malformed pid files
} finally {
clearPidFile(pidPath);
}
}
}