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, theme.licenseUrl, licenseText)
34    }
35}
36
37function writeLicense(
38    themeName: string,
39    licenseUrl: string,
40    licenseText: String
41) {
42    process.stdout.write(
43        `## [${themeName}](${licenseUrl})\n\n${licenseText}\n********************************************************************************\n\n`
44    )
45}
46
47const acceptedLicenses = parseAcceptedToml(ACCEPTED_LICENSES_FILE)
48generateLicenseFile(themes)