build_licenses.ts

 1import * as fs from "fs"
 2import toml from "toml"
 3import { themes } from "./themes"
 4import { ThemeConfig } from "./common"
 5
 6const ACCEPTED_LICENSES_FILE = `${__dirname}/../../script/licenses/zed-licenses.toml`
 7
 8// Use the cargo-about configuration file as the source of truth for supported licenses.
 9function parse_accepted_toml(file: string): string[] {
10    const buffer = fs.readFileSync(file).toString()
11
12    const obj = toml.parse(buffer)
13
14    if (!Array.isArray(obj.accepted)) {
15        throw Error("Accepted license source is malformed")
16    }
17
18    return obj.accepted
19}
20
21function check_licenses(themes: ThemeConfig[]) {
22    for (const theme of themes) {
23        if (!theme.license_file) {
24            throw Error(`Theme ${theme.name} should have a LICENSE file`)
25        }
26    }
27}
28
29function generate_license_file(themes: ThemeConfig[]) {
30    check_licenses(themes)
31    for (const theme of themes) {
32        const license_text = fs.readFileSync(theme.license_file).toString()
33        write_license(theme.name, license_text, theme.license_url)
34    }
35}
36
37function write_license(
38    theme_name: string,
39    license_text: string,
40    license_url?: string
41) {
42    process.stdout.write(
43        license_url
44            ? `## [${theme_name}](${license_url})\n\n${license_text}\n********************************************************************************\n\n`
45            : `## ${theme_name}\n\n${license_text}\n********************************************************************************\n\n`
46    )
47}
48
49const accepted_licenses = parse_accepted_toml(ACCEPTED_LICENSES_FILE)
50generate_license_file(themes)