|
| 1 | +import { GitHubCache, svelteGitHubCache } from "./github-cache"; |
| 2 | +import { transformationRepos } from "$lib/repositories"; |
| 3 | + |
| 4 | +export class PackageDiscoverer { |
| 5 | + readonly #cache: GitHubCache; |
| 6 | + readonly #repos: { owner: string; repoName: string; tagToName: (name: string) => string }[]; |
| 7 | + #packages: { owner: string; repoName: string; packages: string[] }[] = []; |
| 8 | + |
| 9 | + constructor( |
| 10 | + cache: GitHubCache, |
| 11 | + repos: { owner: string; repoName: string; tagToName: (name: string) => string }[] |
| 12 | + ) { |
| 13 | + this.#cache = cache; |
| 14 | + this.#repos = repos; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Discover the packages from the passed releases |
| 19 | + * and replaces/adds it to the #packages array from |
| 20 | + * the owner and repo name. |
| 21 | + * |
| 22 | + * @param owner the owner of the repository to use to populate the array |
| 23 | + * @param repoName the name of the repository to use to populate the array |
| 24 | + * @param releases the releases to populate the array from |
| 25 | + */ |
| 26 | + async discoverReleases( |
| 27 | + owner: string, |
| 28 | + repoName: string, |
| 29 | + releases: Awaited<ReturnType<InstanceType<typeof GitHubCache>["getReleases"]>> |
| 30 | + ) { |
| 31 | + // 1. Find the transformation function in the existing repos, exit otherwise |
| 32 | + const repo = this.#repos.find(({ owner: o, repoName: n }) => o == owner && repoName == n); |
| 33 | + if (!repo) return; |
| 34 | + // 2. Compute the unique packages |
| 35 | + const uniquePackages = [...new Set(releases.map(({ tag_name }) => repo.tagToName(tag_name)))]; |
| 36 | + // 3. Replace or add the value in the array |
| 37 | + for (const [i, { owner: o, repoName: n }] of this.#packages.entries()) { |
| 38 | + if (o === owner && repoName == n) { |
| 39 | + this.#packages[i]!.packages = uniquePackages; |
| 40 | + return; |
| 41 | + } |
| 42 | + } |
| 43 | + this.#packages.push({ owner, repoName, packages: uniquePackages }); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * A processing-heavy function that discovers all the |
| 48 | + * packages for the given #repos. |
| 49 | + * Populates the result into #packages. |
| 50 | + */ |
| 51 | + async discoverAll() { |
| 52 | + this.#packages = await Promise.all( |
| 53 | + this.#repos.map(async ({ owner, repoName, tagToName }) => { |
| 54 | + const releases = await this.#cache.getReleases(owner, repoName); |
| 55 | + const packages = [...new Set(releases.map(({ tag_name }) => tagToName(tag_name)))]; |
| 56 | + return { owner, repoName, packages }; |
| 57 | + }) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the saved #packages if they're not empty, |
| 63 | + * otherwise calls {@link discoverAll} then returns the |
| 64 | + * #packages. |
| 65 | + * |
| 66 | + * @returns all the discovered packages per repo name |
| 67 | + */ |
| 68 | + async getOrDiscover() { |
| 69 | + if (this.#packages.length) { |
| 70 | + return this.#packages; |
| 71 | + } |
| 72 | + await this.discoverAll(); |
| 73 | + return this.#packages; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export const discoverer = new PackageDiscoverer(svelteGitHubCache.cache, transformationRepos); |
0 commit comments