@@ -4,11 +4,21 @@ import fs from 'node:fs';
4
4
import path from 'node:path' ;
5
5
import process from 'node:process' ;
6
6
7
+ let skipGithubStars = false ;
8
+
7
9
const start = performance . now ( ) ;
8
10
console . log ( '[sync-packages] start' ) ;
9
11
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
+ } [ ] = [ ] ;
12
22
13
23
const packages = [
14
24
...PACKAGES_META . FEATURED . flatMap ( ( pkg ) => pkg . packages ) ,
@@ -22,13 +32,13 @@ for (const pkg of packages) {
22
32
const cleanPkg = pkg . replace ( '@' , '' ) . replace ( '/' , '-' ) ;
23
33
const jsonPath = path . join ( registryFolder , `${ cleanPkg } .json` ) ;
24
34
if ( ! fs . existsSync ( jsonPath ) ) {
25
- console . warn ( ` "${ pkg } " -> "${ jsonPath } " not found, we will create it!` ) ;
26
35
const p = await fetchData ( pkg ) ;
27
36
writeButPretty ( jsonPath , JSON . stringify ( p , null , 2 ) ) ;
37
+ logsAtTheEnd . push ( { type : 'new_json_file' , pkg, extra : `created -> ${ jsonPath } ` } ) ;
28
38
}
29
39
}
30
40
31
- // PART 2: check if all json files are needed
41
+ // PART 2: delete unused json files
32
42
let registryJsonFiles = fs . readdirSync ( registryFolder ) ;
33
43
const jsonUsed : string [ ] = [ ] ;
34
44
for ( const pkg of packages ) {
@@ -41,21 +51,18 @@ for (const pkg of packages) {
41
51
}
42
52
const jsonNotNeeded = registryJsonFiles . filter ( ( pkg ) => ! jsonUsed . includes ( pkg ) ) ;
43
53
if ( 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
-
49
54
// 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
+ }
53
60
54
61
theEnd ( 1 ) ;
55
62
}
56
63
57
64
// PART 3: refresh data
58
- registryJsonFiles = fs . readdirSync ( registryFolder ) ; //.slice(0, 1 );
65
+ registryJsonFiles = fs . readdirSync ( registryFolder ) ; //.slice(0, 20 );
59
66
60
67
const batch = 10 ;
61
68
for ( let i = 0 ; i < registryJsonFiles . length ; i += batch ) {
@@ -79,11 +86,21 @@ function theEnd(val: number) {
79
86
msg . push ( `took: ${ ( performance . now ( ) - start ) . toFixed ( 0 ) } ms` ) ;
80
87
console . log ( msg . join ( ' ' ) ) ;
81
88
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
+ ) ;
84
100
}
85
101
process . exit ( val ) ;
86
102
}
103
+
87
104
async function fetchData ( pkg : string ) {
88
105
const [ npmInfo , npmDlInfo ] = await Promise . all ( [
89
106
fetch ( `https://registry.npmjs.org/${ pkg } ` ) . then ( ( r ) => r . json ( ) ) ,
@@ -96,7 +113,7 @@ async function fetchData(pkg: string) {
96
113
const repo_url = raw_repo_url ?. replace ( / ^ g i t \+ / , '' ) . replace ( / \. g i t $ / , '' ) ;
97
114
if ( ! repo_url ) {
98
115
// 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` } ) ;
100
117
}
101
118
const git_org = repo_url ?. split ( '/' ) [ 3 ] ;
102
119
const git_repo = repo_url ?. split ( '/' ) [ 4 ] ;
@@ -122,7 +139,7 @@ async function fetchData(pkg: string) {
122
139
123
140
return {
124
141
name : pkg ,
125
- // description, // let's not overwrite the description for now.
142
+ description,
126
143
repo_url,
127
144
authors,
128
145
homepage,
@@ -150,7 +167,27 @@ async function refreshJson(fullPath: string) {
150
167
}
151
168
}
152
169
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 ) ) ;
154
191
}
155
192
156
193
function writeButPretty ( path : string , data : any ) {
0 commit comments