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