forked from MapComplete/MapComplete
Wrote a script to autofix issues with themes, applied this on the hailhydrant theme
This commit is contained in:
parent
dec565fc12
commit
1c902ca9c5
14 changed files with 910 additions and 53 deletions
63
scripts/fixTheme.ts
Normal file
63
scripts/fixTheme.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
|
||||
/*
|
||||
* This script attempt to automatically fix some basic issues when a theme from the custom generator is loaded
|
||||
*/
|
||||
import {Utils} from "../Utils"
|
||||
Utils.runningFromConsole = true;
|
||||
import {readFileSync, writeFileSync} from "fs";
|
||||
import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson";
|
||||
import {Layer} from "leaflet";
|
||||
import LayerConfig from "../Customizations/JSON/LayerConfig";
|
||||
import SmallLicense from "../Models/smallLicense";
|
||||
|
||||
if(process.argv.length == 2){
|
||||
console.log("USAGE: ts-node scripts/fixTheme <path to theme>")
|
||||
throw "No path specified"
|
||||
}
|
||||
|
||||
const path = process.argv[2]
|
||||
console.log("Fixing up ", path)
|
||||
|
||||
const themeConfigJson : LayoutConfigJson = JSON.parse(readFileSync(path, "UTF8"))
|
||||
|
||||
const linuxHints = []
|
||||
const licenses : SmallLicense[] = []
|
||||
|
||||
const replacements: {source: string, destination: string}[] = []
|
||||
|
||||
for (const layerConfigJson of themeConfigJson.layers) {
|
||||
if(typeof (layerConfigJson) === "string"){
|
||||
continue;
|
||||
}
|
||||
if(layerConfigJson["overpassTags"] !== undefined){
|
||||
const tags = layerConfigJson["overpassTags"];
|
||||
layerConfigJson["overpassTags"] = undefined;
|
||||
layerConfigJson["source"] = { osmTags : tags}
|
||||
}
|
||||
// @ts-ignore
|
||||
const layerConfig = new LayerConfig(layerConfigJson, true)
|
||||
const images : string[] = Array.from(layerConfig.ExtractImages())
|
||||
const remoteImages = images.filter(img => img.startsWith("http"))
|
||||
for (const remoteImage of remoteImages) {
|
||||
linuxHints.push("wget " + remoteImage)
|
||||
const imgPath = remoteImage.substring(remoteImage.lastIndexOf("/") + 1)
|
||||
licenses.push({
|
||||
path: imgPath,
|
||||
license: "",
|
||||
authors: [],
|
||||
sources: [remoteImage]
|
||||
})
|
||||
replacements.push({source: remoteImage, destination: imgPath})
|
||||
}
|
||||
}
|
||||
|
||||
let fixedThemeJson = JSON.stringify(themeConfigJson, null , " ")
|
||||
for (const replacement of replacements) {
|
||||
fixedThemeJson = fixedThemeJson.replace(new RegExp(replacement.source, "g"), replacement.destination)
|
||||
}
|
||||
|
||||
const dir = path.substring(0, path.lastIndexOf("/"))
|
||||
writeFileSync(dir + "/generated.license_info.json", JSON.stringify(licenses, null, " "))
|
||||
writeFileSync(dir + "/fix_script_"+path.replace(/\//g,"_")+".sh", linuxHints.join("\n"))
|
||||
writeFileSync(path+".autofixed.json", fixedThemeJson)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue