@@ -4,26 +4,32 @@ import path from 'path'
4
4
5
5
import Parser from 'rss-parser'
6
6
7
+ import type { ChangelogItem } from '@/types'
8
+
7
9
const CHANGELOG_CACHE_FILE_PATH = process . env . CHANGELOG_CACHE_FILE_PATH
8
10
// This is useful to set when doing things like sync search.
9
11
const CHANGELOG_DISABLED = Boolean ( JSON . parse ( process . env . CHANGELOG_DISABLED || 'false' ) )
10
12
11
- async function getRssFeed ( url ) {
13
+ async function getRssFeed ( url : string ) {
12
14
const parser = new Parser ( { timeout : 5000 } )
13
15
const feedUrl = `${ url } /feed`
14
16
let feed
15
17
16
18
try {
17
19
feed = await parser . parseURL ( feedUrl )
18
20
} catch ( err ) {
19
- console . error ( `cannot get ${ feedUrl } : ${ err . message } ` )
21
+ console . error ( `cannot get ${ feedUrl } : ${ err instanceof Error ? err . message : err } ` )
20
22
return
21
23
}
22
24
23
25
return feed
24
26
}
25
27
26
- export async function getChangelogItems ( prefix , feedUrl , ignoreCache = false ) {
28
+ export async function getChangelogItems (
29
+ prefix : string | undefined ,
30
+ feedUrl : string ,
31
+ ignoreCache = false ,
32
+ ) : Promise < ChangelogItem [ ] | undefined > {
27
33
if ( CHANGELOG_DISABLED ) {
28
34
if ( process . env . NODE_ENV === 'development' ) {
29
35
console . warn ( `Downloading changelog (${ feedUrl } ) items is disabled.` )
@@ -44,14 +50,15 @@ export async function getChangelogItems(prefix, feedUrl, ignoreCache = false) {
44
50
}
45
51
46
52
// only show the first 3 posts
47
- const changelog = feed . items . slice ( 0 , 3 ) . map ( ( item ) => {
53
+ const changelog : ChangelogItem [ ] = feed . items . slice ( 0 , 3 ) . map ( ( item ) => {
54
+ const rawTitle = item . title as string
48
55
// remove the prefix if it exists (Ex: 'GitHub Actions: '), where the colon and expected whitespace should be hardcoded.
49
- const title = prefix ? item . title . replace ( new RegExp ( `^${ prefix } ` ) , '' ) : item . title
56
+ const title = prefix ? rawTitle . replace ( new RegExp ( `^${ prefix } ` ) , '' ) : rawTitle
50
57
return {
51
58
// capitalize the first letter of the title
52
59
title : title . trim ( ) . charAt ( 0 ) . toUpperCase ( ) + title . slice ( 1 ) ,
53
- date : item . isoDate ,
54
- href : item . link ,
60
+ date : item . isoDate as string ,
61
+ href : item . link as string ,
55
62
}
56
63
} )
57
64
@@ -65,13 +72,13 @@ export async function getChangelogItems(prefix, feedUrl, ignoreCache = false) {
65
72
66
73
const globalCache = new Map ( )
67
74
68
- function getChangelogCacheKey ( prefix , feedUrl ) {
75
+ function getChangelogCacheKey ( prefix : string | undefined , feedUrl : string ) {
69
76
// Return a string that is only letters so it's safe to use this
70
77
// for the filename when caching to disk.
71
78
return `${ prefix || '' } ${ feedUrl } ` . replace ( / [ ^ a - z ] + / gi, '' )
72
79
}
73
80
74
- function getDiskCachePath ( prefix , feedUrl ) {
81
+ function getDiskCachePath ( prefix : string | undefined , feedUrl : string ) {
75
82
// When in local development or in tests, use disk caching
76
83
if ( process . env . NODE_ENV === 'test' || process . env . NODE_ENV === 'development' ) {
77
84
if ( CHANGELOG_CACHE_FILE_PATH ) {
@@ -84,7 +91,7 @@ function getDiskCachePath(prefix, feedUrl) {
84
91
}
85
92
}
86
93
87
- function getChangelogItemsFromCache ( prefix , feedUrl ) {
94
+ function getChangelogItemsFromCache ( prefix : string | undefined , feedUrl : string ) {
88
95
const cacheKey = getChangelogCacheKey ( prefix , feedUrl )
89
96
90
97
if ( globalCache . get ( cacheKey ) ) {
@@ -103,7 +110,7 @@ function getChangelogItemsFromCache(prefix, feedUrl) {
103
110
return payload
104
111
} catch ( err ) {
105
112
// If it wasn't on disk, that's fine.
106
- if ( err . code === 'ENOENT' ) return
113
+ if ( err instanceof Error && 'code' in err && err . code === 'ENOENT' ) return
107
114
// The JSON.parse() most likely failed. Ignore the error
108
115
// but delete the file so it won't be attempted again.
109
116
if ( err instanceof SyntaxError ) {
@@ -115,7 +122,11 @@ function getChangelogItemsFromCache(prefix, feedUrl) {
115
122
}
116
123
}
117
124
118
- function setChangelogItemsCache ( prefix , feedUrl , payload ) {
125
+ function setChangelogItemsCache (
126
+ prefix : string | undefined ,
127
+ feedUrl : string ,
128
+ payload : ChangelogItem [ ] ,
129
+ ) {
119
130
const cacheKey = getChangelogCacheKey ( prefix , feedUrl )
120
131
globalCache . set ( cacheKey , payload )
121
132
0 commit comments