mapcomplete/scripts/lint.ts

115 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-09-26 18:21:29 +02:00
/*
* This script reads all theme and layer files and reformats them inplace
* Use with caution, make a commit beforehand!
*/
import ScriptUtils from "./ScriptUtils";
import {writeFileSync} from "fs";
2021-09-26 19:56:40 +02:00
import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson";
import LineRenderingConfigJson from "../Models/ThemeConfig/Json/LineRenderingConfigJson";
2021-09-26 18:21:29 +02:00
2021-09-26 19:56:40 +02:00
/**
* In place fix
*/
function fixLayerConfig(config: LayerConfigJson): void {
if(config["overpassTags"]){
config.source.osmTags = config["overpassTags"]
delete config["overpassTags"]
}
if (config.tagRenderings !== undefined) {
for (const tagRendering of config.tagRenderings) {
if (tagRendering["#"] !== undefined) {
tagRendering["id"] = tagRendering["#"]
delete tagRendering["#"]
}
if (tagRendering["id"] === undefined) {
if (tagRendering["freeform"]?.key !== undefined) {
tagRendering["id"] = config.id + "-" + tagRendering["freeform"]["key"]
}
}
}
2021-09-26 19:59:51 +02:00
}
2021-10-21 21:41:45 +02:00
2021-10-26 01:39:29 +02:00
if (config.mapRendering === undefined || config.id !== "sidewalks") {
// This is a legacy format, lets create a pointRendering
2021-10-21 21:41:45 +02:00
let location: ("point" | "centroid")[] = ["point"]
let wayHandling: number = config["wayHandling"] ?? 0
if (wayHandling === 2) {
location = ["point", "centroid"]
2021-09-26 18:21:29 +02:00
}
config.mapRendering = [
{
icon: config["icon"],
2021-10-21 21:41:45 +02:00
iconBadges: config["iconOverlays"],
label: config["label"],
iconSize: config["iconSize"],
location,
rotation: config["rotation"]
2021-09-26 18:21:29 +02:00
}
]
2021-10-21 21:41:45 +02:00
if (wayHandling !== 1) {
const lineRenderConfig = <LineRenderingConfigJson>{
color: config["color"],
width: config["width"],
dashArray: config["dashArray"]
}
2021-10-21 21:41:45 +02:00
if (Object.keys(lineRenderConfig).length > 0) {
config.mapRendering.push(lineRenderConfig)
}
}
2021-10-21 21:41:45 +02:00
2021-10-28 00:17:43 +02:00
delete config["color"]
delete config["width"]
delete config["dashArray"]
delete config["icon"]
delete config["iconOverlays"]
delete config["label"]
delete config["iconSize"]
delete config["rotation"]
2021-10-21 21:41:45 +02:00
delete config["wayHandling"]
2021-09-26 18:21:29 +02:00
}
2021-10-21 21:41:45 +02:00
for (const mapRenderingElement of config.mapRendering) {
if (mapRenderingElement["iconOverlays"] !== undefined) {
mapRenderingElement["iconBadges"] = mapRenderingElement["iconOverlays"]
}
for (const overlay of mapRenderingElement["iconBadges"] ?? []) {
if (overlay["badge"] !== true) {
console.log("Warning: non-overlay element for ", config.id)
}
delete overlay["badge"]
}
}
2021-09-26 19:56:40 +02:00
}
const layerFiles = ScriptUtils.getLayerFiles();
for (const layerFile of layerFiles) {
fixLayerConfig(layerFile.parsed)
2021-09-26 18:21:29 +02:00
writeFileSync(layerFile.path, JSON.stringify(layerFile.parsed, null, " "))
2021-09-26 19:56:40 +02:00
}
2021-09-26 19:59:51 +02:00
2021-09-26 19:56:40 +02:00
const themeFiles = ScriptUtils.getThemeFiles()
for (const themeFile of themeFiles) {
2021-09-26 19:58:11 +02:00
for (const layerConfig of themeFile.parsed.layers ?? []) {
if (typeof layerConfig === "string" || layerConfig["builtin"] !== undefined) {
2021-09-26 19:56:40 +02:00
continue
}
// @ts-ignore
fixLayerConfig(layerConfig)
}
2021-10-21 21:41:45 +02:00
if (themeFile.parsed["roamingRenderings"] !== undefined && themeFile.parsed["roamingRenderings"].length == 0) {
delete themeFile.parsed["roamingRenderings"]
}
2021-10-21 21:41:45 +02:00
2021-09-26 19:59:51 +02:00
writeFileSync(themeFile.path, JSON.stringify(themeFile.parsed, null, " "))
2021-09-26 19:58:11 +02:00
}
//*/