@@ -3,16 +3,20 @@ import { resolve } from 'path';
3
3
4
4
const sleep = ms => new Promise ( r => setTimeout ( r , ms ) ) ;
5
5
6
- const getLoginsByType = async type => {
6
+ const getLoginsByType = async ( type , cachedLogins ) => {
7
7
// const data = JSON.parse(await readFile(`${type}.json`, 'utf-8'));
8
8
const data = JSON . parse ( await readFile ( resolve ( '..' , 'public' , 'resources' , `${ type } .json` ) , 'utf-8' ) ) ;
9
9
const ids = new Set (
10
10
Object . values ( data )
11
11
. map ( _ => _ . contributors )
12
12
. flat ( )
13
13
) ;
14
- const ret = { } ;
14
+ const jsonType = type === 'real_world' ? 'realWorld' : type ; // real_world -> realWorld
15
+ const ret = cachedLogins [ jsonType ] ;
15
16
for ( const id of ids ) {
17
+ if ( id in cachedLogins [ jsonType ] ) {
18
+ continue ;
19
+ }
16
20
const rep = await ( await fetch ( `https://api.github.com/user/${ id } ` ) ) . json ( ) ;
17
21
ret [ id ] = rep . login ;
18
22
console . log ( `login for id: ${ id } is ${ ret [ id ] } ` ) ;
@@ -21,18 +25,18 @@ const getLoginsByType = async type => {
21
25
return ret ;
22
26
} ;
23
27
24
- export const getLogins = async ( ) => {
28
+ export const makeLogins = async ( ) => {
29
+ const loginPath = resolve ( '..' , 'public' , 'resources' , 'logins.json' ) ;
30
+ const cachedLogins = JSON . parse ( await readFile ( loginPath , 'utf-8' ) ) ;
31
+
25
32
const logins = {
26
- realWorld : await getLoginsByType ( 'real_world' ) ,
27
- fantasy : await getLoginsByType ( 'fantasy' ) ,
33
+ realWorld : await getLoginsByType ( 'real_world' , cachedLogins ) ,
34
+ fantasy : await getLoginsByType ( 'fantasy' , cachedLogins ) ,
28
35
} ;
29
36
30
- // await writeFile('logins.json', JSON.stringify(logins, null, 4), {
31
- // encoding: 'utf-8',
32
- // });
33
- await writeFile ( resolve ( '..' , 'public' , 'resources' , 'logins.json' ) , JSON . stringify ( logins , null , 4 ) , {
37
+ await writeFile ( loginPath , JSON . stringify ( logins , null , 4 ) , {
34
38
encoding : 'utf-8' ,
35
39
} ) ;
36
40
} ;
37
41
38
- await getLogins ( ) ;
42
+ await makeLogins ( ) ;
0 commit comments