1
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
2
+
1
3
import { Octokit } from "@octokit/rest" ;
2
- import YAML from "yaml" ;
3
- import fs from "fs" ;
4
+ import * as YAML from "yaml" ;
5
+ import * as fs from "fs" ;
6
+ import { join , extname } from "path" ;
4
7
5
8
function pad ( number : number ) {
6
9
if ( number < 10 ) {
@@ -12,8 +15,13 @@ function pad(number: number) {
12
15
// Modified version of the toISOString polyfill from https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
13
16
// because I couldn't find a function which outputs the dates like it happened on ruby
14
17
// but the diff from the TS output and the ruby output should be as low as possible
15
- // @ts -expect-error
16
- Date . prototype . toRubyString = function ( ) {
18
+ declare global {
19
+ interface Date {
20
+ toRubyString ( ) : string ;
21
+ }
22
+ }
23
+
24
+ Date . prototype . toRubyString = function ( this : Date ) {
17
25
return (
18
26
this . getUTCFullYear ( ) +
19
27
"-" +
@@ -31,59 +39,57 @@ Date.prototype.toRubyString = function () {
31
39
} ;
32
40
33
41
const client = new Octokit ( {
34
- auth : process . env . GITHUB_TOKEN || "" ,
42
+ auth : process . env . GITHUB_TOKEN ?? "" ,
35
43
} ) ;
36
44
37
45
function walk ( directory : string , filepaths : string [ ] = [ ] ) : string [ ] {
38
46
const files = fs . readdirSync ( directory ) ;
39
- for ( let filename of files ) {
40
- const filepath = require ( "path" ) . join ( directory , filename ) ;
47
+ for ( const filename of files ) {
48
+ const filepath = join ( directory , filename ) ;
41
49
if ( fs . statSync ( filepath ) . isDirectory ( ) ) {
42
50
walk ( filepath , filepaths ) ;
43
- } else if ( require ( "path" ) . extname ( filename ) === ".md" ) {
51
+ } else if ( extname ( filename ) === ".md" ) {
44
52
filepaths . push ( filepath ) ;
45
53
}
46
54
}
47
55
return filepaths ;
48
56
}
49
57
50
- const files = walk ( require ( "path" ) . join ( __dirname , ".." , "_apps" ) ) ;
58
+ const files = walk ( join ( __dirname , ".." , "_apps" ) ) ;
51
59
52
- files . forEach ( async ( path ) => {
53
- let data : string = "" ;
60
+ for ( const path of files ) {
61
+ let data = "" ;
54
62
try {
55
63
try {
56
64
// @ts -expect-error
57
- data = fs
58
- . readFileSync ( path )
59
- . toString ( )
60
- . match ( / ^ - - - ( ( (? ! - - - ) .| [ \r \n ] ) * ) [ \r \n ] - - - $ / m) [ 0 ] ;
65
+ data = / ^ - - - ( ( (? ! - - - ) .| [ \r \n ] ) * ) [ \r \n ] - - - $ / m. exec (
66
+ fs . readFileSync ( path ) . toString ( ) ,
67
+ ) [ 0 ] ;
61
68
} catch {
62
69
console . error ( `Error parsing ${ path } ` ) ;
63
70
console . log ( data ) ;
64
71
process . exit ( 1 ) ;
65
72
}
66
- let app = YAML . parse ( data . replaceAll ( "---" , "" ) ) ;
73
+ const app = YAML . parse ( data . replaceAll ( "---" , "" ) ) ;
67
74
console . log ( `Syncing ${ app . title } ` ) ;
68
75
69
- const repoName = app [ " repository" ] . split ( "/" ) ;
76
+ const repoName = app . repository . split ( "/" ) ;
70
77
const repo = await client . rest . repos . get ( {
71
78
owner : repoName [ 0 ] ,
72
79
repo : repoName [ 1 ] ,
73
80
} ) ;
74
81
app . stars = repo . data . stargazers_count ;
75
- //@ts -expect-error
76
82
app . updated = new Date ( repo . data . pushed_at ) . toRubyString ( ) ;
77
83
78
84
if ( app . host ) {
79
85
const statsFromServer = await fetch ( app . host + "/probot/stats" ) ;
80
86
if ( statsFromServer . status === 200 ) {
81
- let stats : Record < string , unknown > = YAML . parse (
82
- await statsFromServer . text ( )
87
+ const stats : Record < string , unknown > = YAML . parse (
88
+ await statsFromServer . text ( ) ,
83
89
) ;
84
90
if ( stats . popular ) {
85
- app . organizations = ( < Array < unknown > > stats . popular ) . map (
86
- ( org : any ) => org . login
91
+ app . organizations = ( stats . popular as unknown [ ] ) . map (
92
+ ( org : any ) => org . login as string ,
87
93
) ;
88
94
}
89
95
}
@@ -93,7 +99,7 @@ files.forEach(async (path) => {
93
99
content = content . replace (
94
100
data ,
95
101
`---
96
- ${ YAML . stringify ( app ) } ---`
102
+ ${ YAML . stringify ( app ) } ---`,
97
103
) ;
98
104
fs . writeFileSync ( path , content ) ;
99
105
console . log ( `Done for ${ app . title } ` ) ;
@@ -107,4 +113,4 @@ ${YAML.stringify(app)}---`
107
113
console . log ( e ) ;
108
114
process . exit ( 1 ) ;
109
115
}
110
- } ) ;
116
+ }
0 commit comments