-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocess.ts
More file actions
110 lines (99 loc) · 2.58 KB
/
Copy pathprocess.ts
File metadata and controls
110 lines (99 loc) · 2.58 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
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import {
classifyCheckResult,
parseToolIdentity,
type CheckResult,
type ToolIdentity,
} from "./analysis";
const execFileAsync = promisify(execFile);
export interface ProcessResult {
exitCode: number;
stdout: string;
stderr: string;
}
export interface ProcessRunOptions {
timeoutMs?: number;
}
export type ProcessRunner = (
executable: string,
args: string[],
cwd?: string,
options?: ProcessRunOptions,
) => Promise<ProcessResult>;
export interface ToolVersionResult extends ProcessResult {
identity: ToolIdentity | undefined;
}
export interface ClangdCheckResult extends ProcessResult {
output: string;
classification: CheckResult;
}
export async function runProcess(
executable: string,
args: string[],
cwd?: string,
options: ProcessRunOptions = {},
): Promise<ProcessResult> {
try {
const result = await execFileAsync(executable, args, {
cwd,
encoding: "utf8",
maxBuffer: 16 * 1024 * 1024,
timeout: options.timeoutMs,
});
return {
exitCode: 0,
stdout: result.stdout,
stderr: result.stderr,
};
} catch (error) {
const processError = error as NodeJS.ErrnoException & {
stdout?: string;
stderr?: string;
code?: number | string;
};
return {
exitCode: typeof processError.code === "number" ? processError.code : 1,
stdout: processError.stdout ?? "",
stderr: processError.stderr ?? (typeof processError.message === "string" ? processError.message : ""),
};
}
}
export async function runToolVersion(
executable: string,
versionArguments: string[] = ["--version"],
runner: ProcessRunner = runProcess,
): Promise<ToolVersionResult> {
const result = await runner(executable, versionArguments);
return {
...result,
identity: parseToolIdentity(`${result.stdout}\n${result.stderr}`),
};
}
export async function runClangdCheck(
clangdPath: string,
sourceFile: string,
cwd: string,
clangdArguments: string[] = [],
runner: ProcessRunner = runProcess,
): Promise<ClangdCheckResult> {
const result = await runner(
clangdPath,
[`--check=${sourceFile}`, ...clangdArguments],
cwd,
{ timeoutMs: 60_000 },
);
const output = `${result.stdout}\n${result.stderr}`;
return {
...result,
output,
classification: classifyCheckResult(result.exitCode, output),
};
}
export function runMcppBuild(
cwd: string,
executable: string = "mcpp",
runner: ProcessRunner = runProcess,
): Promise<ProcessResult> {
return runner(executable, ["build"], cwd);
}