-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.cts
More file actions
133 lines (110 loc) · 3.57 KB
/
node.cts
File metadata and controls
133 lines (110 loc) · 3.57 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
import fs = require('node:fs')
import path = require('node:path')
type RepoRecord = {
id: string
owner: string
name: string
defaultBranch: string
updatedAt: string
stars: number
languages: string[]
tags: string[]
}
type RepoIndex = {
byOwner: Map<string, RepoRecord[]>
featured: RepoRecord[]
}
function readJsonFile<T>(filePath: string): T {
return JSON.parse(fs.readFileSync(filePath, 'utf8')) as T
}
function normalizeRecord(row: Partial<RepoRecord>): RepoRecord {
const owner = String(row.owner ?? 'unknown')
const name = String(row.name ?? 'repo')
return {
id: String(row.id ?? `${owner}/${name}`),
owner,
name,
defaultBranch: String(row.defaultBranch ?? 'main'),
updatedAt: String(row.updatedAt ?? new Date(0).toISOString()),
stars: Number(row.stars ?? 0),
languages: Array.isArray(row.languages) ? row.languages.map((item) => String(item)) : [],
tags: Array.isArray(row.tags) ? row.tags.map((item) => String(item)) : [],
}
}
function listSnapshotFiles(rootDir: string): string[] {
return fs
.readdirSync(rootDir, { withFileTypes: true })
.filter((entry) => entry.isFile() && entry.name.endsWith('.json'))
.map((entry) => path.join(rootDir, entry.name))
.sort((left, right) => left.localeCompare(right))
}
function loadSnapshots(rootDir: string): RepoRecord[] {
const files = listSnapshotFiles(rootDir)
const records: RepoRecord[] = []
for (const filePath of files) {
const rows = readJsonFile<Partial<RepoRecord>[]>(filePath)
if (!Array.isArray(rows)) {
continue
}
for (const row of rows) {
records.push(normalizeRecord(row))
}
}
return records.sort((left, right) => right.updatedAt.localeCompare(left.updatedAt))
}
function buildIndex(records: RepoRecord[]): RepoIndex {
const byOwner = new Map<string, RepoRecord[]>()
for (const record of records) {
const bucket = byOwner.get(record.owner) ?? []
bucket.push(record)
byOwner.set(record.owner, bucket)
}
for (const bucket of byOwner.values()) {
bucket.sort((left, right) => left.name.localeCompare(right.name))
}
const featured = records
.filter((record) => record.tags.includes('featured') || record.stars >= 500)
.sort((left, right) => right.stars - left.stars)
.slice(0, 12)
return { byOwner, featured }
}
function toMarkdown(index: RepoIndex): string {
const lines: string[] = []
lines.push('# Repo Snapshot')
lines.push('')
lines.push('## Featured')
lines.push('')
for (const record of index.featured) {
const summary = `${record.owner}/${record.name} (${record.defaultBranch})`
const langs = record.languages.join(', ') || 'n/a'
lines.push(`- ${summary} - ${record.stars} stars - ${langs}`)
}
lines.push('')
lines.push('## Owners')
lines.push('')
for (const [owner, repos] of [...index.byOwner.entries()].sort((a, b) => a[0].localeCompare(b[0]))) {
lines.push(`### ${owner}`)
for (const repo of repos) {
lines.push(`- ${repo.name} (${repo.defaultBranch}) updated ${repo.updatedAt}`)
}
lines.push('')
}
return lines.join('\n')
}
function writeReport(snapshotDir: string, outputFile: string): string {
const records = loadSnapshots(snapshotDir)
const report = toMarkdown(buildIndex(records))
fs.writeFileSync(outputFile, report)
return outputFile
}
if (require.main === module) {
const [snapshotDir = '.', outputFile = path.resolve('repo-report.md')] = process.argv.slice(2)
const target = writeReport(snapshotDir, outputFile)
console.log(`wrote ${target}`)
}
export = {
loadSnapshots,
buildIndex,
toMarkdown,
writeReport,
}