mapcomplete/scripts/ScriptUtils.ts

166 lines
5.8 KiB
TypeScript
Raw Normal View History

import * as fs from "fs";
2022-03-08 04:09:03 +01:00
import {existsSync, lstatSync, readdirSync, readFileSync} from "fs";
import {Utils} from "../Utils";
import * as https from "https";
import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson";
import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson";
import xml2js from 'xml2js';
2021-04-10 03:18:32 +02:00
export default class ScriptUtils {
public static fixUtils() {
Utils.externalDownloadFunction = ScriptUtils.Download
}
2021-05-20 12:27:33 +02:00
public static readDirRecSync(path, maxDepth = 999): string[] {
2021-04-10 03:18:32 +02:00
const result = []
if (maxDepth <= 0) {
2021-05-20 12:27:33 +02:00
return []
}
2021-04-10 03:18:32 +02:00
for (const entry of readdirSync(path)) {
const fullEntry = path + "/" + entry
const stats = lstatSync(fullEntry)
if (stats.isDirectory()) {
// Subdirectory
// @ts-ignore
2021-05-20 12:27:33 +02:00
result.push(...ScriptUtils.readDirRecSync(fullEntry, maxDepth - 1))
2021-04-10 03:18:32 +02:00
} else {
result.push(fullEntry)
}
}
return result;
}
2021-05-14 17:37:21 +02:00
public static DownloadFileTo(url, targetFilePath: string): void {
console.log("Downloading ", url, "to", targetFilePath)
https.get(url, (res) => {
const filePath = fs.createWriteStream(targetFilePath);
res.pipe(filePath);
filePath.on('finish', () => {
filePath.close();
console.log('Download Completed');
})
})
}
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}> {
return new Promise((resolve, reject) => {
2021-05-14 17:37:21 +02:00
try {
headers = headers ?? {}
2021-08-22 15:53:25 +02:00
headers.accept = "application/json"
console.log(" > ScriptUtils.DownloadJson(", url, ")")
2021-07-03 21:39:30 +02:00
const urlObj = new URL(url)
https.get({
host: urlObj.host,
2021-07-18 18:29:15 +02:00
path: urlObj.pathname + urlObj.search,
2021-09-04 18:59:51 +02:00
2021-07-03 21:39:30 +02:00
port: urlObj.port,
2021-08-22 15:53:25 +02:00
headers: headers
2021-07-03 21:39:30 +02:00
}, (res) => {
2021-05-14 17:37:21 +02:00
const parts: string[] = []
res.setEncoding('utf8');
res.on('data', function (chunk) {
// @ts-ignore
parts.push(chunk)
});
res.addListener('end', function () {
resolve({content: parts.join("")})
2021-05-14 17:37:21 +02:00
});
})
} catch (e) {
reject(e)
}
})
2021-05-14 17:37:21 +02:00
}
2021-09-04 18:59:51 +02:00
public static erasableLog(...text) {
2021-09-04 18:59:51 +02:00
process.stdout.write("\r " + text.join(" ") + " \r")
}
public static sleep(ms) {
2021-05-14 17:37:21 +02:00
if (ms <= 0) {
process.stdout.write("\r \r")
return;
}
return new Promise((resolve) => {
2021-05-14 17:37:21 +02:00
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)
2021-09-04 18:59:51 +02:00
.filter(path => path.indexOf(".proto.json") < 0)
.filter(path => path.indexOf("license_info.json") < 0)
.map(path => {
try {
2021-07-07 15:19:05 +02:00
const contents = readFileSync(path, "UTF8")
2021-09-04 18:59:51 +02:00
if (contents === "") {
throw "The file " + path + " is empty, did you properly save?"
2021-07-07 15:19:05 +02:00
}
2021-09-04 18:59:51 +02:00
2021-07-07 15:19:05 +02:00
const parsed = JSON.parse(contents);
return {parsed: parsed, path: path}
} catch (e) {
console.error("Could not parse file ", "./assets/layers/" + path, "due to ", 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 {
2021-07-07 15:19:05 +02:00
const contents = readFileSync(path, "UTF8");
2021-09-04 18:59:51 +02:00
if (contents === "") {
throw "The file " + path + " is empty, did you properly save?"
2021-07-07 15:19:05 +02:00
}
const parsed = JSON.parse(contents);
2021-05-19 23:40:55 +02:00
return {parsed: parsed, path: path}
} catch (e) {
console.error("Could not read file ", path, "due to ", e)
throw e
}
});
}
2021-04-10 03:18:32 +02:00
2021-09-13 01:18:57 +02:00
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>{
2022-03-08 04:09:03 +01:00
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"]);
})
}
2021-04-10 03:18:32 +02:00
}