|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { dirname, resolve } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +export type FrameworkCategory = "core-ui" | "meta-framework"; |
| 6 | + |
| 7 | +export type UseCase = |
| 8 | + | "saas-dashboard" |
| 9 | + | "seo-content" |
| 10 | + | "enterprise-app" |
| 11 | + | "lightweight-widget" |
| 12 | + | "ecommerce" |
| 13 | + | "internal-tool" |
| 14 | + | "ai-app"; |
| 15 | + |
| 16 | +export interface FrameworkRecord { |
| 17 | + id: string; |
| 18 | + name: string; |
| 19 | + category: FrameworkCategory; |
| 20 | + github_repo: string; |
| 21 | + legacy_github_repo?: string; |
| 22 | + legacy_stars?: number; |
| 23 | + npm_package: string; |
| 24 | + official_url: string; |
| 25 | + latest_version: string; |
| 26 | + github_latest_tag?: string; |
| 27 | + stars: number; |
| 28 | + forks: number; |
| 29 | + npm_weekly_downloads: number; |
| 30 | + license: string; |
| 31 | + rendering_model: string; |
| 32 | + reactivity: string; |
| 33 | + language: string; |
| 34 | + ssr_seo_score: number; |
| 35 | + learning_curve: string; |
| 36 | + runtime_tier: string; |
| 37 | + best_for: UseCase[]; |
| 38 | + starter_path: string | null; |
| 39 | + summary: string; |
| 40 | +} |
| 41 | + |
| 42 | +export interface FrameworkDataset { |
| 43 | + metadata: { |
| 44 | + verified_at: string; |
| 45 | + sources: string[]; |
| 46 | + notes: string; |
| 47 | + }; |
| 48 | + frameworks: FrameworkRecord[]; |
| 49 | +} |
| 50 | + |
| 51 | +export interface RecommendationOptions { |
| 52 | + useCase: UseCase; |
| 53 | + seoCritical?: boolean; |
| 54 | + lowBundle?: boolean; |
| 55 | + lowLearningCurve?: boolean; |
| 56 | + category?: FrameworkCategory; |
| 57 | + limit?: number; |
| 58 | +} |
| 59 | + |
| 60 | +export interface ScoredFramework extends FrameworkRecord { |
| 61 | + match_score: number; |
| 62 | + match_reasons: string[]; |
| 63 | +} |
| 64 | + |
| 65 | +export const USE_CASE_LABELS: Record<UseCase, string> = { |
| 66 | + "saas-dashboard": "Interactive SaaS Dashboard / Web App", |
| 67 | + "seo-content": "SEO Content Site, Docs, Blog or Marketing Landing", |
| 68 | + "enterprise-app": "Large Multi-Team Enterprise Platform", |
| 69 | + "lightweight-widget": "Embeddable Widget / Low-JS Enhancement", |
| 70 | + "ecommerce": "E-Commerce Storefront (Core Web Vitals + SEO)", |
| 71 | + "internal-tool": "Internal Admin Panel / CRUD Portal", |
| 72 | + "ai-app": "AI Streaming UI / Agent Workspace", |
| 73 | +}; |
| 74 | + |
| 75 | +export function loadDataset(customPath?: string): FrameworkDataset { |
| 76 | + const here = dirname(fileURLToPath(import.meta.url)); |
| 77 | + const datasetPath = customPath ?? resolve(here, "..", "frameworks.json"); |
| 78 | + return JSON.parse(readFileSync(datasetPath, "utf8")) as FrameworkDataset; |
| 79 | +} |
| 80 | + |
| 81 | +export function formatCompactNumber(value: number): string { |
| 82 | + if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`; |
| 83 | + if (value >= 1_000) return `${(value / 1_000).toFixed(1)}K`; |
| 84 | + return String(value); |
| 85 | +} |
| 86 | + |
| 87 | +export function scoreFramework( |
| 88 | + fw: FrameworkRecord, |
| 89 | + options: RecommendationOptions, |
| 90 | +): { score: number; reasons: string[] } { |
| 91 | + let score = 0; |
| 92 | + const reasons: string[] = []; |
| 93 | + |
| 94 | + const idx = fw.best_for.indexOf(options.useCase); |
| 95 | + if (idx !== -1) { |
| 96 | + score += Math.max(40 - idx * 8, 16); |
| 97 | + reasons.push(`strong fit for ${options.useCase}`); |
| 98 | + } |
| 99 | + |
| 100 | + if (options.seoCritical) { |
| 101 | + score += fw.ssr_seo_score * 6; |
| 102 | + if (fw.ssr_seo_score === 5) { |
| 103 | + reasons.push("top-tier SSR/SSG SEO readiness"); |
| 104 | + } |
| 105 | + if (fw.category === "meta-framework") { |
| 106 | + score += 12; |
| 107 | + reasons.push("built-in SSR/SSG routing & metadata"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (options.lowBundle) { |
| 112 | + const tier = fw.runtime_tier.toLowerCase(); |
| 113 | + if (tier.includes("zero js") || tier.includes("tiny")) { |
| 114 | + score += 22; |
| 115 | + reasons.push("minimal client JavaScript payload"); |
| 116 | + } else if (tier.includes("small")) { |
| 117 | + score += 10; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (options.lowLearningCurve) { |
| 122 | + const curve = fw.learning_curve.toLowerCase(); |
| 123 | + if (curve.includes("very low")) { |
| 124 | + score += 18; |
| 125 | + reasons.push("minimal onboarding & zero/low boilerplate"); |
| 126 | + } else if (curve === "low") { |
| 127 | + score += 12; |
| 128 | + reasons.push("gentle learning curve"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (options.category && fw.category === options.category) { |
| 133 | + score += 10; |
| 134 | + } |
| 135 | + |
| 136 | + if (fw.npm_weekly_downloads >= 50_000_000) score += 8; |
| 137 | + else if (fw.npm_weekly_downloads >= 10_000_000) score += 5; |
| 138 | + else if (fw.npm_weekly_downloads >= 1_000_000) score += 2; |
| 139 | + |
| 140 | + if (fw.starter_path) { |
| 141 | + score += 3; |
| 142 | + reasons.push(`free starter at ${fw.starter_path}`); |
| 143 | + } |
| 144 | + |
| 145 | + return { score, reasons }; |
| 146 | +} |
| 147 | + |
| 148 | +export function recommendFrameworks( |
| 149 | + dataset: FrameworkDataset, |
| 150 | + options: RecommendationOptions, |
| 151 | +): ScoredFramework[] { |
| 152 | + const limit = options.limit ?? 5; |
| 153 | + return dataset.frameworks |
| 154 | + .map((fw) => { |
| 155 | + const { score, reasons } = scoreFramework(fw, options); |
| 156 | + return { ...fw, match_score: score, match_reasons: reasons }; |
| 157 | + }) |
| 158 | + .sort((a, b) => b.match_score - a.match_score || b.npm_weekly_downloads - a.npm_weekly_downloads) |
| 159 | + .slice(0, limit); |
| 160 | +} |
| 161 | + |
| 162 | +export function exportCsv(frameworks: FrameworkRecord[]): string { |
| 163 | + const fields: Array<keyof FrameworkRecord> = [ |
| 164 | + "id", |
| 165 | + "name", |
| 166 | + "category", |
| 167 | + "github_repo", |
| 168 | + "npm_package", |
| 169 | + "latest_version", |
| 170 | + "stars", |
| 171 | + "forks", |
| 172 | + "npm_weekly_downloads", |
| 173 | + "license", |
| 174 | + "rendering_model", |
| 175 | + "ssr_seo_score", |
| 176 | + "learning_curve", |
| 177 | + "official_url", |
| 178 | + ]; |
| 179 | + const escapeCell = (raw: unknown): string => { |
| 180 | + const str = String(raw ?? ""); |
| 181 | + return /[",\n]/.test(str) ? `"${str.replace(/"/g, "\"\"")}"` : str; |
| 182 | + }; |
| 183 | + const lines = [fields.join(",")]; |
| 184 | + for (const fw of frameworks) { |
| 185 | + lines.push(fields.map((key) => escapeCell(fw[key])).join(",")); |
| 186 | + } |
| 187 | + return `${lines.join("\n")}\n`; |
| 188 | +} |
| 189 | + |
| 190 | +export function renderMarkdownTable(frameworks: FrameworkRecord[]): string { |
| 191 | + const header = [ |
| 192 | + "| Framework | Category | Latest | GitHub Stars | npm Weekly | Rendering Model | SSR/SEO |", |
| 193 | + "| :--- | :--- | :--- | ---: | ---: | :--- | :---: |", |
| 194 | + ]; |
| 195 | + const rows = frameworks.map( |
| 196 | + (fw) => |
| 197 | + `| **${fw.name}** (\`${fw.npm_package}\`) | ${fw.category} | \`${fw.latest_version}\` | ${fw.stars.toLocaleString("en-US")} | ${formatCompactNumber(fw.npm_weekly_downloads)} | ${fw.rendering_model} | ${fw.ssr_seo_score}/5 |`, |
| 198 | + ); |
| 199 | + return [...header, ...rows].join("\n"); |
| 200 | +} |
| 201 | + |
| 202 | +function parseCliArgs(argv: string[]) { |
| 203 | + let useCase: UseCase | undefined; |
| 204 | + let seoCritical = false; |
| 205 | + let lowBundle = false; |
| 206 | + let lowLearningCurve = false; |
| 207 | + let category: FrameworkCategory | undefined; |
| 208 | + let jsonOutput = false; |
| 209 | + let exportCsvFlag = false; |
| 210 | + let limit = 5; |
| 211 | + const compare: string[] = []; |
| 212 | + |
| 213 | + for (let i = 0; i < argv.length; i += 1) { |
| 214 | + const arg = argv[i]; |
| 215 | + if (arg === "--use-case" && argv[i + 1]) { |
| 216 | + useCase = argv[++i] as UseCase; |
| 217 | + } else if (arg === "--seo-critical") { |
| 218 | + seoCritical = true; |
| 219 | + } else if (arg === "--low-bundle") { |
| 220 | + lowBundle = true; |
| 221 | + } else if (arg === "--low-learning-curve") { |
| 222 | + lowLearningCurve = true; |
| 223 | + } else if (arg === "--category" && argv[i + 1]) { |
| 224 | + category = argv[++i] as FrameworkCategory; |
| 225 | + } else if (arg === "--limit" && argv[i + 1]) { |
| 226 | + limit = Number.parseInt(argv[++i], 10) || 5; |
| 227 | + } else if (arg === "--json") { |
| 228 | + jsonOutput = true; |
| 229 | + } else if (arg === "--export-csv") { |
| 230 | + exportCsvFlag = true; |
| 231 | + } else if (arg === "--compare") { |
| 232 | + while (argv[i + 1] && !argv[i + 1].startsWith("--")) { |
| 233 | + compare.push(argv[++i].toLowerCase()); |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + return { useCase, seoCritical, lowBundle, lowLearningCurve, category, jsonOutput, exportCsvFlag, limit, compare }; |
| 238 | +} |
| 239 | + |
| 240 | +function main(): number { |
| 241 | + const args = parseCliArgs(process.argv.slice(2)); |
| 242 | + const dataset = loadDataset(); |
| 243 | + |
| 244 | + if (args.exportCsvFlag) { |
| 245 | + process.stdout.write(exportCsv(dataset.frameworks)); |
| 246 | + return 0; |
| 247 | + } |
| 248 | + |
| 249 | + if (args.compare.length > 0) { |
| 250 | + const wanted = new Set(args.compare); |
| 251 | + const selected = dataset.frameworks.filter((fw) => wanted.has(fw.id)); |
| 252 | + if (selected.length === 0) { |
| 253 | + console.error(`No matching frameworks found for: ${args.compare.join(", ")}`); |
| 254 | + return 1; |
| 255 | + } |
| 256 | + console.log(args.jsonOutput ? JSON.stringify(selected, null, 2) : renderMarkdownTable(selected)); |
| 257 | + return 0; |
| 258 | + } |
| 259 | + |
| 260 | + if (args.useCase) { |
| 261 | + if (!(args.useCase in USE_CASE_LABELS)) { |
| 262 | + console.error(`Unknown use-case: ${args.useCase}`); |
| 263 | + return 1; |
| 264 | + } |
| 265 | + const recs = recommendFrameworks(dataset, { |
| 266 | + useCase: args.useCase, |
| 267 | + seoCritical: args.seoCritical, |
| 268 | + lowBundle: args.lowBundle, |
| 269 | + lowLearningCurve: args.lowLearningCurve, |
| 270 | + category: args.category, |
| 271 | + limit: args.limit, |
| 272 | + }); |
| 273 | + if (args.jsonOutput) { |
| 274 | + console.log(JSON.stringify({ use_case: args.useCase, recommendations: recs }, null, 2)); |
| 275 | + return 0; |
| 276 | + } |
| 277 | + console.log(`Top Recommendations for: ${USE_CASE_LABELS[args.useCase]}\n`); |
| 278 | + recs.forEach((fw, index) => { |
| 279 | + const why = fw.match_reasons.length > 0 ? fw.match_reasons.join("; ") : fw.summary; |
| 280 | + console.log(`${index + 1}. ${fw.name} (${fw.latest_version}) — score ${fw.match_score} | ${why}`); |
| 281 | + }); |
| 282 | + return 0; |
| 283 | + } |
| 284 | + |
| 285 | + console.log( |
| 286 | + args.jsonOutput |
| 287 | + ? JSON.stringify(dataset, null, 2) |
| 288 | + : `Verified Frontend Frameworks (${dataset.metadata.verified_at}):\n\n${renderMarkdownTable(dataset.frameworks)}`, |
| 289 | + ); |
| 290 | + return 0; |
| 291 | +} |
| 292 | + |
| 293 | +const isMain = process.argv[1] && fileURLToPath(import.meta.url) === resolve(process.argv[1]); |
| 294 | +if (isMain) { |
| 295 | + process.exitCode = main(); |
| 296 | +} |
0 commit comments