forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-codeowners.js
89 lines (80 loc) · 2.92 KB
/
update-codeowners.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
import * as cp from 'node:child_process';
import * as os from 'node:os';
import { AllPackages, getDefinitelyTyped, parseDefinitions, clean } from '@definitelytyped/definitions-parser';
import { loggerWithErrors } from '@definitelytyped/utils';
import fsExtra from 'fs-extra';
const { writeFile } = fsExtra;
async function main() {
const options = { definitelyTypedPath: '.', progress: false, parseInParallel: true };
const log = loggerWithErrors()[0];
clean();
const dt = await getDefinitelyTyped(options, log);
await parseDefinitions(dt, { nProcesses: os.cpus().length, definitelyTypedPath: '.' }, log);
const allPackages = await AllPackages.read(dt);
const typings = allPackages.allTypings();
const maxPathLen = Math.max(...typings.map(t => t.subDirectoryPath.length));
const entries = mapDefined(typings, t => getEntry(t, maxPathLen));
await writeFile(
[options.definitelyTypedPath, '.github', 'CODEOWNERS'].join('/'),
`${header}\n\n${entries.join('\n')}\n`,
{ encoding: 'utf-8' },
);
}
runSequence([
['git', ['checkout', '.']], // reset any changes
]);
main()
.then(() => {
runSequence([
['git', ['add', '.github/CODEOWNERS']], // Add CODEOWNERS
['git', ['pull']], // Ensure we're up-to-date
['git', ['commit', '-m', `"🤖 Update CODEOWNERS"`]], // Commit all changes
['git', ['push']], // push the branch
]);
console.log(`Pushed new commit.`);
})
.catch(e => {
console.error(e);
process.exit(1);
});
/** @param {[string, string[]][]} tasks */
function runSequence(tasks) {
for (const task of tasks) {
console.log(`${task[0]} ${task[1].join(' ')}`);
const result = cp.spawnSync(task[0], task[1], { timeout: 100000, shell: true, stdio: 'inherit' });
if (result.status !== 0)
throw new Error(`${task[0]} ${task[1].join(' ')} failed: ${result.stderr && result.stderr.toString()}`);
}
}
const header = `# This file is generated.
# Add yourself to the "Definitions by:" list instead.
# See https://github.com/DefinitelyTyped/DefinitelyTyped#definition-owners`;
/**
* @param { { contributors: ReadonlyArray<{githubUsername?: string }>, subDirectoryPath: string} } pkg
* @param {number} maxPathLen
* @return {string | undefined}
*/
function getEntry(pkg, maxPathLen) {
const users = mapDefined(pkg.contributors, c => c.githubUsername);
if (!users.length) {
return undefined;
}
const path = `${pkg.subDirectoryPath}/`.padEnd(maxPathLen + 1);
return `/types/${path} ${users.map(u => `@${u}`).join(' ')}`;
}
/**
* @template T,U
* @param {ReadonlyArray<T>} arr
* @param {(t: T) => U | undefined} mapper
* @return U[]
*/
function mapDefined(arr, mapper) {
const out = [];
for (const a of arr) {
const res = mapper(a);
if (res !== undefined) {
out.push(res);
}
}
return out;
}