import { PlatformPackager } from "app-builder-lib"
import { getLicenseFiles } from "app-builder-lib/out/util/license"
import { log } from "builder-util"
import { dmgLicenseFromJSON } from "dmg-license"
import { readFile, readJson } from "fs-extra"
import { load } from "js-yaml"
import { getLicenseButtonsFile } from "./licenseButtons"
type LicenseConfig = {
$schema: string
body: any[]
labels: any[]
}
export async function addLicenseToDmg(packager: PlatformPackager<any>, dmgPath: string): Promise<LicenseConfig | null> {
const licenseFiles = await getLicenseFiles(packager)
if (licenseFiles.length === 0) {
return null
}
const licenseButtonFiles = await getLicenseButtonsFile(packager)
packager.debugLogger.add("dmg.licenseFiles", licenseFiles)
packager.debugLogger.add("dmg.licenseButtons", licenseButtonFiles)
const jsonFile: LicenseConfig = {
$schema: "https://github.com/argv-minus-one/dmg-license/raw/master/schema.json",
body: [],
labels: [],
}
for (const file of licenseFiles) {
jsonFile.body.push({
file: file.file,
lang: file.langWithRegion.replace("_", "-"),
})
}
for (const button of licenseButtonFiles) {
const filepath = button.file
const label = filepath.endsWith(".yml") ? load(await readFile(filepath, "utf-8")) : await readJson(filepath)
if (label.description) {
label.message = label.description
delete label.description
}
jsonFile.labels.push(
Object.assign(
{
lang: button.langWithRegion.replace("_", "-"),
},
label
)
)
}
await dmgLicenseFromJSON(dmgPath, jsonFile, {
onNonFatalError: log.warn.bind(log),
})
return jsonFile
}