@@ -4,11 +4,21 @@ import fs from 'node:fs';
44import path from 'node:path' ;
55import process from 'node:process' ;
66
7+ let skipGithubStars = false ;
8+
79const start = performance . now ( ) ;
810console . log ( '[sync-packages] start' ) ;
911
10- let skipGithubStars = false ;
11- let logsAtTheEnd : String [ ] = [ ] ;
12+ let logsAtTheEnd : {
13+ type :
14+ | 'no_repo_url'
15+ | 'low_downloads'
16+ | 'low_github_stars'
17+ | 'new_json_file'
18+ | 'deleted_unused_json_file' ;
19+ pkg : string ;
20+ extra : string ;
21+ } [ ] = [ ] ;
1222
1323const packages = [
1424 ...PACKAGES_META . FEATURED . flatMap ( ( pkg ) => pkg . packages ) ,
@@ -22,13 +32,13 @@ for (const pkg of packages) {
2232 const cleanPkg = pkg . replace ( '@' , '' ) . replace ( '/' , '-' ) ;
2333 const jsonPath = path . join ( registryFolder , `${ cleanPkg } .json` ) ;
2434 if ( ! fs . existsSync ( jsonPath ) ) {
25- console . warn ( ` "${ pkg } " -> "${ jsonPath } " not found, we will create it!` ) ;
2635 const p = await fetchData ( pkg ) ;
2736 writeButPretty ( jsonPath , JSON . stringify ( p , null , 2 ) ) ;
37+ logsAtTheEnd . push ( { type : 'new_json_file' , pkg, extra : `created -> ${ jsonPath } ` } ) ;
2838 }
2939}
3040
31- // PART 2: check if all json files are needed
41+ // PART 2: delete unused json files
3242let registryJsonFiles = fs . readdirSync ( registryFolder ) ;
3343const jsonUsed : string [ ] = [ ] ;
3444for ( const pkg of packages ) {
@@ -41,21 +51,18 @@ for (const pkg of packages) {
4151}
4252const jsonNotNeeded = registryJsonFiles . filter ( ( pkg ) => ! jsonUsed . includes ( pkg ) ) ;
4353if ( jsonNotNeeded . length > 0 ) {
44- console . error ( jsonNotNeeded . join ( '\n' ) ) ;
45- console . error (
46- `ERROR: ${ jsonNotNeeded . length } json files are not needed as they are not in the packages array`
47- ) ;
48-
4954 // delete json files
50- // for (const pkg of jsonNotNeeded) {
51- // fs.unlinkSync(path.join(registryFolder, pkg));
52- // }
55+ for ( const pkg of jsonNotNeeded ) {
56+ const jsonPath = path . join ( registryFolder , pkg ) ;
57+ fs . unlinkSync ( jsonPath ) ;
58+ logsAtTheEnd . push ( { type : 'deleted_unused_json_file' , pkg, extra : `deleted -> ${ jsonPath } ` } ) ;
59+ }
5360
5461 theEnd ( 1 ) ;
5562}
5663
5764// PART 3: refresh data
58- registryJsonFiles = fs . readdirSync ( registryFolder ) ; //.slice(0, 1 );
65+ registryJsonFiles = fs . readdirSync ( registryFolder ) ; //.slice(0, 20 );
5966
6067const batch = 10 ;
6168for ( let i = 0 ; i < registryJsonFiles . length ; i += batch ) {
@@ -79,11 +86,21 @@ function theEnd(val: number) {
7986 msg . push ( `took: ${ ( performance . now ( ) - start ) . toFixed ( 0 ) } ms` ) ;
8087 console . log ( msg . join ( ' ' ) ) ;
8188 if ( logsAtTheEnd . length > 0 ) {
82- console . log ( '[sync-packages] Report:' ) ;
83- console . log ( ` - ${ logsAtTheEnd . join ( '\n - ' ) } ` ) ;
89+ console . log ( '[sync-packages] report:' ) ;
90+ const typePrints : Record < ( typeof logsAtTheEnd ) [ number ] [ 'type' ] , string > = {
91+ no_repo_url : 'No GitHub URL' ,
92+ low_downloads : 'Low Downloads' ,
93+ low_github_stars : 'Low Stars' ,
94+ new_json_file : 'NEW JSON' ,
95+ deleted_unused_json_file : 'DEL JSON'
96+ } ;
97+ console . log (
98+ ` - ${ logsAtTheEnd . map ( ( l ) => `${ typePrints [ l . type ] . padEnd ( 15 ) } | ${ l . pkg . padEnd ( 35 ) } | ${ l . extra } ` ) . join ( '\n - ' ) } `
99+ ) ;
84100 }
85101 process . exit ( val ) ;
86102}
103+
87104async function fetchData ( pkg : string ) {
88105 const [ npmInfo , npmDlInfo ] = await Promise . all ( [
89106 fetch ( `https://registry.npmjs.org/${ pkg } ` ) . then ( ( r ) => r . json ( ) ) ,
@@ -96,7 +113,7 @@ async function fetchData(pkg: string) {
96113 const repo_url = raw_repo_url ?. replace ( / ^ g i t \+ / , '' ) . replace ( / \. g i t $ / , '' ) ;
97114 if ( ! repo_url ) {
98115 // console.error(`repo_url not found for ${pkg}`);
99- logsAtTheEnd . push ( `repo_url not found for ${ pkg } ` ) ;
116+ logsAtTheEnd . push ( { type : 'no_repo_url' , pkg , extra : ` not found` } ) ;
100117 }
101118 const git_org = repo_url ?. split ( '/' ) [ 3 ] ;
102119 const git_repo = repo_url ?. split ( '/' ) [ 4 ] ;
@@ -122,7 +139,7 @@ async function fetchData(pkg: string) {
122139
123140 return {
124141 name : pkg ,
125- // description, // let's not overwrite the description for now.
142+ description,
126143 repo_url,
127144 authors,
128145 homepage,
@@ -150,7 +167,27 @@ async function refreshJson(fullPath: string) {
150167 }
151168 }
152169
153- writeButPretty ( fullPath , JSON . stringify ( { ...currentJson , ...newData } , null , 2 ) ) ;
170+ // don't overwrite the description if it exists
171+ if ( currentJson . description ) {
172+ delete newData . description ;
173+ }
174+
175+ const data = { ...currentJson , ...newData } ;
176+
177+ // Some stats infos to log
178+ if ( data . downloads && data . downloads < 255 ) {
179+ logsAtTheEnd . push ( { type : 'low_downloads' , pkg : data . name , extra : `${ data . downloads } ` } ) ;
180+ }
181+
182+ if ( data . github_stars && data . github_stars < 42 ) {
183+ logsAtTheEnd . push ( {
184+ type : 'low_github_stars' ,
185+ pkg : data . name ,
186+ extra : `${ data . github_stars } `
187+ } ) ;
188+ }
189+
190+ writeButPretty ( fullPath , JSON . stringify ( data , null , 2 ) ) ;
154191}
155192
156193function writeButPretty ( path : string , data : any ) {
0 commit comments