Skip to content

Commit

Permalink
fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sarisia committed Dec 4, 2022
1 parent fd28fbc commit 401a8a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
26 changes: 16 additions & 10 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import rss from 'rss-parser'


interface MatchedPlaceholder {
raw: string
flag: string
verb: string
}


const placeholderRe = /(?<raw>\$\{(?<flag>\d*)(?<verb>.+?)\})/g

export function formatFeeds(
feeds:Array<rss.Item>, format:string, startFlag:string, endFlag:string,
locale:string, timezone:string
): Array<string> {
// @ts-ignore
const fields = [...format.matchAll(placeholderRe)].map((e) => e.groups)
feeds: Array<rss.Item>, format: string, startFlag: string, endFlag: string,
locale: string, timezone: string
): Array<string> {
const fields = [...format.matchAll(placeholderRe)].map((e) => e.groups) as unknown as MatchedPlaceholder[]
return [
startFlag,
...feeds.map((feed) => formatFeed(feed, format, fields, locale, timezone)),
Expand All @@ -16,9 +23,8 @@ export function formatFeeds(
}

function formatFeed(
feed:rss.Item, format:string, fields: Array<{raw:string, flag:string, verb:string}>,
locale:string, timezone:string): string
{
feed: rss.Item, format: string, fields: MatchedPlaceholder[],
locale: string, timezone: string): string {
const feedDate = feed.isoDate ? new Date(feed.isoDate) : undefined

// TODO: more memory-friendly arg initialize
Expand All @@ -33,7 +39,7 @@ function formatFeed(
date: feedDate?.toLocaleString(locale, { timeZone: timezone })
}

return fields.reduce((acc:string, cur:{raw:string, flag:string, verb:string}) => {
return fields.reduce((acc: string, cur: MatchedPlaceholder) => {
// flag: 2 -> pad to length 2 with space (' ')
// flag:02 -> pad to length 2 with zero('0')
const padstring = cur.flag.startsWith('0') ? '0' : ' '
Expand All @@ -43,5 +49,5 @@ function formatFeed(

return acc.replace(cur.raw, arg || '')
}
, format)
, format)
}
22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function run() {
core.setFailed('url is missing.')
return
}

const file = core.getInput('file')
if (!file) {
if (nowrite) {
Expand All @@ -60,7 +60,7 @@ async function run() {
if (file) {
try {
lines = await getLines(file)
} catch(e) {
} catch (e: any) {
core.setFailed(`failed to read file: ${e.message}`)
return
}
Expand All @@ -69,7 +69,7 @@ async function run() {
// get entries from feed
// don't do Array.prototype.forEach! It won't wait promises!
const fetchers = urls.map((u, i) => {
return async function() {
return async function () {
for (let trycount = 0; trycount < retry; ++trycount) {
if (trycount) {
// retry backoff
Expand All @@ -80,7 +80,7 @@ async function run() {
try {
return await getFeedItems(u)
} catch (e) {
core.error(`[feed ${i + 1}/${urls.length}][try ${trycount+1}/${retry}] failed to get feed: ${e}`)
core.error(`[feed ${i + 1}/${urls.length}][try ${trycount + 1}/${retry}] failed to get feed: ${e}`)
}
}

Expand All @@ -97,7 +97,7 @@ async function run() {
try {
const results = await Promise.all(fetchers.map(f => f()))
allItems = allItems.concat(...results)
} catch(e) {
} catch (e) {
core.setFailed("Aborted by ensure_all")
return
}
Expand All @@ -106,12 +106,12 @@ async function run() {
core.setFailed('Nothing was fetched')
return
}

// sort
if (sort) {
try {
allItems = sortItems(allItems)
} catch (e) {
} catch (e: any) {
core.setFailed(`failed to sort feed items: ${e.message}`)
return
}
Expand All @@ -122,20 +122,20 @@ async function run() {
const newLines = formatFeeds(items, format, startFlag, endFlag, locale, timezone)
const joinedNewLines = newLines.join('\n')
core.startGroup('Dump feeds block')
core.info(joinedNewLines)
core.info(joinedNewLines)
core.endGroup()

const result = updateFeed(lines, newLines, startFlag, endFlag)
const joinedResult = result.join('\n')
core.startGroup('Dump result document')
core.info(joinedResult)
core.info(joinedResult)
core.endGroup()

// write result to file if nowrite is not set
if (!nowrite) {
try {
await write(file, joinedResult)
} catch (e) {
} catch (e: any) {
core.setFailed(`failed to write file: ${e.message}`)
return
}
Expand Down

0 comments on commit 401a8a6

Please sign in to comment.