Repository navigation
Expand file tree
/
Copy pathexec-trace.ts
More file actions
69 lines (66 loc) · 1.95 KB
/
Copy pathexec-trace.ts
File metadata and controls
69 lines (66 loc) · 1.95 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
import { randomUUID } from 'node:crypto';
import path from 'node:path';
import {
emitDiagnostic,
flushDiagnosticsToSessionFile,
getDiagnosticsMeta,
updateDiagnosticsScope,
} from './diagnostics.ts';
export type ExecSettlementReason =
| 'success'
| 'exit_failure'
| 'timeout'
| 'abort'
| 'spawn_error'
| 'stdin_error';
type ExecLifecycleEvent =
| 'spawn_start'
| 'spawn'
| 'deadline'
| 'abort'
| 'error'
| 'exit'
| 'close'
| 'settled';
type ExecLifecycleData = {
pid?: number;
exitCode?: number | null;
signal?: NodeJS.Signals | null;
reason?: ExecSettlementReason;
};
export function createExecTraceContext(
cmd: string,
args: readonly string[],
timeoutMs: number | undefined,
envTraceEnabled: boolean,
): { emit(event: ExecLifecycleEvent, data?: ExecLifecycleData): void } {
const meta = getDiagnosticsMeta();
if (!meta.debug && !envTraceEnabled) return { emit: () => {} };
if (envTraceEnabled && !meta.flushOnSuccess) updateDiagnosticsScope({ flushOnSuccess: true });
const started = performance.now();
const execId = randomUUID();
let pid: number | undefined;
let settlementEmitted = false;
const command = path.basename(cmd);
const simctlArgs = args[1] === '--set' ? args.slice(3) : args.slice(1);
const operation =
command === 'xcrun' &&
args[0] === 'simctl' &&
simctlArgs.length === 3 &&
simctlArgs.join(' ') === 'list devices -j'
? 'simctl_list_devices'
: undefined;
const emit = (event: ExecLifecycleEvent, data: ExecLifecycleData = {}): void => {
pid = data.pid ?? pid;
emitDiagnostic({
level: 'debug',
phase: 'exec_command',
durationMs: performance.now() - started,
data: { execId, command, operation, event, pid, timeoutMs, ...data },
});
if (event === 'settled') settlementEmitted = true;
if (event === 'close' && settlementEmitted && envTraceEnabled) flushDiagnosticsToSessionFile();
};
emit('spawn_start');
return { emit };
}