mapcomplete/scripts/generateLicenseInfo.ts
2021-04-07 01:32:39 +02:00

147 lines
No EOL
4.5 KiB
TypeScript

import {Utils} from "../Utils";
Utils.runningFromConsole = true;
import {existsSync, mkdirSync, readdirSync, readFileSync, writeFile, writeFileSync, lstatSync} from "fs";
import {LicenseInfo} from "../Logic/Web/Wikimedia";
import {icon, Icon} from "leaflet";
interface SmallLicense {
path: string,
authors: string[],
license: string,
sources: string[]
}
/**
* Sweeps the entire 'assets/' (except assets/generated) directory for image files and any 'license_info.json'-file.
* Checks that the license info is included for each of them and generates a compiles license_info.json for those
*/
function readDirRecSync(path): string[] {
const result = []
for (const entry of readdirSync(path)) {
const fullEntry = path + "/" + entry
const stats = lstatSync(fullEntry)
if (stats.isDirectory()) {
// Subdirectory
// @ts-ignore
result.push(...readDirRecSync(fullEntry))
} else {
result.push(fullEntry)
}
}
return result;
}
function generateLicenseInfos(paths: string[]): SmallLicense[] {
const licenses = []
for (const path of paths) {
console.log("Reading info from " + path)
const parsed = JSON.parse(readFileSync(path, "UTF-8"))
if (Array.isArray(parsed)) {
const l: SmallLicense[] = parsed
for (const smallLicens of l) {
smallLicens.path = path.substring(0, path.length - "license_info.json".length) + smallLicens.path
}
licenses.push(...l)
} else {
const smallLicens: SmallLicense = parsed;
smallLicens.path = path.substring(0, 1 + path.lastIndexOf("/")) + smallLicens.path
licenses.push(smallLicens)
}
}
return licenses
}
function missingLicenseInfos(licenseInfos: SmallLicense[], allIcons: string[]) {
const missing = []
const knownPaths = new Set<string>()
for (const licenseInfo of licenseInfos) {
knownPaths.add(licenseInfo.path)
}
for (const iconPath of allIcons) {
if (iconPath.indexOf("license_info.json") >= 0) {
continue;
}
if (knownPaths.has(iconPath)) {
continue;
}
missing.push(iconPath)
}
return missing;
}
const prompt = require('prompt-sync')();
function promptLicenseFor(path): SmallLicense {
const author = prompt("What is the author for artwork " + path + "? (me == Pieter Vander Vennet/CC0, Q to quit) > ")
path = path.substring(path.lastIndexOf("/") + 1)
if (author == "me") {
return {
authors: ["Pieter Vander Vennet"],
path: path,
license: "CC0",
sources: []
}
}
if (author == "cf") {
return {
authors: ["Pieter Fiers", "Thibault Declercq", "Pierre Barban", "Joost Schouppe","Pieter Vander Vennet"],
path: path,
license: "CC-BY-SA",
sources: ["https://osoc.be/editions/2020/cyclofix"]
}
}
if (author == "gitte") {
return {
authors: ["Gitte Vande Graveele"],
path: path,
license: "CC-BY-SA",
sources: ["https://osoc.be/editions/2020/walk-by-brussels"]
}
}
if(author == "s"){
return null;
}
if (author == "Q" || author == "q" || author == "") {
throw "Quitting now!"
}
return {
authors: author.split(";"),
path: path,
license: prompt("What is the license for artwork " + path + "? > "),
sources: prompt("Where was this artwork found? > ").split(";")
}
}
function createLicenseInfoFor(path): void {
const li = promptLicenseFor(path);
if(li == null){
return;
}
writeFileSync(path + ".license_info.json", JSON.stringify(li, null, " "))
}
console.log("Checking and compiling license info")
const contents = readDirRecSync("./assets")
.filter(entry => entry.indexOf("./assets/generated") != 0)
const licensePaths = contents.filter(entry => entry.indexOf("license_info.json") >= 0)
const licenseInfos = generateLicenseInfos(licensePaths);
writeFileSync("./assets/generated/license_info.json", JSON.stringify(licenseInfos, null, " "))
const artwork = contents.filter(pth => pth.match(/(.svg|.png|.jpg)$/i) != null)
const missingLicenses = missingLicenseInfos(licenseInfos, artwork)
console.log(`There are ${missingLicenses.length} licenses missing.`)
let i = 1;
for (const missingLicens of missingLicenses) {
console.log(i + " / " + missingLicenses.length)
createLicenseInfoFor(missingLicens)
i++;
}