|
| 1 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +const PRIMARY_PACKAGE_NAME = '@lyfie/luthor'; |
| 5 | +const HEADLESS_PACKAGE_NAME = '@lyfie/luthor-headless'; |
| 6 | + |
| 7 | +const FALLBACK_METRICS = { |
| 8 | + totalDownloads: null, |
| 9 | + lastMonthDownloads: null, |
| 10 | + latestVersion: 'N/A', |
| 11 | + headlessVersion: 'N/A', |
| 12 | + luthorPackageSize: null, |
| 13 | + headlessPackageSize: null, |
| 14 | + combinedPackageSize: null, |
| 15 | + releaseCount: null, |
| 16 | + fetchedAtIso: null, |
| 17 | +}; |
| 18 | + |
| 19 | +async function fetchJson(url) { |
| 20 | + const controller = new AbortController(); |
| 21 | + const timeout = setTimeout(() => controller.abort(), 5000); |
| 22 | + |
| 23 | + try { |
| 24 | + const response = await fetch(url, { |
| 25 | + headers: { |
| 26 | + accept: 'application/json', |
| 27 | + }, |
| 28 | + signal: controller.signal, |
| 29 | + }); |
| 30 | + |
| 31 | + if (!response.ok) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + return await response.json(); |
| 36 | + } catch { |
| 37 | + return null; |
| 38 | + } finally { |
| 39 | + clearTimeout(timeout); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function toIsoDate(value) { |
| 44 | + if (!value) return null; |
| 45 | + const parsed = new Date(value); |
| 46 | + if (Number.isNaN(parsed.getTime())) return null; |
| 47 | + return parsed.toISOString().slice(0, 10); |
| 48 | +} |
| 49 | + |
| 50 | +function numberOrNull(value) { |
| 51 | + return typeof value === 'number' && Number.isFinite(value) ? value : null; |
| 52 | +} |
| 53 | + |
| 54 | +async function getDownloadPoint(range, packageName) { |
| 55 | + const encodedPackageName = encodeURIComponent(packageName); |
| 56 | + const response = await fetchJson(`https://api.npmjs.org/downloads/point/${range}/${encodedPackageName}`); |
| 57 | + return numberOrNull(response?.downloads); |
| 58 | +} |
| 59 | + |
| 60 | +async function getPackageMetrics() { |
| 61 | + const [luthorRegistry, headlessRegistry] = await Promise.all([ |
| 62 | + fetchJson(`https://registry.npmjs.org/${encodeURIComponent(PRIMARY_PACKAGE_NAME)}`), |
| 63 | + fetchJson(`https://registry.npmjs.org/${encodeURIComponent(HEADLESS_PACKAGE_NAME)}`), |
| 64 | + ]); |
| 65 | + |
| 66 | + const today = new Date().toISOString().slice(0, 10); |
| 67 | + const luthorCreatedDate = toIsoDate(luthorRegistry?.time?.created); |
| 68 | + const headlessCreatedDate = toIsoDate(headlessRegistry?.time?.created); |
| 69 | + |
| 70 | + const [luthorTotalDownloads, headlessTotalDownloads, luthorLastMonthDownloads, headlessLastMonthDownloads] = |
| 71 | + await Promise.all([ |
| 72 | + luthorCreatedDate ? getDownloadPoint(`${luthorCreatedDate}:${today}`, PRIMARY_PACKAGE_NAME) : Promise.resolve(null), |
| 73 | + headlessCreatedDate ? getDownloadPoint(`${headlessCreatedDate}:${today}`, HEADLESS_PACKAGE_NAME) : Promise.resolve(null), |
| 74 | + getDownloadPoint('last-month', PRIMARY_PACKAGE_NAME), |
| 75 | + getDownloadPoint('last-month', HEADLESS_PACKAGE_NAME), |
| 76 | + ]); |
| 77 | + |
| 78 | + const totalDownloads = |
| 79 | + typeof luthorTotalDownloads === 'number' || typeof headlessTotalDownloads === 'number' |
| 80 | + ? (luthorTotalDownloads ?? 0) + (headlessTotalDownloads ?? 0) |
| 81 | + : null; |
| 82 | + const lastMonthDownloads = |
| 83 | + typeof luthorLastMonthDownloads === 'number' || typeof headlessLastMonthDownloads === 'number' |
| 84 | + ? (luthorLastMonthDownloads ?? 0) + (headlessLastMonthDownloads ?? 0) |
| 85 | + : null; |
| 86 | + |
| 87 | + const latestVersion = luthorRegistry?.['dist-tags']?.latest ?? 'N/A'; |
| 88 | + const headlessVersion = headlessRegistry?.['dist-tags']?.latest ?? 'N/A'; |
| 89 | + const luthorPackageSize = |
| 90 | + latestVersion !== 'N/A' ? numberOrNull(luthorRegistry?.versions?.[latestVersion]?.dist?.unpackedSize) : null; |
| 91 | + const headlessPackageSize = |
| 92 | + headlessVersion !== 'N/A' ? numberOrNull(headlessRegistry?.versions?.[headlessVersion]?.dist?.unpackedSize) : null; |
| 93 | + const combinedPackageSize = |
| 94 | + typeof luthorPackageSize === 'number' || typeof headlessPackageSize === 'number' |
| 95 | + ? (luthorPackageSize ?? 0) + (headlessPackageSize ?? 0) |
| 96 | + : null; |
| 97 | + const luthorReleaseCount = luthorRegistry?.versions ? Object.keys(luthorRegistry.versions).length : 0; |
| 98 | + const headlessReleaseCount = headlessRegistry?.versions ? Object.keys(headlessRegistry.versions).length : 0; |
| 99 | + const releaseCount = |
| 100 | + luthorReleaseCount > 0 || headlessReleaseCount > 0 ? luthorReleaseCount + headlessReleaseCount : null; |
| 101 | + |
| 102 | + const hasLiveData = Boolean( |
| 103 | + luthorRegistry || |
| 104 | + headlessRegistry || |
| 105 | + luthorTotalDownloads || |
| 106 | + headlessTotalDownloads || |
| 107 | + luthorLastMonthDownloads || |
| 108 | + headlessLastMonthDownloads, |
| 109 | + ); |
| 110 | + |
| 111 | + return { |
| 112 | + totalDownloads, |
| 113 | + lastMonthDownloads, |
| 114 | + latestVersion, |
| 115 | + headlessVersion, |
| 116 | + luthorPackageSize, |
| 117 | + headlessPackageSize, |
| 118 | + combinedPackageSize, |
| 119 | + releaseCount, |
| 120 | + fetchedAtIso: hasLiveData ? new Date().toISOString() : null, |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +function serializeMetrics(metrics) { |
| 125 | + return `export const homepageMetrics = ${JSON.stringify(metrics, null, 2)} as const;\n`; |
| 126 | +} |
| 127 | + |
| 128 | +async function writeMetrics(metrics) { |
| 129 | + const targetDir = path.join(process.cwd(), 'src', 'data'); |
| 130 | + await mkdir(targetDir, { recursive: true }); |
| 131 | + await writeFile(path.join(targetDir, 'homepage-metrics.generated.ts'), serializeMetrics(metrics)); |
| 132 | +} |
| 133 | + |
| 134 | +try { |
| 135 | + await writeMetrics(await getPackageMetrics()); |
| 136 | + console.log('Homepage metrics synced.'); |
| 137 | +} catch (error) { |
| 138 | + console.warn('Homepage metrics sync failed. Writing fallback metrics.'); |
| 139 | + console.warn(error); |
| 140 | + await writeMetrics(FALLBACK_METRICS); |
| 141 | +} |
0 commit comments