forked from react-native-community/directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-and-score-data.js
347 lines (288 loc) · 10.1 KB
/
build-and-score-data.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { BlobAccessError, list, put } from '@vercel/blob';
import fetch from 'cross-fetch';
import chunk from 'lodash/chunk.js';
import fs from 'node:fs';
import path from 'node:path';
import { calculateDirectoryScore, calculatePopularityScore } from './calculate-score.js';
import { fetchGithubData, fetchGithubRateLimit, loadGitHubLicenses } from './fetch-github-data.js';
import { fetchNpmData, fetchNpmDataBulk } from './fetch-npm-data.js';
import fetchReadmeImages from './fetch-readme-images.js';
import { fillNpmName, hasMismatchedPackageData, sleep } from './helpers.js';
import debugGithubRepos from '../debug-github-repos.json' assert { type: 'json' };
import githubRepos from '../react-native-libraries.json' assert { type: 'json' };
import { isLaterThan, TimeRange } from '../util/datetime.js';
import { isEmptyOrNull } from '../util/strings.js';
// Uses debug-github-repos.json instead, so we have less repositories to crunch
// each time we run the script
const USE_DEBUG_REPOS = false;
const DATASET = USE_DEBUG_REPOS ? debugGithubRepos : githubRepos;
// Loads the GitHub API results from disk rather than hitting the API each time.
// The first run will hit the API if raw-github-results.json doesn't exist yet.
const LOAD_GITHUB_RESULTS_FROM_DISK = false;
// If script should try to scrape images from GitHub repositories.
const SCRAPE_GH_IMAGES = true;
const DATA_PATH = path.resolve('assets', 'data.json');
const GITHUB_RESULTS_PATH = path.join('scripts', 'raw-github-results.json');
const invalidRepos = [];
const mismatchedRepos = [];
const wantedPackageName = process.argv[2];
const buildAndScoreData = async () => {
console.log('⬇️️ Fetching latest data blob from the store');
const { latestData } = await fetchLatestData();
console.log('📦️ Loading data from GitHub');
let data = await loadRepositoryDataAsync();
data = data.filter(project => {
if (!project.github || isEmptyOrNull(project.github.name)) {
invalidRepos.push(project.githubUrl);
return false;
}
return !isEmptyOrNull(project.github.name);
});
// Detect mismatched package and package.json content
data.forEach(project => {
if (hasMismatchedPackageData(project) && !project.template) {
mismatchedRepos.push(project);
}
});
// Mark libraries as `unmaintained` automatically
data = data.map(project => {
if (!project.unmaintained) {
if (project.github.isArchived) {
project.unmaintained = true;
} else if (isLaterThan(project.github.stats.pushedAt, TimeRange.YEAR * 2)) {
project.unmaintained = true;
}
}
return project;
});
if (SCRAPE_GH_IMAGES) {
console.log('\n📝 Scraping images from README');
await sleep(1000);
data = await Promise.all(data.map(project => fetchReadmeImages(project)));
}
console.log('\n🔖 Determining npm package names');
await sleep(1000);
data = data.map(fillNpmName);
console.log('\n⬇️ Fetching download stats from npm');
await sleep(1000);
// https://github.com/npm/registry/blob/master/docs/download-counts.md#bulk-queries
let bulkList = [];
// Fetch scoped packages data
data = await Promise.all(
data.map(project => {
if (!project.template) {
if (project.npmPkg.startsWith('@')) {
return fetchNpmData(project);
} else {
bulkList.push(project.npmPkg);
return project;
}
}
return project;
})
);
// Assemble and fetch regular packages data in bulk queries
const CHUNK_SIZE = 32;
bulkList = [...Array(Math.ceil(bulkList.length / CHUNK_SIZE))].map(_ =>
bulkList.splice(0, CHUNK_SIZE)
);
const downloadsList = (
await Promise.all(
bulkList.map(async (chunk, index) => {
await sleep(250 * index);
return await fetchNpmDataBulk(chunk);
})
)
).flat();
const downloadsListWeek = (
await Promise.all(
bulkList.map(async (chunk, index) => {
await sleep(250 * index);
return await fetchNpmDataBulk(chunk, 'week');
})
)
).flat();
// Fill npm data from bulk queries
data = data.map(project =>
project.npm
? project
: {
...project,
npm: {
...(downloadsList.find(d => d.name === project.npmPkg)?.npm || {}),
...(downloadsListWeek.find(d => d.name === project.npmPkg)?.npm || {}),
},
}
);
console.log('\n⚛️ Calculating Directory Score');
data = data.map(project => {
try {
return calculateDirectoryScore(project);
} catch (e) {
console.error(`Failed to calculate score for ${project.github.name}`, e.message);
}
});
console.log('\n🧮 Calculating popularity');
data = data.map(project => {
try {
return calculatePopularityScore(project);
} catch (e) {
console.error(`Failed to calculate popularity for ${project.github.name}`, e.message);
console.error(project.githubUrl);
}
});
console.log('\n🏷️ Processing topics');
const topicCounts = {};
data.forEach((project, index, projectList) => {
let topicSearchString = '';
if (project.github.topics) {
project.github.topics.forEach(topic => {
topicSearchString = `${topicSearchString} ${topic}`;
if (!topicCounts[topic]) {
topicCounts[topic] = 1;
return;
}
topicCounts[topic] += 1;
});
}
projectList[index].topicSearchString = topicSearchString.trim();
});
if (invalidRepos.length) {
console.warn(
'\n 🚨 The following repositories were unable to fetch from GitHub, they may need to be removed from react-native-libraries.json:'
);
invalidRepos.forEach(repoUrl => console.warn(`- ${repoUrl}`));
}
if (mismatchedRepos.length) {
console.warn(
`\n 🚨 The following projects repository URLs (${mismatchedRepos.length}) are misaligned with the package name extracted from package.json:`
);
mismatchedRepos.forEach(project =>
console.warn(`- ${project.githubUrl}: ${project.github.name}`)
);
}
console.log('📄️ Preparing data file');
const { libraries, ...rest } = latestData;
let fileContent;
if (wantedPackageName) {
fileContent = JSON.stringify(
{
libraries: libraries.map(library => {
if (library.npmPkg === wantedPackageName) {
return data.find(entry => entry.npmPkg === wantedPackageName);
}
return library;
}),
...rest,
},
null,
2
);
} else {
const existingData = libraries.map(lib => lib.npmPkg);
const newData = data.map(lib => lib.npmPkg);
const missingData = existingData.filter(npmPkg => !newData.includes(npmPkg));
fileContent = JSON.stringify(
{
libraries: [...libraries.filter(lib => missingData.includes(lib.npmPkg)), ...data],
topics: topicCounts,
topicsList: Object.keys(topicCounts).sort(),
},
null,
2
);
}
await uploadToStore(fileContent);
return fs.writeFileSync(DATA_PATH, fileContent);
};
export async function fetchGithubDataThrottled({ data, chunkSize, staggerMs }) {
let results = [];
const chunks = chunk(data, chunkSize);
for (const c of chunks) {
if (chunks.indexOf(c) > 0) {
console.log(`${results.length} of ${data.length} fetched`);
console.log(`Sleeping ${staggerMs}ms`);
await sleep(staggerMs);
}
const partialResult = await Promise.all(c.map(fetchGithubData));
results = [...results, ...partialResult];
if (partialResult.length !== c.length) {
throw new Error(
`Error in fetching data from GitHub... Expected ${c.length} results but only received ${partialResult.length}`
);
}
}
return results;
}
function getDataForFetch(wantedPackage) {
if (wantedPackage) {
const match = DATASET.find(
entry =>
entry?.npmPkg === wantedPackageName || entry.githubUrl.endsWith(`/${wantedPackageName}`)
);
if (!match) {
console.error(`❌ Package named "${wantedPackageName}" does not exist in the dataset!`);
process.exit(1);
}
return [match];
}
return DATASET;
}
async function loadRepositoryDataAsync() {
const data = getDataForFetch(wantedPackageName);
let githubResultsFileExists = false;
try {
fs.statSync(GITHUB_RESULTS_PATH);
githubResultsFileExists = true;
} catch {}
const { apiLimit, apiLimitRemaining, apiLimitCost } = await fetchGithubRateLimit();
// 5000 requests per hour is the authenticated API request rate limit
if (!apiLimit || apiLimit < 5000) {
throw new Error('GitHub API token is invalid or query is not properly configured.');
}
// Error out if not enough remaining
if (apiLimitRemaining < data.length * apiLimitCost) {
throw new Error('Not enough requests left on GitHub API rate limiting to proceed.');
}
console.info(
`${apiLimitRemaining} of ${apiLimit} GitHub API requests remaining for the hour at a cost of ${apiLimitCost} per request`
);
await loadGitHubLicenses();
let result;
if (LOAD_GITHUB_RESULTS_FROM_DISK && githubResultsFileExists) {
result = fs.readFileSync(GITHUB_RESULTS_PATH);
console.log('Loaded Github results from disk, skipped API calls');
} else {
result = await fetchGithubDataThrottled({ data, chunkSize: 25, staggerMs: 5000 });
if (LOAD_GITHUB_RESULTS_FROM_DISK) {
fs.writeFileSync(GITHUB_RESULTS_PATH, JSON.stringify(result, null, 2));
console.log('Saved Github results from disk');
}
}
return result;
}
async function fetchLatestData() {
const { blobs } = await list();
if (blobs?.length > 0) {
const sortedBlobs = blobs.sort((a, b) => new Date(b.uploadedAt) - new Date(a.uploadedAt));
const response = await fetch(sortedBlobs[0].downloadUrl);
return {
latestData: await response.json(),
};
}
return JSON.parse(fs.readFileSync(DATA_PATH).toString());
}
async function uploadToStore(fileContent) {
console.log('⬆️ Uploading data blob to the store');
try {
await put('data.json', fileContent, { access: 'public' });
} catch (error) {
if (error instanceof BlobAccessError) {
console.error('❌ Cannot access the blob store, aborting!');
throw error;
} else {
throw error;
}
}
}
buildAndScoreData();