-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess.ts
63 lines (58 loc) · 1.55 KB
/
process.ts
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
const decoder = new TextDecoder();
export type ExecuteOptions = Omit<
Deno.CommandOptions,
"args" | "stdin" | "stdout" | "stderr"
>;
export function execute(
args: string[],
options: ExecuteOptions = {},
): Promise<string> {
return _internals.execute(args, options);
}
async function _execute(
args: string[],
options: ExecuteOptions = {},
): Promise<string> {
// --literal-pathspecs
// Treat pathspecs literally (i.e. no globbing, no pathspec magic).
// This is equivalent to setting the GIT_LITERAL_PATHSPECS environment
// variable to 1.
//
// --no-optional-locks
// Do not perform optional operations that require locks. This is
// equivalent to setting the GIT_OPTIONAL_LOCKS to 0.
const cmdArgs = [
"--no-pager",
"--literal-pathspecs",
"--no-optional-locks",
];
const command = new Deno.Command("git", {
args: [...cmdArgs, ...args],
stdin: "null",
stdout: "piped",
stderr: "piped",
...options,
});
const { code, success, stdout, stderr } = await command.output();
const stdoutStr = decoder.decode(stdout);
const stderrStr = decoder.decode(stderr);
if (!success) {
throw new ExecuteError(args, code, stdoutStr, stderrStr);
}
return stdoutStr;
}
export class ExecuteError extends Error {
constructor(
public args: string[],
public code: number,
public stdout: string,
public stderr: string,
) {
super(`[${code}]: ${stderr}`);
this.name = this.constructor.name;
}
}
// For internal stub testing
export const _internals = {
execute: _execute,
};