api/scripts/build-data.ts

34 lines · 1116 bytes

5 symbols in this file
 1#!/usr/bin/env bun
 2// Read awesome-privacy.yml, parse, write a runtime JSON module
 3import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'
 4import { resolve, dirname } from 'node:path'
 5import { fileURLToPath } from 'node:url'
 6import yaml from 'js-yaml'
 7
 8const here = dirname(fileURLToPath(import.meta.url))
 9const ymlPath =
10  process.env.AWESOME_PRIVACY_YML ?? resolve(here, '../../awesome-privacy.yml')
11const outPath = resolve(here, '../src/generated/data.json')
12
13// Pull source YAML (local path or fall back to GitHub raw)
14const loadYaml = async (): Promise<string> => {
15  try {
16    return readFileSync(ymlPath, 'utf8')
17  } catch {
18    const url =
19      'https://raw.githubusercontent.com/Lissy93/awesome-privacy/main/awesome-privacy.yml'
20    const res = await fetch(url)
21    if (!res.ok) throw new Error(`Failed to fetch yml from ${url}`)
22    return await res.text()
23  }
24}
25
26const main = async () => {
27  const text = await loadYaml()
28  const data = yaml.load(text)
29  mkdirSync(dirname(outPath), { recursive: true })
30  writeFileSync(outPath, JSON.stringify(data))
31  console.log(`Wrote ${outPath}`)
32}
33
34main()