-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathexec-command.ts
More file actions
65 lines (55 loc) · 1.72 KB
/
exec-command.ts
File metadata and controls
65 lines (55 loc) · 1.72 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
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import { getConfig as cmGetConfig } from '../config-manager.js';
type ExecCommandOptions = {
encoding: string;
cwd?: string;
};
// Runs the given command in a shell.
export async function execCommand(command: string): Promise<string> {
const cmdConfig: ExecCommandOptions = { encoding: 'utf8' };
const processCwd = cmGetConfig('processCwd');
if (processCwd) cmdConfig.cwd = processCwd;
const execAsync = promisify(exec);
const { stderr, stdout } = await execAsync(command, cmdConfig);
if (stderr) {
throw new Error(`Git mob core execCommand: "${command}" ${stderr.trim()}`);
}
return stdout.trim();
}
export async function getConfig(key: string) {
try {
return await execCommand(`git config --get ${key}`);
} catch {
return undefined;
}
}
export async function getAllConfig(key: string) {
try {
return await execCommand(`git config --get-all ${key}`);
} catch {
return undefined;
}
}
export async function getRegexpConfig(key: string) {
try {
return await execCommand(`git config --get-regexp ${key}`);
} catch {
return undefined;
}
}
export async function setConfig(key: string, value: string) {
try {
await execCommand(`git config ${key} "${value}"`);
} catch {
const message = `Option ${key} has multiple values. Cannot overwrite multiple values for option ${key} with a single value.`;
throw new Error(`Git mob core setConfig: ${message}`);
}
}
export async function getRepoAuthors(authorFilter?: string) {
let repoAuthorQuery = 'git shortlog -seni HEAD';
if (authorFilter) {
repoAuthorQuery += ` --author="${authorFilter}"`;
}
return execCommand(repoAuthorQuery);
}