-
Notifications
You must be signed in to change notification settings - Fork 5
/
generateData.mjs
80 lines (71 loc) · 2.72 KB
/
generateData.mjs
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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import yir from './src/yir.js';
import fetch from 'node-fetch';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename)
const ROOT = `${__dirname}/src/assets/data/shortcut`;
global.fetch = fetch;
global.localStorage = { setItem: () => {} };
/**
* @param {number} year
* @param {string} project
*/
function generateSummaryForProjectAndYear( year, project ) {
}
const currentYear = (new Date()).getFullYear();
/**
* @param {number} year
* @param {string} project
*/
function generateForProjectAndYear( year, project ) {
const BASE_DIR = `${ROOT}/${year}/${project}`;
const index = fs.readFileSync( `${BASE_DIR}/index.json` );
const usernames = JSON.parse( index );
usernames.forEach( ( /** @type string */ username ) => {
console.log(`Generating /${year}/${project}/${username}`)
if ( currentYear === year ) {
const userPath = `${BASE_DIR}/${ encodeURIComponent( username.replace(/ /g, '_') ) }.json`;
const localResults = fs.existsSync( userPath ) ?
JSON.parse( fs.readFileSync( userPath ).toString() ) : [];
// To avoid JavaScript heap out of memory bug, limit to only the first 1000 edits of the year
if ( localResults.length < 1000 ) {
// Expand the data we have on this user.
yir.resumeContributionsFetch( username, year, project, localResults, 5 )
.then( ( contribs ) => {
// delete unused fields
contribs.forEach( ( contribution ) => {
delete contribution.user;
delete contribution.userid;
} );
fs.writeFileSync( userPath, JSON.stringify( contribs ) );
})
}
} else {
const userPath = `${BASE_DIR}/${ encodeURIComponent( username.replace(/ /g, '_') ) }.summary.json`;
yir( username, year, project ).then( ( result ) => {
fs.writeFileSync( userPath, JSON.stringify( result ) );
} );
}
} );
}
/**
* @param {number} year
*/
function generateYear( year ) {
const files = fs.readdirSync( `${ROOT}/${year}` );
files.forEach( ( file, index) => {
const stats = fs.statSync( `${ROOT}/${year}/${file}` );
if ( stats.isDirectory() ) {
generateForProjectAndYear( year, file );
}
});
}
const files = fs.readdirSync( ROOT );
files.forEach( ( file ) => {
const stats = fs.statSync( `${ROOT}/${file}` );
if ( stats.isDirectory() ) {
generateYear( parseInt( file, 10 ) );
}
});