mapcomplete/scripts/fixImagesInTagRenderings.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2.5 KiB
TypeScript
Raw Normal View History

import { readFileSync, writeFileSync } from "fs"
import { DesugaringStep } from "../Models/ThemeConfig/Conversion/Conversion"
import { LayerConfigJson } from "../Models/ThemeConfig/Json/LayerConfigJson"
import { Utils } from "../Utils"
import Translations from "../UI/i18n/Translations"
class ConvertImagesToIcon extends DesugaringStep<LayerConfigJson> {
private _iconClass: string
constructor(iconClass: string) {
super(
"Searches for images in the 'then' path, removes the <img> block and extracts the image itself a 'icon'",
[],
"ConvertImagesToIcon"
)
this._iconClass = iconClass
}
convert(
json: LayerConfigJson,
context: string
): { result: LayerConfigJson; errors?: string[]; warnings?: string[]; information?: string[] } {
const information = []
const errors = []
json = Utils.Clone(json)
Utils.WalkPath(["tagRenderings", "mappings"], json, (mapping) => {
const then = Translations.T(mapping.then)
const images = Utils.Dedup(then.ExtractImages())
if (images.length == 0) {
return mapping
}
if (images.length > 1) {
errors.push(
"The mapping " + mapping.then + " has multiple images: " + images.join(", ")
)
2022-09-08 21:40:48 +02:00
}
information.push("Replaced image " + images[0])
const replaced = then.OnEveryLanguage((s) => {
2022-09-08 21:40:48 +02:00
return s
2022-02-18 04:09:43 +01:00
.replace(/(<div [^>]*>)?<img [^>]*> ?/, "")
.replace(/<\/div>$/, "")
2022-09-08 21:40:48 +02:00
.trim()
})
mapping.then = replaced.translations
mapping.icon = { path: images[0], class: this._iconClass }
return mapping
2022-09-08 21:40:48 +02:00
})
return {
information,
result: json,
}
}
}
/**
* One-of script to load one layer.json-file and rewrite all tagrenderings
*/
function main() {
let args = [...process.argv]
args.splice(0, 2)
const path = args[0]
const iconClass = args[1] ?? "small"
const targetFile = args[2] ?? path + ".autoconverted.json"
2023-01-15 23:28:02 +01:00
const parsed = JSON.parse(readFileSync(path, { encoding: "utf8" }))
const converted = new ConvertImagesToIcon(iconClass).convertStrict(
parsed,
"While running the fixImagesInTagRenderings-script"
)
2022-02-18 03:22:25 +01:00
writeFileSync(targetFile, JSON.stringify(converted, null, " "))
console.log("Written fixed version to " + targetFile)
}
main()