1- import { writeFileSync } from 'fs ' ;
2- import { join , resolve } from 'path ' ;
3- import { randomBytes } from 'crypto ' ;
1+ import { randomBytes } from 'node:crypto ' ;
2+ import { writeFileSync } from 'node:fs ' ;
3+ import { join , resolve } from 'node:path ' ;
44
5+ // @ts -expect-error Seems the type definitions are wrong here
56import { markdownMagic } from 'markdown-magic' ;
67import { markdownTable } from 'markdown-table' ;
78
8- import pkg from './package.json' with { type : 'json' } ;
99import badgeConfig from './badge.json' with { type : 'json' } ;
10+ import pkg from './package.json' with { type : 'json' } ;
1011
1112const key = pkg . stats ?. user ;
1213const __dirname = resolve ( ) ;
@@ -15,6 +16,16 @@ if (!key) {
1516 throw new Error ( 'Please add `user` in the `stats` field of your package.json' ) ;
1617}
1718
19+ type NPMResponse = {
20+ downloads : Array < {
21+ day : string ;
22+ downloads : number ;
23+ } > ;
24+ end : string ;
25+ package : string ;
26+ start : string ;
27+ } ;
28+
1829const names = [
1930 '@three11/accordion' ,
2031 '@three11/animate-top-offset' ,
@@ -71,7 +82,7 @@ function randomFloat() {
7182 return num / 0xffffffff ;
7283}
7384
74- async function fetchPackageDownloads ( name , maxRetries = 3 ) {
85+ async function fetchPackageDownloads ( name : string , maxRetries = 3 ) {
7586 const today = new Date ( ) ;
7687 const endDate = `${ today . getFullYear ( ) } -${ today . getMonth ( ) + 1 } -${ today . getDate ( ) } ` ;
7788 const startDate = pkg . stats . startDate ;
@@ -95,16 +106,20 @@ async function fetchPackageDownloads(name, maxRetries = 3) {
95106 throw new Error ( 'Non-JSON response' ) ;
96107 }
97108
98- const json = JSON . parse ( text ) ;
109+ const json = JSON . parse ( text ) as NPMResponse ;
99110 const count = json . downloads ? json . downloads . reduce ( ( sum , d ) => sum + d . downloads , 0 ) : 0 ;
100111
101112 console . log ( `✅ [${ name } ] ${ count . toLocaleString ( ) } downloads` ) ;
102113
103114 return count ;
104- } catch ( err ) {
115+ } catch ( err : unknown ) {
105116 const delay = 1000 * Math . pow ( 2 , attempt - 1 ) + randomFloat ( ) * 500 ;
106117
107- console . warn ( `⏳ [${ name } ] Retry ${ attempt } /${ maxRetries } in ${ Math . round ( delay ) } ms (${ err . message } )` ) ;
118+ if ( err instanceof Error ) {
119+ console . warn ( `⏳ [${ name } ] Retry ${ attempt } /${ maxRetries } in ${ Math . round ( delay ) } ms (${ err . message } )` ) ;
120+ } else {
121+ console . warn ( `⏳ [${ name } ] Retry ${ attempt } /${ maxRetries } in ${ Math . round ( delay ) } ms (Unknown error)` ) ;
122+ }
108123
109124 await new Promise ( resolve => setTimeout ( resolve , delay ) ) ;
110125 }
@@ -115,42 +130,38 @@ async function fetchPackageDownloads(name, maxRetries = 3) {
115130 return 0 ;
116131}
117132
118- ( async ( ) => {
119- console . log ( `⏳ Fetching packages downloads data for user ${ key } from NPM. Please wait...` ) ;
133+ console . log ( `⏳ Fetching packages downloads data for user ${ key } from NPM. Please wait...` ) ;
120134
121- const data = [ ] ;
135+ const data = [ ] ;
122136
123- for ( const name of names ) {
124- const count = await fetchPackageDownloads ( name ) ;
125- const delay = 1500 + randomFloat ( ) * 500 ;
137+ for ( const name of names ) {
138+ const count = await fetchPackageDownloads ( name ) ;
139+ const delay = 1500 + randomFloat ( ) * 500 ;
126140
127- data . push ( { name , count } ) ;
141+ data . push ( { count , name } ) ;
128142
129- console . log ( `⏸ Waiting ${ Math . round ( delay ) } ms before next package...` ) ;
143+ console . log ( `⏸ Waiting ${ Math . round ( delay ) } ms before next package...` ) ;
130144
131- await new Promise ( resolve => setTimeout ( resolve , delay ) ) ;
132- }
145+ await new Promise ( resolve => setTimeout ( resolve , delay ) ) ;
146+ }
133147
134- const sum = data . reduce ( ( sum , curr ) => sum + curr . count , 0 ) ;
148+ const sum = data . reduce ( ( sum , curr ) => sum + curr . count , 0 ) ;
135149
136- const sortedStats = data . toSorted ( ( { name : aName } , { name : bName } ) =>
137- aName > bName ? 1 : aName < bName ? - 1 : 0
138- ) ;
150+ const sortedStats = data . toSorted ( ( { name : aName } , { name : bName } ) => ( aName > bName ? 1 : aName < bName ? - 1 : 0 ) ) ;
139151
140- const mappedStats = sortedStats . map ( item => {
141- const { name , count } = item ;
152+ const mappedStats = sortedStats . map ( item => {
153+ const { count , name } = item ;
142154
143- return [ `[${ name } ](https://www.npmjs.com/package/${ name } )` , count ] ;
144- } ) ;
155+ return [ `[${ name } ](https://www.npmjs.com/package/${ name } )` , count . toString ( ) ] ;
156+ } ) ;
145157
146- badgeConfig . message = `${ sum } Downloads` ;
158+ badgeConfig . message = `${ sum } Downloads` ;
147159
148- await writeFileSync ( './badge.json' , JSON . stringify ( badgeConfig , null , 2 ) ) ;
160+ writeFileSync ( './badge.json' , JSON . stringify ( badgeConfig , null , 2 ) ) ;
149161
150- await markdownMagic ( join ( __dirname , 'README.md' ) , {
151- matchWord : 'AUTO-GENERATED-CONTENT' ,
152- transforms : {
153- customTransform : ( ) => markdownTable ( [ [ 'Name' , 'Downloads' ] , ...mappedStats , [ '**Sum**' , `**${ sum } **` ] ] )
154- }
155- } ) ;
156- } ) ( ) ;
162+ markdownMagic ( join ( __dirname , 'README.md' ) , {
163+ matchWord : 'AUTO-GENERATED-CONTENT' ,
164+ transforms : {
165+ customTransform : ( ) => markdownTable ( [ [ 'Name' , 'Downloads' ] , ...mappedStats , [ '**Sum**' , `**${ sum } **` ] ] )
166+ }
167+ } ) ;
0 commit comments