zed/styles/src/build_licenses.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

import * as fs from "fs"
import toml from "toml"
import { themes } from "./themes"
import { ThemeConfig } from "./common"
2023-05-30 16:12:31 +00:00
const ACCEPTED_LICENSES_FILE = `${__dirname}/../../script/licenses/zed-licenses.toml`
// Use the cargo-about configuration file as the source of truth for supported licenses.
2023-06-29 05:48:40 +00:00
function parse_accepted_toml(file: string): string[] {
const buffer = fs.readFileSync(file).toString()
const obj = toml.parse(buffer)
if (!Array.isArray(obj.accepted)) {
throw Error("Accepted license source is malformed")
}
return obj.accepted
}
2023-06-29 05:48:40 +00:00
function check_licenses(themes: ThemeConfig[]) {
for (const theme of themes) {
2023-06-29 15:41:51 +00:00
if (!theme.license_file) {
2023-06-07 16:40:49 +00:00
throw Error(`Theme ${theme.name} should have a LICENSE file`)
}
}
}
2023-06-29 05:48:40 +00:00
function generate_license_file(themes: ThemeConfig[]) {
check_licenses(themes)
for (const theme of themes) {
2023-06-29 15:41:51 +00:00
const license_text = fs.readFileSync(theme.license_file).toString()
2023-06-29 06:16:21 +00:00
write_license(theme.name, license_text, theme.license_url)
}
}
2023-06-29 05:48:40 +00:00
function write_license(
theme_name: string,
license_text: string,
license_url?: string
2023-05-30 16:12:31 +00:00
) {
process.stdout.write(
2023-06-29 05:48:40 +00:00
license_url
? `## [${theme_name}](${license_url})\n\n${license_text}\n********************************************************************************\n\n`
: `## ${theme_name}\n\n${license_text}\n********************************************************************************\n\n`
)
}
2023-06-29 05:48:40 +00:00
const accepted_licenses = parse_accepted_toml(ACCEPTED_LICENSES_FILE)
generate_license_file(themes)