forked from matiasngf/frontend-masters-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-experiments.ts
More file actions
69 lines (56 loc) · 1.86 KB
/
get-experiments.ts
File metadata and controls
69 lines (56 loc) · 1.86 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
import { readdir, writeFile } from "fs/promises"
import { join } from "path"
interface Experiment {
url: string
name: string
}
async function getExperiments(): Promise<Experiment[]> {
const experimentsDir = join(process.cwd(), "src/app/(experiments)")
const entries = await readdir(experimentsDir, { withFileTypes: true })
const experiments: Experiment[] = []
for (const entry of entries) {
if (!entry.isDirectory()) continue
const folderName = entry.name
try {
// Fallback: format folder name nicely
const experimentName = folderName
.split("-")
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
experiments.push({
url: `/${folderName}`,
name: experimentName,
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
// If page.tsx doesn't exist, skip this folder
console.warn(`Skipping ${folderName}: page.tsx not found`)
}
}
return experiments.sort((a, b) => {
// Extract numeric prefix from URL (e.g., "/1-gsap-basics" -> 1)
const getNumber = (url: string) => {
const match = url.match(/^\/(\d+)-/)
return match ? parseInt(match[1], 10) : Infinity
}
const numA = getNumber(a.url)
const numB = getNumber(b.url)
// Sort by numeric prefix first, then by name if numbers are equal
if (numA !== numB) {
return numA - numB
}
return a.name.localeCompare(b.name)
})
}
async function main() {
try {
const experiments = await getExperiments()
const outputPath = join(process.cwd(), "experiments.json")
await writeFile(outputPath, JSON.stringify(experiments, null, 2), "utf-8")
console.log(`Generated ${experiments.length} experiments in ${outputPath}`)
} catch (error) {
console.error("Error generating experiments:", error)
process.exit(1)
}
}
main()