-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathutils.js
127 lines (110 loc) · 3.57 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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 || null;
};
/**
* Extracts the releasers' information from the provided markdown txt.
* Each releaser's information includes their name, email, and GPG key.
*
* @param {string} txt - The README content.
* @returns {Array<Array<string>>} An array of releaser information arrays.
* Each sub-array contains the name, email,
* and GPG key of a releaser.
*/
export function extractReleasersFromReadme(txt) {
const regex = /\* \*\*(.*)\*\*.*<<(.*)>>\n.*`(.*)`/gm;
let match;
const result = [];
while ((match = regex.exec(txt)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (match.index === regex.lastIndex) {
regex.lastIndex++;
}
const [, name, email, key] = match;
result.push([name, email, key]);
}
return result;
}
export function checkReleaserDiscrepancies(member, extractedMembers) {
let releaseKey, extractedMember;
member.keys.forEach(key => {
extractedMembers.filter(eMember => {
if (eMember[2].includes(key.key_id)) {
extractedMember = eMember;
releaseKey = key;
}
});
});
if (!extractedMember || !releaseKey) {
console.error(`The releaser ${member.name} (${member.login}) is not listed or any of the current profile GPG keys are listed in README.md`);
return;
}
if (!releaseKey.emails.some(({ email }) => email === extractedMember[1])) {
console.error(`The releaser ${member.name} (${member.login}) has a key (${releaseKey.key_id}) that is not associated with their email address ${extractedMember[1]} in the README.md`);
}
if (!releaseKey.can_sign) {
console.error(`The releaser ${member.name} (${member.login}) has a key (${releaseKey.key_id}) that cannot sign`);
}
if (!releaseKey.can_certify) {
console.error(`The releaser ${member.name} (${member.login}) has a key (${releaseKey.key_id}) that cannot certify`);
}
if (!releaseKey.expires_at) {
console.error(`The releaser ${member.name} (${member.login}) has a key (${releaseKey.key_id}) that cannot expire`);
}
if (releaseKey.revoked) {
console.error(`The releaser ${member.name} (${member.login}) has a key (${releaseKey.key_id}) that has been revoked`);
}
}