-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (45 loc) · 1.23 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
46
47
48
49
50
51
52
53
54
55
56
'use strict';
var child = require('child_process');
var isObj = require('is-obj');
var execa = require('execa');
var multimatch = require('multimatch');
var TEN_MEBIBYTE = 1024 * 1024 * 10;
var porcelainRegex = /([AMRDC]|\?\?)\s/g;
function filterIgnored(stdout, options) {
var parsed = stdout.replace(porcelainRegex, '').trim().split('\n');
if (options.files) {
parsed = multimatch(parsed, ['*'].concat(options.files));
}
return parsed;
}
function normalizeArgs(fn) {
return function (dir, options) {
if (isObj(dir)) {
options = dir;
dir = null;
}
return fn(dir, options);
};
}
module.exports = normalizeArgs(function (dir, options) {
return execa('git', ['status', '--porcelain'], {
cwd: dir || process.cwd(),
preferLocal: false
}).then(function (result) {
if (options) {
return filterIgnored(result.stdout, options).length === 0;
}
return !(result.stdout && result.stdout.trim());
});
});
module.exports.sync = normalizeArgs(function (dir, options) {
var stdout = child.execFileSync('git', ['status', '--porcelain'], {
cwd: dir || process.cwd(),
encoding: 'utf8',
maxBuffer: TEN_MEBIBYTE
});
if (options) {
return filterIgnored(stdout, options).length === 0;
}
return !(stdout && stdout.trim());
});