this repo has no description
1/**
2 * Low-level file I/O for the data pipeline.
3 *
4 * This module only handles reading and writing raw files — no domain logic.
5 * For dictionary, subject, ordering, or sentence helpers, see the sibling modules.
6 */
7
8import { parse } from '@std/csv/parse'
9import { stringify } from '@std/csv/stringify'
10import stringifyJSON from 'json-stringify-pretty-compact'
11
12/** Root directory for data source files (TSVs, CSVs, JSON sources). */
13export const DATA_ROOT = './data/'
14
15/** Output directory for generated app JSON files served at runtime. */
16export const APP_ROOT = './www/static/gen/'
17
18/** Reads a TSV from `data/`. Skips the header row; returns records keyed by column name. */
19export function readTsv(input: string): Record<string, string>[] {
20 const text = Deno.readTextFileSync(DATA_ROOT + input)
21 return parse(text, {
22 separator: '\t',
23 lazyQuotes: true,
24 skipFirstRow: true,
25 }) as Record<string, string>[]
26}
27
28/** Reads a CSV from `data/`. Skips the header row; returns records keyed by column name. */
29export function readCsv(input: string): Record<string, string>[] {
30 const text = Deno.readTextFileSync(DATA_ROOT + input)
31 return parse(text, {
32 lazyQuotes: true,
33 skipFirstRow: true,
34 }) as Record<string, string>[]
35}
36
37/** Reads and parses a JSON file from `data/`. */
38export function readJson<T = unknown>(input: string): T {
39 return JSON.parse(Deno.readTextFileSync(DATA_ROOT + input)) as T
40}
41
42/** Writes rows as a TSV to `data/`. Column order follows the `columns` array. */
43export function writeTsv(input: string, columns: string[], data: unknown[]): void {
44 Deno.writeTextFileSync(
45 DATA_ROOT + input,
46 stringify(data as Record<string, string>[], { columns, separator: '\t' }),
47 )
48}
49
50/** Writes content as JSON to `www/static/gen/`. Pass `minify=true` for production builds. */
51export function writeAppJson(path: string, content: unknown, minify = false): void {
52 Deno.writeTextFileSync(APP_ROOT + path, minify ? JSON.stringify(content) : stringifyJSON(content))
53}