1import * as fs from "fs";
2import toml from "toml";
3import {
4 schemeMeta
5} from "./colorSchemes";
6import { Meta } from "./themes/common/colorScheme";
7import https from "https";
8import crypto from "crypto";
9
10const accepted_licenses_file = `${__dirname}/../../script/licenses/zed-licenses.toml`
11
12// Use the cargo-about configuration file as the source of truth for supported licenses.
13function parseAcceptedToml(file: string): string[] {
14 let buffer = fs.readFileSync(file).toString();
15
16 let obj = toml.parse(buffer);
17
18 if (!Array.isArray(obj.accepted)) {
19 throw Error("Accepted license source is malformed")
20 }
21
22 return obj.accepted
23}
24
25
26function checkLicenses(schemeMeta: Meta[], licenses: string[]) {
27 for (let meta of schemeMeta) {
28 // FIXME: Add support for conjuctions and conditions
29 if (licenses.indexOf(meta.license.SPDX) < 0) {
30 throw Error(`License for theme ${meta.name} (${meta.license.SPDX}) is not supported`)
31 }
32 }
33}
34
35
36function getLicenseText(schemeMeta: Meta[], callback: (meta: Meta, license_text: string) => void) {
37 for (let meta of schemeMeta) {
38 // The following copied from the example code on nodejs.org:
39 // https://nodejs.org/api/http.html#httpgetoptions-callback
40 https.get(meta.license.https_url, (res) => {
41 const { statusCode } = res;
42
43 if (statusCode < 200 || statusCode >= 300) {
44 throw new Error('Failed to fetch license file.\n' +
45 `Status Code: ${statusCode}`);
46 }
47
48 res.setEncoding('utf8');
49 let rawData = '';
50 res.on('data', (chunk) => { rawData += chunk; });
51 res.on('end', () => {
52 const hash = crypto.createHash('sha256').update(rawData).digest('hex');
53 if (meta.license.license_checksum == hash) {
54 callback(meta, rawData)
55 } else {
56 throw Error(`Checksum for ${meta.name} did not match file downloaded from ${meta.license.https_url}`)
57 }
58 });
59 }).on('error', (e) => {
60 throw e
61 });
62 }
63}
64
65function writeLicense(schemeMeta: Meta, text: String) {
66 process.stdout.write(`## [${schemeMeta.name}](${schemeMeta.url})\n\n${text}\n********************************************************************************\n\n`)
67}
68
69const accepted_licenses = parseAcceptedToml(accepted_licenses_file);
70checkLicenses(schemeMeta, accepted_licenses)
71
72getLicenseText(schemeMeta, (meta, text) => {
73 writeLicense(meta, text)
74});