-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
45 lines (42 loc) · 1.28 KB
/
index.js
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
const util = require("util");
const execFile = util.promisify(require("child_process").execFile);
const defaultCallback = (stdout) => stdout;
const defaultOptions = {};
const isString = (_) => typeof _ === "string";
const isObject = (_) => typeof _ === "object";
const isFunction = (_) => typeof _ === "function";
module.exports = function (commandOrArgs, optionsOrCallback, callbackMaybe) {
const callback = [
optionsOrCallback,
callbackMaybe,
defaultCallback,
].find(isFunction);
const options = [
optionsOrCallback,
callbackMaybe,
defaultOptions,
].find(isObject);
// Strip `git ` from the beginning since it's reduntant
if (isString(commandOrArgs) && commandOrArgs.startsWith("git ")) {
commandOrArgs = commandOrArgs.substring(4);
}
const execBinary = options.gitExec || "git";
const execOptions = {
cwd: options.cwd,
windowsHide: true,
};
const execArguments = isString(commandOrArgs)
? commandOrArgs.split(" ")
: commandOrArgs;
return execFile(execBinary, execArguments, execOptions).then(
({stdout}) => callback(stdout, null),
(error) => {
if (callback.length === 1) {
throw error;
} else {
// The callback is interested in the error, try to catch it.
return callback("", error);
}
},
);
};