-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathutils.js
68 lines (59 loc) · 1.45 KB
/
utils.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
57
58
59
60
61
62
63
64
65
66
67
68
import { platform } from 'node:os';
import which from 'which';
import { forceRunAsync } from './run.js';
export function ascending(a, b) {
if (a === b) return 0;
return a < b ? -1 : 1;
};
export function descending(a, b) {
if (a === b) return 0;
return a > b ? -1 : 1;
};
export function flatten(arr) {
let result = [];
for (const item of arr) {
if (Array.isArray(item)) {
result = result.concat(flatten(item));
} else {
result.push(item);
}
}
return result;
}
export function shortSha(sha) {
return sha.slice(0, 12);
};
let isGhAvailableCache;
export function isGhAvailable() {
if (isGhAvailableCache === undefined) {
isGhAvailableCache = which.sync('gh', { nothrow: true }) !== null;
}
return isGhAvailableCache;
};
/**
* Returns the user's preferred text editor command.
* @param {object} [options]
* @param {boolean} [options.git] - Whether to try the GIT_EDITOR environment
* variable or `git config`.
* @returns {Promise<string|null>}
*/
export async function getEditor(options = {}) {
const {
git = false
} = options;
if (git) {
if (process.env.GIT_EDITOR) {
return process.env.GIT_EDITOR;
}
const out = await forceRunAsync(
'git',
['config', 'core.editor'],
{ captureStdout: 'lines' }
);
if (out && out[0]) {
return out[0];
}
}
return process.env.VISUAL || process.env.EDITOR ||
(platform() === 'win32' ? 'notepad.exe' : 'vi');
};