import { differenceInDays } from 'date-fns' import type { FilteredRepository, Repository } from '../types/index.js' import type { GitHubClient } from './github.js' export async function filterInactiveRepositories( repositories: Repository[], githubClient: GitHubClient, ): Promise { const now = new Date() const oneYearAgo = new Date( now.getFullYear() - 1, now.getMonth(), now.getDate(), ) // First filter by stars and forks const candidateRepos = repositories.filter( (repo) => repo.stargazers_count === 0 && repo.forks_count === 0, ) // Process all candidates in parallel const repoPromises = candidateRepos.map(async (repo) => { // Get last commit date const lastCommitDate = await githubClient.getLastCommitDate( repo.owner.login, repo.name, ) // If no commits found, use pushed_at date const lastActivityDate = lastCommitDate || (repo.pushed_at ? new Date(repo.pushed_at) : null) if (!lastActivityDate || lastActivityDate < oneYearAgo) { const daysSinceLastCommit = lastActivityDate ? differenceInDays(now, lastActivityDate) : null return { ...repo, lastCommitDate: lastActivityDate, daysSinceLastCommit, } as FilteredRepository } return null }) const results = await Promise.all(repoPromises) return results.filter((repo): repo is FilteredRepository => repo !== null) } export function sortByInactivity( repos: FilteredRepository[], ): FilteredRepository[] { return [...repos].sort((a, b) => { // Repos with no activity go first if (!a.lastCommitDate && b.lastCommitDate) return -1 if (a.lastCommitDate && !b.lastCommitDate) return 1 if (!a.lastCommitDate && !b.lastCommitDate) return 0 // Sort by oldest activity first return a.lastCommitDate!.getTime() - b.lastCommitDate!.getTime() }) }