buildLicenses.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 parseAcceptedToml(file: string): string[] {
10    let buffer = fs.readFileSync(file).toString()
11
12    let 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 checkLicenses(themes: ThemeConfig[]) {
22    for (const theme of themes) {
23        if (!theme.licenseFile) {
24            throw Error(`Theme ${theme.name} should have a LICENSE file`)
25        }
26    }
27}
28
29function generateLicenseFile(themes: ThemeConfig[]) {
30    checkLicenses(themes)
31    for (const theme of themes) {
32        const licenseText = fs.readFileSync(theme.licenseFile).toString()
33        writeLicense(theme.name, licenseText, theme.licenseUrl)
34    }
35}
36
37function writeLicense(
38    themeName: string,
39    licenseText: string,
40    licenseUrl?: string
41) {
42    process.stdout.write(
43        licenseUrl
44            ? `## [${themeName}](${licenseUrl})\n\n${licenseText}\n********************************************************************************\n\n`
45            : `## ${themeName}\n\n${licenseText}\n********************************************************************************\n\n`
46    )
47}
48
49const acceptedLicenses = parseAcceptedToml(ACCEPTED_LICENSES_FILE)
50generateLicenseFile(themes)