Generate layer overview now only recompiles files that need to be recompiled

This commit is contained in:
Pieter Vander Vennet 2022-07-06 13:58:56 +02:00
parent 063d7e4637
commit 8c036e159f
4 changed files with 204 additions and 137 deletions

View file

@ -45,13 +45,103 @@ export default class ScriptUtils {
})
}
private static async DownloadJSON(url: string, headers?: any): Promise<any>{
public static erasableLog(...text) {
process.stdout.write("\r " + text.join(" ") + " \r")
}
public static sleep(ms) {
if (ms <= 0) {
process.stdout.write("\r \r")
return;
}
return new Promise((resolve) => {
process.stdout.write("\r Sleeping for " + (ms / 1000) + "s \r")
setTimeout(resolve, 1000);
}).then(() => ScriptUtils.sleep(ms - 1000));
}
public static getLayerPaths(): string[] {
return ScriptUtils.readDirRecSync("./assets/layers")
.filter(path => path.indexOf(".json") > 0)
.filter(path => path.indexOf(".proto.json") < 0)
.filter(path => path.indexOf("license_info.json") < 0)
}
public static getLayerFiles(): { parsed: LayerConfigJson, path: string }[] {
return ScriptUtils.readDirRecSync("./assets/layers")
.filter(path => path.indexOf(".json") > 0)
.filter(path => path.indexOf(".proto.json") < 0)
.filter(path => path.indexOf("license_info.json") < 0)
.map(path => {
try {
const contents = readFileSync(path, "UTF8")
if (contents === "") {
throw "The file " + path + " is empty, did you properly save?"
}
const parsed = JSON.parse(contents);
return {parsed, path}
} catch (e) {
console.error("Could not parse file ", "./assets/layers/" + path, "due to ", e)
throw e
}
})
}
public static getThemePaths(): string[] {
return ScriptUtils.readDirRecSync("./assets/themes")
.filter(path => path.endsWith(".json") && !path.endsWith(".proto.json"))
.filter(path => path.indexOf("license_info.json") < 0)
}
public static getThemeFiles(): { parsed: LayoutConfigJson, path: string }[] {
return this.getThemePaths()
.map(path => {
try {
const contents = readFileSync(path, "UTF8");
if (contents === "") {
throw "The file " + path + " is empty, did you properly save?"
}
const parsed = JSON.parse(contents);
return {parsed: parsed, path: path}
} catch (e) {
console.error("Could not read file ", path, "due to ", e)
throw e
}
});
}
public static TagInfoHistogram(key: string): Promise<{
data: { count: number, value: string, fraction: number }[]
}> {
const url = `https://taginfo.openstreetmap.org/api/4/key/values?key=${key}&filter=all&lang=en&sortname=count&sortorder=desc&page=1&rp=17&qtype=value`
return ScriptUtils.DownloadJSON(url)
}
public static async ReadSvg(path: string): Promise<any> {
if (!existsSync(path)) {
throw "File not found: " + path
}
const root = await xml2js.parseStringPromise(readFileSync(path, "UTF8"))
return root.svg
}
public static async ReadSvgSync(path: string, callback: ((svg: any) => void)): Promise<any> {
xml2js.parseString(readFileSync(path, "UTF8"), {async: false}, (err, root) => {
if (err) {
throw err
}
callback(root["svg"]);
})
}
private static async DownloadJSON(url: string, headers?: any): Promise<any> {
const data = await ScriptUtils.Download(url, headers);
return JSON.parse(data.content)
}
private static Download(url, headers?: any): Promise<{content: string}> {
private static Download(url, headers?: any): Promise<{ content: string }> {
return new Promise((resolve, reject) => {
try {
headers = headers ?? {}
@ -83,84 +173,4 @@ export default class ScriptUtils {
}
public static erasableLog(...text) {
process.stdout.write("\r " + text.join(" ") + " \r")
}
public static sleep(ms) {
if (ms <= 0) {
process.stdout.write("\r \r")
return;
}
return new Promise((resolve) => {
process.stdout.write("\r Sleeping for " + (ms / 1000) + "s \r")
setTimeout(resolve, 1000);
}).then(() => ScriptUtils.sleep(ms - 1000));
}
public static getLayerFiles(): { parsed: LayerConfigJson, path: string }[] {
return ScriptUtils.readDirRecSync("./assets/layers")
.filter(path => path.indexOf(".json") > 0)
.filter(path => path.indexOf(".proto.json") < 0)
.filter(path => path.indexOf("license_info.json") < 0)
.map(path => {
try {
const contents = readFileSync(path, "UTF8")
if (contents === "") {
throw "The file " + path + " is empty, did you properly save?"
}
const parsed = JSON.parse(contents);
return {parsed, path}
} catch (e) {
console.error("Could not parse file ", "./assets/layers/" + path, "due to ", e)
throw e
}
})
}
public static getThemeFiles(): { parsed: LayoutConfigJson, path: string }[] {
return ScriptUtils.readDirRecSync("./assets/themes")
.filter(path => path.endsWith(".json") && !path.endsWith(".proto.json"))
.filter(path => path.indexOf("license_info.json") < 0)
.map(path => {
try {
const contents = readFileSync(path, "UTF8");
if (contents === "") {
throw "The file " + path + " is empty, did you properly save?"
}
const parsed = JSON.parse(contents);
return {parsed: parsed, path: path}
} catch (e) {
console.error("Could not read file ", path, "due to ", e)
throw e
}
});
}
public static TagInfoHistogram(key: string): Promise<{
data: { count: number, value: string, fraction: number }[]
}> {
const url = `https://taginfo.openstreetmap.org/api/4/key/values?key=${key}&filter=all&lang=en&sortname=count&sortorder=desc&page=1&rp=17&qtype=value`
return ScriptUtils.DownloadJSON(url)
}
public static async ReadSvg(path: string): Promise<any>{
if(!existsSync(path)){
throw "File not found: "+path
}
const root = await xml2js.parseStringPromise(readFileSync(path, "UTF8"))
return root.svg
}
public static async ReadSvgSync(path: string, callback: ((svg: any) => void)): Promise<any>{
xml2js.parseString(readFileSync(path, "UTF8"),{async: false} , (err, root) => {
if(err){
throw err
}
callback(root["svg"]);
})
}
}