Refactoring: refactoring of all Conversions

This commit is contained in:
Pieter Vander Vennet 2023-10-11 04:16:52 +02:00
parent 4e8dfc0026
commit f2863cdf17
38 changed files with 1177 additions and 1269 deletions

View file

@ -13,7 +13,9 @@ export default abstract class Script {
ScriptUtils.fixUtils()
const args = [...process.argv]
args.splice(0, 2)
this.main(args).then((_) => console.log("All done"))
this.main(args)
.then((_) => console.log("All done"))
.catch((e) => console.log("ERROR:", e))
}
public printHelp() {

View file

@ -14,13 +14,18 @@ import {
import { Translation } from "../src/UI/i18n/Translation"
import { PrepareLayer } from "../src/Models/ThemeConfig/Conversion/PrepareLayer"
import { PrepareTheme } from "../src/Models/ThemeConfig/Conversion/PrepareTheme"
import { DesugaringContext } from "../src/Models/ThemeConfig/Conversion/Conversion"
import {
ConversionContext,
DesugaringContext,
} from "../src/Models/ThemeConfig/Conversion/Conversion"
import { Utils } from "../src/Utils"
import Script from "./Script"
import { AllSharedLayers } from "../src/Customizations/AllSharedLayers"
import { parse as parse_html } from "node-html-parser"
import { ExtraFunctions } from "../src/Logic/ExtraFunctions"
import { QuestionableTagRenderingConfigJson } from "../src/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
import LayerConfig from "../src/Models/ThemeConfig/LayerConfig"
import PointRenderingConfig from "../src/Models/ThemeConfig/PointRenderingConfig"
// This scripts scans 'src/assets/layers/*.json' for layer definition files and 'src/assets/themes/*.json' for theme definition files.
// It spits out an overview of those to be used to load them
@ -307,7 +312,7 @@ class LayerOverviewUtils extends Script {
layers: ScriptUtils.getLayerFiles().map((f) => f.parsed),
themes: ScriptUtils.getThemeFiles().map((f) => f.parsed),
},
"GenerateLayerOverview:"
ConversionContext.construct([], [])
)
if (AllSharedLayers.getSharedLayersConfigs().size == 0) {
@ -329,8 +334,13 @@ class LayerOverviewUtils extends Script {
} catch (e) {
throw "Could not parse or read file " + sharedLayerPath
}
const context = "While building builtin layer " + sharedLayerPath
const fixed = prepLayer.convertStrict(parsed, context)
if (parsed === undefined) {
throw "File " + sharedLayerPath + " yielded undefined"
}
const fixed = prepLayer.convertStrict(
parsed,
ConversionContext.construct([sharedLayerPath], ["PrepareLayer"])
)
if (!fixed.source) {
console.error(sharedLayerPath, "has no source configured:", fixed)
@ -346,7 +356,10 @@ class LayerOverviewUtils extends Script {
}
const validator = new ValidateLayer(sharedLayerPath, true, doesImageExist)
validator.convertStrict(fixed, context)
validator.convertStrict(
fixed,
ConversionContext.construct([sharedLayerPath], ["PrepareLayer"])
)
return fixed
}
@ -386,12 +399,35 @@ class LayerOverviewUtils extends Script {
const fixed = this.parseLayer(doesImageExist, prepLayer, sharedLayerPath)
if (sharedLayers.has(fixed.id)) {
throw "There are multiple layers with the id " + fixed.id
throw "There are multiple layers with the id " + fixed.id + ", " + sharedLayerPath
}
sharedLayers.set(fixed.id, fixed)
recompiledLayers.push(fixed.id)
{
// Add a summary of the icon
const layerConfig = new LayerConfig(fixed, "generating_icon")
const pointRendering: PointRenderingConfig = layerConfig.mapRendering.find((pr) =>
pr.location.has("point")
)
const defaultTags = layerConfig.GetBaseTags()
fixed["_layerIcon"] = Utils.NoNull(
(pointRendering?.marker ?? []).map((i) => {
const icon = i.icon?.GetRenderValue(defaultTags)?.txt
if (!icon) {
return undefined
}
const result = { icon }
const c = i.color?.GetRenderValue(defaultTags)?.txt
if (c) {
result["color"] = c
}
return result
})
)
}
this.writeLayer(fixed)
}
@ -594,16 +630,25 @@ class LayerOverviewUtils extends Script {
recompiledThemes.push(themeFile.id)
new PrevalidateTheme().convertStrict(themeFile, themePath)
new PrevalidateTheme().convertStrict(
themeFile,
ConversionContext.construct([themePath], ["PrepareLayer"])
)
try {
themeFile = new PrepareTheme(convertState).convertStrict(themeFile, themePath)
themeFile = new PrepareTheme(convertState).convertStrict(
themeFile,
ConversionContext.construct([themePath], ["PrepareLayer"])
)
new ValidateThemeAndLayers(
new DoesImageExist(licensePaths, existsSync, knownTagRenderings),
themePath,
true,
knownTagRenderings
).convertStrict(themeFile, themePath)
).convertStrict(
themeFile,
ConversionContext.construct([themePath], ["PrepareLayer"])
)
if (themeFile.icon.endsWith(".svg")) {
try {

View file

@ -19,3 +19,10 @@ report.mapcomplete.org {
to http://127.0.0.1:2600
}
}
studio.mapcomplete.org {
reverse_proxy {
to http://127.0.0.1:1235
}
}

View file

@ -1,7 +1,6 @@
import * as fs from "node:fs"
import * as http from "node:http"
import * as path from "node:path"
import { ReadStream } from "fs"
import ScriptUtils from "./ScriptUtils"
const PORT = 1235
@ -26,15 +25,10 @@ async function prepareFile(url: string): Promise<string> {
const paths = [STATIC_PATH, url]
if (url.endsWith("/")) paths.push("index.html")
const filePath = path.join(...paths)
const exists = fs.existsSync(filePath)
console.log("Checking", filePath, exists)
const found = exists
if (!found) {
return null
if (fs.existsSync(filePath)) {
return fs.readFileSync(filePath, "utf8")
}
const streamPath = filePath
const ext = path.extname(streamPath).substring(1).toLowerCase()
return fs.readFileSync(streamPath, "utf8")
return null
}
http.createServer(async (req, res) => {
@ -61,7 +55,7 @@ http.createServer(async (req, res) => {
fs.mkdirSync(dir)
}
}
req.pipe(fs.createWriteStream(STATIC_PATH + paths.join("/") + ".new.json"))
req.pipe(fs.createWriteStream(STATIC_PATH + paths.join("/")))
res.writeHead(200, { "Content-Type": MIME_TYPES.html })
res.write("<html><body>OK</body></html>", "utf8")
res.end()