xcodebug 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GUIDE.md +213 -0
- package/LICENSE +21 -0
- package/README.md +449 -2
- package/archive.ts +164 -0
- package/capture.ts +327 -0
- package/changes.ts +368 -0
- package/devices.ts +278 -0
- package/exec.ts +122 -0
- package/index.ts +2274 -0
- package/package.json +52 -4
- package/progress.ts +171 -0
- package/xcode.ts +220 -0
package/exec.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small child-process helpers shared by every xcodebug command.
|
|
3
|
+
*
|
|
4
|
+
* Every invocation is bounded by a timeout and a byte cap so a stuck
|
|
5
|
+
* xcrun/devicectl call can never wedge a tool call or grow without limit.
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
|
|
9
|
+
export interface CommandResult {
|
|
10
|
+
stdout: string;
|
|
11
|
+
stderr: string;
|
|
12
|
+
code: number;
|
|
13
|
+
timedOut: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CommandOptions {
|
|
17
|
+
cwd?: string;
|
|
18
|
+
timeoutMs?: number;
|
|
19
|
+
maxBytes?: number;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
/** Called as output arrives, before the final buffered result is returned. */
|
|
22
|
+
onChunk?: (stream: "stdout" | "stderr", chunk: string) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function runCommand(
|
|
26
|
+
command: string,
|
|
27
|
+
args: string[],
|
|
28
|
+
options: CommandOptions = {},
|
|
29
|
+
): Promise<CommandResult> {
|
|
30
|
+
const { cwd, timeoutMs = 120_000, maxBytes = 2_000_000, signal, onChunk } = options;
|
|
31
|
+
return new Promise((resolve) => {
|
|
32
|
+
let stdout = "";
|
|
33
|
+
let stderr = "";
|
|
34
|
+
let timedOut = false;
|
|
35
|
+
let settled = false;
|
|
36
|
+
|
|
37
|
+
// `detached` puts the child in its own process group so the whole tree can
|
|
38
|
+
// be signalled at once. `sh -lc "cd x && npm test"` runs the real work as a
|
|
39
|
+
// grandchild that inherits our stdio pipes: killing only the `sh` wrapper
|
|
40
|
+
// leaves it running, and `close` then never fires because the pipes are
|
|
41
|
+
// still open - the promise would hang forever and the runner would outlive
|
|
42
|
+
// the cancelled run.
|
|
43
|
+
const child = spawn(command, args, { cwd, stdio: ["ignore", "pipe", "pipe"], detached: true });
|
|
44
|
+
const killTree = (): void => {
|
|
45
|
+
const pid = child.pid;
|
|
46
|
+
if (pid === undefined) return;
|
|
47
|
+
try {
|
|
48
|
+
process.kill(-pid, "SIGKILL");
|
|
49
|
+
} catch {
|
|
50
|
+
// No process group (already gone, or detached failed): fall back.
|
|
51
|
+
child.kill("SIGKILL");
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const onAbort = (): void => {
|
|
55
|
+
killTree();
|
|
56
|
+
};
|
|
57
|
+
if (signal) {
|
|
58
|
+
if (signal.aborted) onAbort();
|
|
59
|
+
else signal.addEventListener("abort", onAbort, { once: true });
|
|
60
|
+
}
|
|
61
|
+
const timer =
|
|
62
|
+
timeoutMs > 0
|
|
63
|
+
? setTimeout(() => {
|
|
64
|
+
timedOut = true;
|
|
65
|
+
killTree();
|
|
66
|
+
}, timeoutMs)
|
|
67
|
+
: undefined;
|
|
68
|
+
|
|
69
|
+
const append = (current: string, chunk: string): string =>
|
|
70
|
+
current.length >= maxBytes ? current : (current + chunk).slice(0, maxBytes);
|
|
71
|
+
|
|
72
|
+
const finish = (code: number): void => {
|
|
73
|
+
if (settled) return;
|
|
74
|
+
settled = true;
|
|
75
|
+
if (timer) clearTimeout(timer);
|
|
76
|
+
signal?.removeEventListener("abort", onAbort);
|
|
77
|
+
resolve({ stdout, stderr, code, timedOut });
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
child.stdout?.setEncoding("utf8");
|
|
81
|
+
child.stderr?.setEncoding("utf8");
|
|
82
|
+
child.stdout?.on("data", (chunk: string) => {
|
|
83
|
+
stdout = append(stdout, chunk);
|
|
84
|
+
onChunk?.("stdout", chunk);
|
|
85
|
+
});
|
|
86
|
+
child.stderr?.on("data", (chunk: string) => {
|
|
87
|
+
stderr = append(stderr, chunk);
|
|
88
|
+
onChunk?.("stderr", chunk);
|
|
89
|
+
});
|
|
90
|
+
child.on("error", (error: Error) => {
|
|
91
|
+
stderr = append(stderr, error.message);
|
|
92
|
+
finish(-1);
|
|
93
|
+
});
|
|
94
|
+
child.on("close", (code: number | null) => {
|
|
95
|
+
finish(code ?? -1);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function lastLines(text: string, count: number): string {
|
|
101
|
+
const lines = text.split(/\r?\n/).filter((line) => line.trim().length > 0);
|
|
102
|
+
return lines.slice(-count).join("\n");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function asRecord(value: unknown): Record<string, unknown> {
|
|
106
|
+
return value !== null && typeof value === "object" ? (value as Record<string, unknown>) : {};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function asString(value: unknown): string | undefined {
|
|
110
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A filesystem-safe timestamp with millisecond precision.
|
|
115
|
+
*
|
|
116
|
+
* Second precision is not enough: relaunching the same app twice within one
|
|
117
|
+
* second produced two captures writing interleaved lines into the same file.
|
|
118
|
+
* `slice(0, 23)` keeps `YYYY-MM-DD_HH-mm-ss-SSS`.
|
|
119
|
+
*/
|
|
120
|
+
export function timestamp(): string {
|
|
121
|
+
return new Date().toISOString().replace(/[:.]/g, "-").replace("T", "_").slice(0, 23);
|
|
122
|
+
}
|