-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
139 lines (132 loc) · 3.9 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
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
const fetch = require('node-fetch');
const { fromUrl } = require('hosted-git-info');
const { argv } = require('yargs').options({
labels: {
description:
'Limit issues to those matching a comma-separated list of labels',
type: 'string',
},
depth: {
description:
'Limit issues to those from packages <depth> levels deep in the dependency tree',
type: 'number',
},
format: {
description: 'output format',
type: 'string',
choices: ['console', 'html', 'md'],
default: 'console',
},
});
const { exec } = require('child_process');
const ISSUE_COUNT = 15;
const MAX_CONCURRENT_REQUESTS = 10;
function chunksOfSize(arr, size) {
return arr.reduce((chunks, el, i) => {
const chunkIdx = Math.floor(i / size);
if (!chunks[chunkIdx]) {
chunks[chunkIdx] = [];
}
chunks[chunkIdx].push(el);
return chunks;
}, Array(Math.ceil(arr.length / size)));
}
function buildUrl(info, labels) {
const url = `https://api.github.com/repos/${info.user}/${info.project}/issues?per_page=${ISSUE_COUNT}`;
// Allow passing a comma-separated lists of labels
if (typeof labels === 'string') {
return `${url}&labels=${encodeURIComponent(
labels
.split(',')
.map((label) => label.trim())
.join(','),
)}`;
}
return url;
}
async function* loadIssues(paths) {
let rateLimitExceeded = false;
for (const chunk of chunksOfSize(paths, MAX_CONCURRENT_REQUESTS)) {
const promises = chunk.map(async (path) => {
const json = require(path);
let { name, repository } = json;
// Not all package.json files include a `name`, fall back to `path`
name = name || path;
if (!repository) {
return { name, rateLimitExceeded };
}
const info = fromUrl(repository.url || repository);
if (!info || info.type !== 'github' || rateLimitExceeded) {
return { name, rateLimitExceeded, info };
}
const githubUrl = buildUrl(info, argv.labels);
const res = await fetch(
githubUrl,
process.env.GITHUB_TOKEN && {
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
},
},
);
if (!res.ok) {
if (
res.status === 403 &&
res.headers.get('X-RateLimit-Remaining') === '0'
) {
rateLimitExceeded = true;
}
return { name, rateLimitExceeded, info };
}
const issuesAndPRs = await res.json();
const issues = issuesAndPRs.filter(
(issue) => !issue.hasOwnProperty('pull_request'),
);
const hasAdditionalIssues = !!res.headers.get('link');
return { name, rateLimitExceeded, info, issues, hasAdditionalIssues };
});
for (const promise of promises) {
yield await promise;
}
}
}
async function locatePackages(depth) {
const depthParam = typeof depth === 'number' ? `--depth=${depth}` : '';
return await new Promise((resolve, reject) => {
exec(`npm ls --parseable ${depthParam}`, (err, stdout) => {
const packages = stdout
.trim()
.split('\n')
.map((path) => `${path}/package.json`);
if (packages.length) {
resolve(packages);
} else {
reject(new Error('Unable to detect dependencies'));
}
});
});
}
function getRenderer(format) {
switch (format) {
case 'md':
return require('./render/markdown.js');
case 'html':
return require('./render/html.js');
case 'console':
default:
return require('./render/console.js');
}
}
(async function main() {
const renderer = getRenderer(argv.format);
const packageJsonLocations = await locatePackages(argv.depth);
renderer.renderHeader(packageJsonLocations);
for await (const p of loadIssues(packageJsonLocations)) {
if (p.rateLimitExceeded) {
renderer.renderRateLimitExceeded();
break;
} else {
renderer.renderPackage(p);
}
}
renderer.renderFooter();
})();