import Script from "./Script" import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs" import { LayerConfigJson } from "../src/Models/ThemeConfig/Json/LayerConfigJson" import LayerConfig from "../src/Models/ThemeConfig/LayerConfig" import { DoesImageExist } from "../src/Models/ThemeConfig/Conversion/Validation" import { ExtractImages } from "../src/Models/ThemeConfig/Conversion/FixImages" import { ThemeConfigJson } from "../src/Models/ThemeConfig/Json/ThemeConfigJson" import { ConversionContext } from "../src/Models/ThemeConfig/Conversion/ConversionContext" import Constants from "../src/Models/Constants" import { main as downloadCommons } from "./downloadCommons" import { WikimediaImageProvider } from "../src/Logic/ImageProviders/WikimediaImageProvider" import { Utils } from "../src/Utils" import { GenerateLicenseInfo } from "./generateLicenseInfo" class ImportCustomTheme extends Script { constructor() { super("Given the path of a custom layer, will load the layer into mapcomplete as official") } async main(args: string[]) { const path = args[0] const layerconfig = JSON.parse(readFileSync(path, "utf-8")) const id = layerconfig.id const dirPath = "./assets/layers/" + id if (!existsSync(dirPath)) { mkdirSync(dirPath) } const imageFinder = new ExtractImages(true) const theme: ThemeConfigJson = { layers: [layerconfig], id: "dummy-theme-during-import", title: { en: "Dummy", }, icon: "./assets/svg/plus.svg", description: { en: "Dummy", }, } const usedImagesAll: { path: string context: string location: (string | number)[] }[] = imageFinder.convert(theme, ConversionContext.construct([], ["checking images"])) const usedImages = usedImagesAll .filter((img) => !img.path.startsWith("./assets")) .filter((img) => Constants.defaultPinIcons.indexOf(img.path) < 0) console.log(usedImages) const wm = WikimediaImageProvider.singleton for (const usedImage of usedImages) { if (usedImage.path.indexOf("wikimedia") >= 0) { const filename = WikimediaImageProvider.extractFileName(usedImage.path) console.log("Canonical URL is", filename) await downloadCommons([ dirPath, "https://commons.wikimedia.org/wiki/File:" + filename, ]) console.log("Used image context:", usedImage.context) const replaceAt = usedImage.location .slice(2) // We drop ".layer.0" as we put this in a dummy theme .map((breadcrumb) => isNaN(Number(breadcrumb)) ? breadcrumb : Number(breadcrumb) ) console.log("Replacement target location is", replaceAt) Utils.WalkPath(replaceAt, layerconfig, (_, path) => { console.log("Found", path, "to replace with", filename, "origvalue:", _) return `./assets/layers/${id}/${filename}` }) } } writeFileSync( "./assets/layers/" + id + "/" + id + ".json", JSON.stringify(layerconfig, null, " ") ) } } new ImportCustomTheme().run() new GenerateLicenseInfo().run()