-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
executable file
·205 lines (159 loc) · 4.08 KB
/
bin.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env node
import path from "node:path";
import fs from "node:fs/promises";
import fsSync from "node:fs";
import process from "node:process";
import { parseArgs } from "node:util";
import { execa } from "execa";
import cliProgress from "cli-progress";
import { cacheResponse, readCachedResponse, clearCache } from "./cache.js";
import {
IN_MONOREPO,
getMonorepoPackage,
getAllPackageJSONs,
} from "./monorepo.js";
import { getPackageInfo } from "./npm.js";
import { readPackageJson, getDeclaredDeps } from "./package-json.js";
import { printSummary } from "./output.js";
import {
SEEN_DEPS,
MAINTAINERS,
WHO_MAINTAINS,
NOT_FOUND,
NOT_AUTHORIZED,
} from "./info-buckets.js";
const CWD = process.cwd();
const args = parseArgs({
options: {
recursive: {
type: "boolean",
short: "r",
default: false,
},
force: {
type: "boolean",
default: false,
},
verbose: {
type: "boolean",
short: "v",
default: false,
},
help: {
type: "boolean",
short: "h",
default: false,
},
},
});
if (args.values.help) {
console.log(`
Usage:
npx dependency-maintainers
--recursive, -r In a monorepo, find the montainers of every (package in the monorepo)'s (dev)dependencies
npx dependency-maintainers --recursive
--verbose, -v Print extra logging to stdout
npx dependency-maintainers --verbose
--force Force a cache refresh
npx dependency-maintainers --force
--help, -h show this message
npx dependency-maintainers --help
`);
process.exit(0);
}
if (args.values.force) {
await clearCache();
}
const BATCH_SIZE = 40;
const IS_VERBOSE = args.values.verbose;
function updateMaintainers(npmInfo) {
/**
* Array:
* username <email>
* username2 <email2>
*/
let { maintainers, _npmUser } = npmInfo;
let users = maintainers?.map((maintainer) => maintainer.split(" ")[0]) ?? [];
users.map(incrementUser);
if (!maintainers) {
WHO_MAINTAINS.add(npmInfo.name);
}
}
function incrementUser(user) {
let count = MAINTAINERS.get(user) ?? 0;
MAINTAINERS.set(user, count + 1);
}
const QUEUE = [];
const HAS_INFO = new Set();
let seen = 0;
let total = 0;
const progress = new cliProgress.SingleBar(
{},
cliProgress.Presets.shades_classic,
);
function showProgress() {
progress.start(total, seen);
}
function updateProgress() {
if (IS_VERBOSE) return;
progress.update(seen);
}
async function traverseGraph() {
async function processDep(depName) {
if (IS_VERBOSE) {
console.debug(`Processed ${SEEN_DEPS.size}. Processing ${depName}`);
} else {
showProgress();
}
let shouldSkipMaintainers = IN_MONOREPO.has(depName);
let info = getMonorepoPackage(depName) || (await getPackageInfo(depName));
seen++;
updateProgress();
if (!info) {
return;
}
HAS_INFO.add(info);
if (!shouldSkipMaintainers) {
updateMaintainers(info);
}
let subDeps = await getDeclaredDeps(info);
QUEUE.push(...subDeps);
}
async function prepareBatch(batch) {
await Promise.all(
batch.map((depName) => {
if (SEEN_DEPS.has(depName)) return;
total++;
SEEN_DEPS.add(depName);
return processDep(depName);
}),
);
}
while (QUEUE.length > 0) {
let batch = [];
for (let i = 0; i < Math.min(QUEUE.length, BATCH_SIZE); i++) {
let depName = QUEUE.pop();
if (SEEN_DEPS.has(depName)) {
i--;
continue;
}
batch.push(depName);
}
await prepareBatch(batch);
}
}
let rootDeps = [];
if (args.values.recursive) {
let all = getAllPackageJSONs();
console.log(`Getting maintainers for all ${all.length} packages...`);
for (let rootJson of all) {
rootDeps = await getDeclaredDeps(rootJson, true);
}
} else {
let rootJson = await readPackageJson();
rootDeps = await getDeclaredDeps(rootJson, true);
}
QUEUE.push(...rootDeps);
await traverseGraph();
progress.stop();
printSummary();