Skip to content

Commit eeea23e

Browse files
feat(repos): transition to a built-in way to get the package names alongside the versions
1 parent 995766b commit eeea23e

File tree

5 files changed

+79
-40
lines changed

5 files changed

+79
-40
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ Fork the repo, edit the `/src/lib/repositories.ts` file, and open a PR.
6363
The code architecture is made to be as flexible as possible, here's how it works:
6464

6565
```typescript
66-
const repos: Record<Tab, { name: string; repos: Repo[] }> = {
67-
svelte: ...,
68-
kit: ...,
66+
const repos = {
67+
svelte: {/* ... */},
68+
kit: {/* ... */},
6969
others: {
7070
name: "Other",
7171
repos: [
@@ -76,7 +76,7 @@ const repos: Record<Tab, { name: string; repos: Repo[] }> = {
7676
changesMode: "releases", // Optional line, the way to get the changes; either "releases" or "changelog", defaults to "releases"
7777
repoName: "your-repo", // The name of the repo on GitHub, as it appears in the URL: https://github.com/sveltejs/your-repo
7878
dataFilter: ({ tag_name }) => true, // Optional line, return false to exclude a version from its tag name
79-
versionFromTag: tag => "...", // Return the version from the tag name; must be a valid semver
79+
metadataFromTag: tag => ["package-name", "2.4.3"], // Return the package name and version from the tag name; the version must be a valid semver without any leading "v"
8080
changelogContentsReplacer: contents => contents, // Optional line, replace the contents of the changelog file before parsing it; only used if `changesMode` is "changelog"
8181
}
8282
]

src/lib/repositories.ts

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Repo, Tab } from "$lib/types";
1+
import type { RepoInfo, Category } from "$lib/types";
22

3-
const repos: Record<Tab, { name: string; repos: Repo[] }> = {
3+
const repos: Record<Category, { name: string; repos: RepoInfo[] }> = {
44
svelte: {
55
name: "Svelte",
66
repos: [
77
{
88
repoName: "svelte",
9-
versionFromTag: tag => tag.substring(tag.indexOf("@") + 1)
9+
metadataFromTag: splitByLastAt
1010
}
1111
]
1212
},
@@ -16,7 +16,7 @@ const repos: Record<Tab, { name: string; repos: Repo[] }> = {
1616
{
1717
repoName: "kit",
1818
dataFilter: ({ tag_name }) => tag_name.includes("/kit@"),
19-
versionFromTag: tag => tag.substring(tag.lastIndexOf("@") + 1)
19+
metadataFromTag: splitByLastAt
2020
}
2121
]
2222
},
@@ -26,72 +26,111 @@ const repos: Record<Tab, { name: string; repos: Repo[] }> = {
2626
{
2727
repoName: "kit",
2828
dataFilter: ({ tag_name }) => !tag_name.includes("/kit@"),
29-
versionFromTag: tag => tag.substring(tag.lastIndexOf("@") + 1)
29+
metadataFromTag: splitByLastAt
3030
},
3131
{
3232
repoName: "cli",
33-
versionFromTag: tag => tag.substring(tag.lastIndexOf("@") + 1)
33+
metadataFromTag: splitByLastAt
3434
},
3535
{
3636
repoName: "vite-plugin-svelte",
37-
versionFromTag: tag => tag.substring(tag.lastIndexOf("@") + 1)
37+
metadataFromTag: splitByLastAt
3838
},
3939
{
4040
repoName: "eslint-plugin-svelte",
41-
versionFromTag: tag =>
42-
tag.includes("@") ? tag.substring(tag.lastIndexOf("@") + 1) : tag.replace(/^v/, "")
41+
metadataFromTag(tag) {
42+
if (tag.includes("@")) {
43+
return splitByLastAt(tag);
44+
}
45+
return [this.repoName, tag.replace(/^v/, "")];
46+
}
4347
},
4448
{
4549
repoName: "eslint-config",
46-
versionFromTag: tag => tag.replace(/^v/, "")
50+
metadataFromTag(tag) {
51+
return [this.repoName, tag.replace(/^v/, "")];
52+
}
4753
},
4854
{
4955
repoName: "svelte-eslint-parser",
50-
versionFromTag: tag => tag.replace(/^v/, "")
56+
metadataFromTag(tag) {
57+
return [this.repoName, tag.replace(/^v/, "")];
58+
}
5159
},
5260
{
5361
repoName: "language-tools",
54-
versionFromTag: tag => tag.substring(tag.lastIndexOf("-") + 1)
62+
metadataFromTag: tag => {
63+
const lastIndex = tag.lastIndexOf("-");
64+
return [tag.substring(0, lastIndex), tag.substring(lastIndex + 1)];
65+
}
5566
},
5667
{
5768
repoName: "acorn-typescript",
58-
versionFromTag: tag => tag.replace(/^v/, "")
69+
metadataFromTag(tag) {
70+
return [this.repoName, tag.replace(/^v/, "")];
71+
}
5972
},
6073
{
6174
repoName: "svelte-devtools",
62-
versionFromTag: tag => tag.replace(/^v/, "")
75+
metadataFromTag(tag) {
76+
return [this.repoName, tag.replace(/^v/, "")];
77+
}
6378
},
6479
{
6580
changesMode: "changelog",
6681
repoName: "svelte-preprocess",
67-
versionFromTag: tag => tag.replace(/^v/, ""),
82+
metadataFromTag(tag) {
83+
return [this.repoName, tag.replace(/^v/, "")];
84+
},
6885
changelogContentsReplacer: file => file.replace(/^# \[/gm, "## [")
6986
},
7087
{
7188
changesMode: "changelog",
7289
repoName: "rollup-plugin-svelte",
73-
versionFromTag: tag => tag.replace(/^v/, "")
90+
metadataFromTag(tag) {
91+
return [this.repoName, tag.replace(/^v/, "")];
92+
}
7493
},
7594
{
7695
changesMode: "changelog",
7796
repoName: "prettier-plugin-svelte",
78-
versionFromTag: tag => tag.replace(/^v/, "")
97+
metadataFromTag(tag) {
98+
return [this.repoName, tag.replace(/^v/, "")];
99+
}
79100
}
80101
]
81102
}
82103
};
83104

105+
/**
106+
* A convenience helper to split a string into two parts
107+
* from its last occurrence of the `@` symbol.
108+
*
109+
* @param s the input string
110+
* @returns an array of length 2 with the two split elements
111+
*/
112+
function splitByLastAt(s: string): [string, string] {
113+
const lastIndex = s.lastIndexOf("@");
114+
return [s.substring(0, lastIndex), s.substring(lastIndex + 1)];
115+
}
116+
84117
type Entries<T> = {
85118
[K in keyof T]: [K, T[K]];
86119
}[keyof T][];
87120

88121
/**
89-
* Returns repositories as entries for ease of use
90-
* and iterating.
122+
* Get all repositories as entries for ease of use
123+
* and iteration.
91124
*
92125
* @example
93-
* const [id, { name, repos }] = getRepositories();
126+
* const [id, { name, repos }] = repositories;
94127
*/
95-
export function getRepositories() {
96-
return Object.entries(repos) as unknown as Entries<typeof repos>;
97-
}
128+
export const repositories = Object.entries(repos) as unknown as Entries<typeof repos>;
129+
130+
/**
131+
* Get all a record of all GitHub repositories
132+
* from the collection.
133+
*/
134+
export const githubRepos = {
135+
sveltejs: [...new Set(repositories.flatMap(([, { repos }]) => repos.map(r => r.repoName)))]
136+
};

src/lib/types.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { GitHubRelease } from "$lib/server/github-cache";
22

3-
export type Repo = {
3+
export type RepoInfo = {
44
/**
55
* Mode to fetch the releases of the repo.
66
* - `releases`: Fetches from the Releases page
@@ -16,23 +16,26 @@ export type Repo = {
1616
* If it returns false, the release is filtered out.
1717
*
1818
* @param release The release to filter
19+
* @returns whether we want to keep the release
1920
*/
2021
dataFilter?: (release: GitHubRelease) => boolean;
2122
/**
22-
* Extracts the version from the tag name.
23+
* Extracts the package name and version from the tag name.
2324
*
24-
* @param tag The tag name to extract the version from
25+
* @param tag The tag name to extract the name and version from
26+
* @returns an array with the package name, and the package version
2527
*/
26-
versionFromTag: (tag: string) => string;
28+
metadataFromTag: (tag: string) => [string, string];
2729
/**
2830
* Replaces the contents of the changelog file.
2931
* Only used when `changesMode` is set to `changelog`.
3032
* By default, no replacement is performed.
3133
*
3234
* @param file The contents of the changelog file
35+
* @returns the modified contents
3336
*/
3437
changelogContentsReplacer?: (file: string) => string;
3538
};
3639

37-
export const availableTabs = ["svelte", "kit", "others"] as const;
38-
export type Tab = (typeof availableTabs)[number];
40+
export const availableCategory = ["svelte", "kit", "others"] as const;
41+
export type Category = (typeof availableCategory)[number];

src/routes/+layout.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { injectSpeedInsights } from "@vercel/speed-insights/sveltekit";
2-
import { getRepositories } from "$lib/repositories";
2+
import { repositories } from "$lib/repositories";
33

44
injectSpeedInsights();
55

66
export function load() {
7-
return { repos: getRepositories() };
7+
return { repos: repositories };
88
}

src/routes/cron/+server.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { error, type RequestHandler } from "@sveltejs/kit";
22
import { CRON_SECRET } from "$env/static/private";
33
import { svelteGitHubCache } from "$lib/server/github-cache";
4-
import { getRepositories } from "$lib/repositories";
4+
import { githubRepos } from "$lib/repositories";
55

66
export const GET: RequestHandler = async ({ request }) => {
77
if (request.headers.get("Authorization") !== `Bearer ${CRON_SECRET}`) {
88
error(401);
99
}
1010

11-
const allRepos = new Set(
12-
getRepositories().flatMap(([, { repos }]) => repos.map(r => r.repoName))
13-
);
14-
for (const repo of allRepos) {
11+
for (const repo of githubRepos.sveltejs) {
1512
await svelteGitHubCache.fetchAndCacheReleases(repo);
1613
}
1714

0 commit comments

Comments
 (0)