-
Notifications
You must be signed in to change notification settings - Fork 53
/
exec.js
48 lines (38 loc) · 1.1 KB
/
exec.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
46
47
48
'use strict'
const process = require('process')
const execa = require('execa')
const moize = require('moize')
const pathExists = require('path-exists')
// Fires the `git` binary. Memoized.
const mGit = function (args, cwd) {
const cwdA = safeGetCwd(cwd)
// The child process `error.message` includes stderr and stdout output which most of the times contains duplicate
// information. We rely on `error.shortMessage` instead.
try {
const { stdout } = execa.sync('git', args, { cwd: cwdA })
return stdout
} catch (error) {
error.message = error.shortMessage
throw error
}
}
// eslint-disable-next-line no-magic-numbers
const git = moize(mGit, { isDeepEqual: true, maxSize: 1e3 })
const safeGetCwd = function (cwd) {
const cwdA = getCwdValue(cwd)
if (!pathExists.sync(cwdA)) {
throw new Error(`Current directory does not exist: ${cwdA}`)
}
return cwdA
}
const getCwdValue = function (cwd) {
if (cwd !== undefined) {
return cwd
}
try {
return process.cwd()
} catch (error) {
throw new Error('Current directory does not exist')
}
}
module.exports = { git }