buildLicenses.ts

 1import * as fs from "fs"
 2import toml from "toml"
 3import { schemeMeta } from "./colorSchemes"
 4import { MetaAndLicense } from "./themes/common/colorScheme"
 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(
22    schemeMetaWithLicense: MetaAndLicense[],
23    licenses: string[]
24) {
25    for (const { meta } of schemeMetaWithLicense) {
26        // FIXME: Add support for conjunctions and conditions
27        if (licenses.indexOf(meta.license.SPDX) < 0) {
28            throw Error(
29                `License for theme ${meta.name} (${meta.license.SPDX}) is not supported`
30            )
31        }
32    }
33}
34
35function generateLicenseFile(schemeMetaWithLicense: MetaAndLicense[]) {
36    for (const { meta, licenseFile } of schemeMetaWithLicense) {
37        const licenseText = fs.readFileSync(licenseFile).toString()
38        writeLicense(meta.name, meta.url, licenseText)
39    }
40}
41
42function writeLicense(
43    themeName: string,
44    themeUrl: string,
45    licenseText: String
46) {
47    process.stdout.write(
48        `## [${themeName}](${themeUrl})\n\n${licenseText}\n********************************************************************************\n\n`
49    )
50}
51
52const acceptedLicenses = parseAcceptedToml(ACCEPTED_LICENSES_FILE)
53checkLicenses(schemeMeta, acceptedLicenses)
54generateLicenseFile(schemeMeta)