Skip to content

Commit 1a1c02a

Browse files
committed
update metadata script
1 parent eb59c03 commit 1a1c02a

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

apps/svelte.dev/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"check": "node scripts/update.js && svelte-kit sync && svelte-check",
1414
"format": "prettier --write .",
1515
"lint": "prettier --check .",
16-
"sync-docs": "tsx scripts/sync-docs/index.ts"
16+
"sync-docs": "tsx scripts/sync-docs/index.ts",
17+
"sync-packages": "tsx scripts/sync-packages/index.ts"
1718
},
1819
"dependencies": {
1920
"@jridgewell/sourcemap-codec": "^1.4.15",
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { PACKAGES_META } from '../../src/lib/packages-meta';
2+
import { execSync } from 'node:child_process';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import process from 'node:process';
6+
7+
const start = performance.now();
8+
console.log('[sync-packages] start');
9+
10+
const packages = [
11+
...PACKAGES_META.FEATURED.flatMap((pkg) => pkg.packages),
12+
...PACKAGES_META.SV_ADD.packages
13+
];
14+
15+
const registryFolder = 'src/lib/server/generated/registry';
16+
17+
// PART 1: create missing json files
18+
for (const pkg of packages) {
19+
const cleanPkg = pkg.replace('@', '').replace('/', '-');
20+
const jsonPath = path.join(registryFolder, `${cleanPkg}.json`);
21+
if (!fs.existsSync(jsonPath)) {
22+
console.warn(` "${pkg}" -> "${jsonPath}" not found, we will create it!`);
23+
const p = await fetchData(pkg);
24+
writeButPretty(jsonPath, JSON.stringify(p, null, 2));
25+
}
26+
}
27+
28+
// PART 2: check if all json files are needed
29+
let registryJsonFiles = fs.readdirSync(registryFolder);
30+
const jsonUsed: string[] = [];
31+
for (const pkg of packages) {
32+
const cleanPkg = pkg.replace('@', '').replace('/', '-');
33+
const cleanPkgFile = `${cleanPkg}.json`;
34+
const jsonPath = path.join(registryFolder, cleanPkgFile);
35+
if (fs.existsSync(jsonPath)) {
36+
jsonUsed.push(cleanPkgFile);
37+
}
38+
}
39+
const jsonNotNeeded = registryJsonFiles.filter((pkg) => !jsonUsed.includes(pkg));
40+
if (jsonNotNeeded.length > 0) {
41+
console.error(jsonNotNeeded.join('\n'));
42+
console.error(
43+
`ERROR: ${jsonNotNeeded.length} json files are not needed as they are not in the packages array`
44+
);
45+
46+
// delete json files
47+
// for (const pkg of jsonNotNeeded) {
48+
// fs.unlinkSync(path.join(registryFolder, pkg));
49+
// }
50+
51+
theEnd(1);
52+
}
53+
54+
// PART 3: refresh data
55+
registryJsonFiles = fs.readdirSync(registryFolder); // .slice(0, 1);
56+
57+
const batch = 10;
58+
for (let i = 0; i < registryJsonFiles.length; i += batch) {
59+
const batchFiles = registryJsonFiles.slice(i, i + batch);
60+
await Promise.all(
61+
batchFiles.map(async (pkg) => {
62+
await refreshJson(path.join(registryFolder, pkg));
63+
})
64+
);
65+
}
66+
67+
theEnd(0);
68+
69+
// HELPERS
70+
71+
function theEnd(val: number) {
72+
console.log(`[sync-packages] exit(${val}) - took: ${(performance.now() - start).toFixed(0)}ms`);
73+
process.exit(val);
74+
}
75+
76+
async function fetchData(pkg: string) {
77+
const [npmInfo, npmDlInfo] = await Promise.all([
78+
fetch(`https://registry.npmjs.org/${pkg}`).then((r) => r.json()),
79+
fetch(`https://api.npmjs.org/downloads/point/last-week/${pkg}`).then((r) => r.json())
80+
]);
81+
// delete npmInfo.readme;
82+
// delete npmInfo.versions;
83+
const description = npmInfo.description;
84+
const raw_repo_url = npmInfo.repository?.url ?? '';
85+
const repo_url = raw_repo_url?.replace(/^git\+/, '').replace(/\.git$/, '');
86+
if (!repo_url) {
87+
console.error(`repo_url not found for ${pkg}`);
88+
}
89+
const git_org = repo_url?.split('/')[3];
90+
const git_repo = repo_url?.split('/')[4];
91+
92+
const authors = npmInfo.maintainers?.map((m: { name: string }) => m.name);
93+
const homepage = npmInfo.homepage;
94+
const downloads = npmDlInfo.downloads;
95+
const version = npmInfo['dist-tags'].latest;
96+
const updated = npmInfo.time[version];
97+
98+
let github_stars = 0;
99+
if (git_org && git_repo) {
100+
const githubInfo = await fetch(`https://api.github.com/repos/${git_org}/${git_repo}`).then(
101+
(r) => r.json()
102+
);
103+
github_stars = githubInfo.stargazers_count;
104+
}
105+
106+
return {
107+
name: pkg,
108+
// description, // let's not overwrite the description for now.
109+
repo_url,
110+
authors,
111+
homepage,
112+
downloads,
113+
version,
114+
updated,
115+
// runes: false,
116+
github_stars
117+
// typescript: false,
118+
// kit_range: '',
119+
// last_rune_check_version: ''
120+
};
121+
}
122+
123+
async function refreshJson(fullPath: string) {
124+
console.log(`Working on:`, fullPath);
125+
126+
const currentJson = JSON.parse(fs.readFileSync(fullPath, 'utf-8'));
127+
const newData = await fetchData(currentJson.name);
128+
129+
// remove all undefined values
130+
for (const key in newData) {
131+
if (newData[key] === undefined) {
132+
delete newData[key];
133+
}
134+
}
135+
136+
writeButPretty(fullPath, JSON.stringify({ ...currentJson, ...newData }, null, 2));
137+
}
138+
139+
function writeButPretty(path: string, data: any) {
140+
fs.writeFileSync(path, data);
141+
execSync(`prettier --write ${path}`);
142+
}

0 commit comments

Comments
 (0)