2023-02-25 16:46:33 +00:00
|
|
|
import * as fs from "fs"
|
|
|
|
import toml from "toml"
|
|
|
|
import { schemeMeta } from "./colorSchemes"
|
|
|
|
import { Meta } from "./themes/common/colorScheme"
|
|
|
|
import https from "https"
|
|
|
|
import crypto from "crypto"
|
2023-01-27 02:15:34 +00:00
|
|
|
|
2023-01-27 01:27:42 +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.
|
|
|
|
function parseAcceptedToml(file: string): string[] {
|
2023-02-25 16:46:33 +00:00
|
|
|
let buffer = fs.readFileSync(file).toString()
|
2023-01-27 01:27:42 +00:00
|
|
|
|
2023-02-25 16:46:33 +00:00
|
|
|
let obj = toml.parse(buffer)
|
2023-01-27 01:27:42 +00:00
|
|
|
|
2023-02-25 16:46:33 +00:00
|
|
|
if (!Array.isArray(obj.accepted)) {
|
|
|
|
throw Error("Accepted license source is malformed")
|
|
|
|
}
|
2023-01-27 01:27:42 +00:00
|
|
|
|
2023-02-25 16:46:33 +00:00
|
|
|
return obj.accepted
|
2023-01-27 01:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkLicenses(schemeMeta: Meta[], licenses: string[]) {
|
2023-02-25 16:46:33 +00:00
|
|
|
for (let meta of schemeMeta) {
|
|
|
|
// FIXME: Add support for conjuctions and conditions
|
|
|
|
if (licenses.indexOf(meta.license.SPDX) < 0) {
|
|
|
|
throw Error(
|
|
|
|
`License for theme ${meta.name} (${meta.license.SPDX}) is not supported`
|
|
|
|
)
|
|
|
|
}
|
2023-01-27 01:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-25 16:46:33 +00:00
|
|
|
function getLicenseText(
|
|
|
|
schemeMeta: Meta[],
|
|
|
|
callback: (meta: Meta, license_text: string) => void
|
|
|
|
) {
|
|
|
|
for (let meta of schemeMeta) {
|
|
|
|
// The following copied from the example code on nodejs.org:
|
|
|
|
// https://nodejs.org/api/http.html#httpgetoptions-callback
|
|
|
|
https
|
|
|
|
.get(meta.license.https_url, (res) => {
|
|
|
|
const { statusCode } = res
|
2023-01-27 01:27:42 +00:00
|
|
|
|
2023-02-25 16:46:33 +00:00
|
|
|
if (statusCode < 200 || statusCode >= 300) {
|
|
|
|
throw new Error(
|
|
|
|
`Failed to fetch license for: ${meta.name}, Status Code: ${statusCode}`
|
|
|
|
)
|
|
|
|
}
|
2023-01-27 01:27:42 +00:00
|
|
|
|
2023-02-25 16:46:33 +00:00
|
|
|
res.setEncoding("utf8")
|
|
|
|
let rawData = ""
|
|
|
|
res.on("data", (chunk) => {
|
|
|
|
rawData += chunk
|
|
|
|
})
|
|
|
|
res.on("end", () => {
|
|
|
|
const hash = crypto
|
|
|
|
.createHash("sha256")
|
|
|
|
.update(rawData)
|
|
|
|
.digest("hex")
|
|
|
|
if (meta.license.license_checksum == hash) {
|
|
|
|
callback(meta, rawData)
|
|
|
|
} else {
|
|
|
|
throw Error(
|
|
|
|
`Checksum for ${meta.name} did not match file downloaded from ${meta.license.https_url}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.on("error", (e) => {
|
|
|
|
throw e
|
|
|
|
})
|
|
|
|
}
|
2023-01-27 01:27:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 02:15:34 +00:00
|
|
|
function writeLicense(schemeMeta: Meta, text: String) {
|
2023-02-25 16:46:33 +00:00
|
|
|
process.stdout.write(
|
|
|
|
`## [${schemeMeta.name}](${schemeMeta.url})\n\n${text}\n********************************************************************************\n\n`
|
|
|
|
)
|
2023-01-27 01:27:42 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 16:46:33 +00:00
|
|
|
const accepted_licenses = parseAcceptedToml(accepted_licenses_file)
|
2023-01-27 01:27:42 +00:00
|
|
|
checkLicenses(schemeMeta, accepted_licenses)
|
|
|
|
|
|
|
|
getLicenseText(schemeMeta, (meta, text) => {
|
2023-02-25 16:46:33 +00:00
|
|
|
writeLicense(meta, text)
|
|
|
|
})
|