-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopulate-catalog.ts
More file actions
224 lines (200 loc) · 6.57 KB
/
populate-catalog.ts
File metadata and controls
224 lines (200 loc) · 6.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
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
/**
* CLI tool: populate-catalog
*
* Usage:
* # Run the scraper first, then insert into DB:
* pnpm run cli tools populate-catalog --scraperPath ~/Documents/major-scraper
*
* # Skip scraping, just read existing output:
* pnpm run cli tools populate-catalog --scraperPath ~/Documents/major-scraper --skipScrape
*
* # Scrape specific years with verbose output:
* pnpm run cli tools populate-catalog --scraperPath ~/Documents/major-scraper --years 2024,2025 -v
*/
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
import path from "node:path";
import { execSync } from "node:child_process";
import { defineCommand } from "citty";
import { brandIntro, isVerbose, p, pc, setVerbosity } from "../ui";
import { getDb } from "@sneu/db/pg";
import { catalogMajorsT, catalogMinorsT } from "@sneu/db/schema";
export function chunk<T>(array: T[], size: number): T[][] {
const chunks: T[][] = [];
for (let i = 0; i < array.length; i += size) {
chunks.push(array.slice(i, i + size));
}
return chunks;
}
/**
* Recursively find all files matching a given filename within a directory.
*/
function findFiles(dir: string, filename: string): string[] {
const results: string[] = [];
if (!existsSync(dir)) {
return results;
}
for (const entry of readdirSync(dir)) {
const full = path.join(dir, entry);
if (statSync(full).isDirectory()) {
results.push(...findFiles(full, filename));
} else if (entry === filename) {
results.push(full);
}
}
return results;
}
/**
* Extract year/college/name from a path like:
* .../degrees/major/{year}/{college}/{name}/parsed.initial.json
*/
function extractPathParts(filePath: string): {
year: string;
college: string;
name: string;
} {
const dir = path.dirname(filePath);
const name = path.basename(dir);
const college = path.basename(path.dirname(dir));
const year = path.basename(path.dirname(path.dirname(dir)));
return { year, college, name };
}
export default defineCommand({
meta: {
name: "populate-catalog",
description:
"populate catalog_majors and catalog_minors from scraper output",
},
args: {
scraperPath: {
type: "string",
required: true,
description: "path to the major-scraper repo",
},
years: {
type: "string",
default: "current",
description: 'comma-separated years to scrape (default: "current")',
},
skipScrape: {
type: "boolean",
alias: "s",
default: false,
description: "skip running the scraper, just read existing output",
},
verbose: {
alias: "v",
type: "boolean",
description: "show detailed output",
},
},
async run({ args }) {
setVerbosity({ verbose: args.verbose });
brandIntro("tools populate-catalog");
const scraperPath = path.resolve(args.scraperPath);
// 1. Validate scraperPath
if (
!existsSync(scraperPath) ||
!existsSync(path.join(scraperPath, "package.json"))
) {
p.log.error(
`Invalid scraper path: ${pc.dim(scraperPath)} (missing package.json)`,
);
process.exit(1);
}
p.log.info(`Scraper repo: ${pc.dim(scraperPath)}`);
// 2. Run scraper unless --skip-scrape
if (!args.skipScrape) {
const cmd = `pnpm scrape:all ${args.years}`;
p.log.info(`Running: ${pc.dim(cmd)}`);
try {
execSync(cmd, {
cwd: scraperPath,
stdio: "inherit",
});
} catch {
p.log.error("Scraper failed — aborting.");
process.exit(1);
}
} else {
p.log.info("Skipping scraper run (--skip-scrape)");
}
const db = getDb(process.env.DATABASE_URL!, true);
// 3. Process majors
const majorDir = path.join(scraperPath, "degrees", "major");
const majorFiles = findFiles(majorDir, "parsed.initial.json");
p.log.info(`Found ${pc.bold(String(majorFiles.length))} major files`);
const majorValues: (typeof catalogMajorsT.$inferInsert)[] = [];
for (const file of majorFiles) {
const raw = JSON.parse(readFileSync(file, "utf-8"));
const { year, college, name } = extractPathParts(file);
// Look for matching template
const templatePath = path.join(
scraperPath,
"templates",
year,
college,
name,
"template.json",
);
let templateOptions: Record<string, unknown> = {};
if (existsSync(templatePath)) {
const templateRaw = JSON.parse(readFileSync(templatePath, "utf-8"));
// Strip metadata fields, keep the rest
const { ...rest } = templateRaw;
templateOptions = rest;
if (isVerbose()) {
p.log.info(
` Template matched: ${pc.dim(`${year}/${college}/${name}`)}`,
);
}
}
majorValues.push({
name: raw.name,
totalCreditsRequired: raw.totalCreditsRequired,
yearVersion: raw.yearVersion,
requirementSections: raw.requirementSections,
concentrationOptions: raw.concentrations?.concentrationOptions ?? [],
minConcentrationOptions: raw.concentrations?.minOptions ?? 0,
templateOptions,
});
if (isVerbose()) {
p.log.info(` Major: ${pc.dim(raw.name)} (${year})`);
}
}
// Batched insert for majors
let majorsInserted = 0;
for (const batch of chunk(majorValues, 500)) {
await db.insert(catalogMajorsT).values(batch);
majorsInserted += batch.length;
}
// 4. Process minors
const minorDir = path.join(scraperPath, "degrees", "minor");
const minorFiles = findFiles(minorDir, "parsed.initial.json");
p.log.info(`Found ${pc.bold(String(minorFiles.length))} minor files`);
const minorValues: (typeof catalogMinorsT.$inferInsert)[] = [];
for (const file of minorFiles) {
const raw = JSON.parse(readFileSync(file, "utf-8"));
minorValues.push({
name: raw.name,
totalCreditsRequired: raw.totalCreditsRequired,
yearVersion: raw.yearVersion,
requirementSections: raw.requirementSections,
concentrationOptions: raw.concentrations?.concentrationOptions ?? [],
});
if (isVerbose()) {
const { year } = extractPathParts(file);
p.log.info(` Minor: ${pc.dim(raw.name)} (${year})`);
}
}
// Batched insert for minors
let minorsInserted = 0;
for (const batch of chunk(minorValues, 500)) {
await db.insert(catalogMinorsT).values(batch);
minorsInserted += batch.length;
}
// 5. Epic Summary!!!!!!!!!!!
p.outro(
`Inserted ${pc.bold(String(majorsInserted))} majors, ${pc.bold(String(minorsInserted))} minors`,
);
},
});