diff --git a/src/command/like-rank.ts b/src/command/like-rank.ts index e9217d3..6c5749f 100644 --- a/src/command/like-rank.ts +++ b/src/command/like-rank.ts @@ -21,8 +21,8 @@ export const likeRank = createCommand('like-rank') count: +opts.count, }) }) -const maxRetries = 3; // Set the maximum number of retries -const retryDelay = 1000; // Set the delay between retries in milliseconds +const maxRetries = 3 // Set the maximum number of retries +const retryDelay = 1000 // Set the delay between retries in milliseconds export const likeRanking = async ({ top, count }: LikeRankOptions) => { const [user] = filterUsers() @@ -34,42 +34,48 @@ export const likeRanking = async ({ top, count }: LikeRankOptions) => { limit: count > 0 ? limit.limitMaxCount(count) : limit.limitNone(), }) - const userMap: Record = {} - const repositoryErrorCount: Record = {}; // Track the error count for each repository + const repositoryErrorCount: Record = {} // Track the error count for each repository for (const [i, post] of posts.entries()) { spinner.update(`Fetching post (${i + 1} / ${posts.length})`) - const repositoryId = post.id; + const repositoryId = post.id - let retryCount = 0; - let success = false; + let retryCount = 0 + let success = false while (!success && retryCount < maxRetries) { try { - const users = await post.listLikedUsers(); + const users = await post.listLikedUsers() for (const user of users) { - const id = user.id; - if (!userMap[id]) userMap[id] = { user, count: 1 }; - else userMap[id].count++; + const id = user.id + if (!userMap[id]) userMap[id] = { user, count: 1 } + else userMap[id].count++ } - success = true; // Mark success to exit the loop - } catch (error) { - retryCount++; - repositoryErrorCount[repositoryId] = (repositoryErrorCount[repositoryId] || 0) + 1; + success = true // Mark success to exit the loop + } catch { + retryCount++ + repositoryErrorCount[repositoryId] = + (repositoryErrorCount[repositoryId] || 0) + 1 if (retryCount < maxRetries) { - await new Promise(resolve => setTimeout(resolve, retryDelay)); // Wait for the specified delay + await new Promise((resolve) => setTimeout(resolve, retryDelay)) // Wait for the specified delay } } } if (!success) { - console.error(`Failed to fetch liked users for post ${i + 1} after ${maxRetries} attempts.`); + console.error( + `Failed to fetch liked users for post ${ + i + 1 + } after ${maxRetries} attempts.`, + ) if (repositoryErrorCount[repositoryId] >= 3) { - console.error(`Exiting due to repeated failures for repository ${repositoryId}`); - process.exit(1); // Exit the process with an error code + console.error( + `Exiting due to repeated failures for repository ${repositoryId}`, + ) + process.exit(1) // Exit the process with an error code } } }