2021-05-19 23:31:00 +02:00
|
|
|
import {lstatSync, readdirSync, readFileSync} from "fs";
|
2021-06-23 02:41:30 +02:00
|
|
|
import {Utils} from "../Utils";
|
2021-07-03 21:39:30 +02:00
|
|
|
|
2021-06-23 02:41:30 +02:00
|
|
|
Utils.runningFromConsole = true
|
2021-04-22 03:30:46 +02:00
|
|
|
import * as https from "https";
|
2021-05-19 23:31:00 +02:00
|
|
|
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
|
2021-05-19 23:40:55 +02:00
|
|
|
import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson";
|
2021-06-22 14:21:32 +02:00
|
|
|
import * as fs from "fs";
|
2021-06-23 02:41:30 +02:00
|
|
|
|
2021-04-10 03:18:32 +02:00
|
|
|
|
|
|
|
export default class ScriptUtils {
|
2021-06-22 14:21:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
public static fixUtils() {
|
|
|
|
Utils.externalDownloadFunction = ScriptUtils.DownloadJSON
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 = []
|
2021-06-22 14:21:32 +02:00
|
|
|
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
|
|
|
|
2021-06-22 14:21:32 +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');
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:37:21 +02:00
|
|
|
public static DownloadJSON(url): Promise<any> {
|
2021-05-14 02:25:30 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-05-14 17:37:21 +02:00
|
|
|
try {
|
|
|
|
|
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-07-03 21:39:30 +02:00
|
|
|
port: urlObj.port,
|
|
|
|
headers: {
|
|
|
|
"accept": "application/json"
|
|
|
|
}
|
|
|
|
}, (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 () {
|
|
|
|
const result = parts.join("")
|
|
|
|
try {
|
|
|
|
resolve(JSON.parse(result))
|
|
|
|
} catch (e) {
|
2021-07-18 18:51:34 +02:00
|
|
|
console.error("Could not parse the following as JSON:", result)
|
2021-05-14 17:37:21 +02:00
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
reject(e)
|
|
|
|
}
|
2021-04-22 03:30:46 +02:00
|
|
|
})
|
2021-05-14 17:37:21 +02:00
|
|
|
|
2021-04-22 03:30:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static sleep(ms) {
|
2021-05-14 17:37:21 +02:00
|
|
|
if (ms <= 0) {
|
2021-05-14 02:25:30 +02:00
|
|
|
process.stdout.write("\r \r")
|
|
|
|
return;
|
|
|
|
}
|
2021-04-22 03:30:46 +02:00
|
|
|
return new Promise((resolve) => {
|
2021-05-14 17:37:21 +02:00
|
|
|
process.stdout.write("\r Sleeping for " + (ms / 1000) + "s \r")
|
2021-05-14 02:25:30 +02:00
|
|
|
setTimeout(resolve, 1000);
|
|
|
|
}).then(() => ScriptUtils.sleep(ms - 1000));
|
2021-04-22 03:30:46 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 23:31:00 +02:00
|
|
|
public static getLayerFiles(): { parsed: LayerConfigJson, path: string }[] {
|
|
|
|
return ScriptUtils.readDirRecSync("./assets/layers")
|
|
|
|
.filter(path => path.indexOf(".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")
|
|
|
|
if(contents === ""){
|
|
|
|
throw "The file "+path+" is empty, did you properly save?"
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsed = JSON.parse(contents);
|
2021-05-19 23:31:00 +02:00
|
|
|
return {parsed: parsed, path: path}
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Could not parse file ", "./assets/layers/" + path, "due to ", e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-22 14:21:32 +02:00
|
|
|
public static getThemeFiles(): { parsed: LayoutConfigJson, path: string }[] {
|
2021-05-19 23:31:00 +02:00
|
|
|
return ScriptUtils.readDirRecSync("./assets/themes")
|
|
|
|
.filter(path => path.endsWith(".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");
|
|
|
|
if(contents === ""){
|
|
|
|
throw "The file "+path+" is empty, did you properly save?"
|
|
|
|
}
|
|
|
|
const parsed = JSON.parse(contents);
|
2021-05-19 23:40:55 +02:00
|
|
|
return {parsed: parsed, path: path}
|
2021-05-19 23:31:00 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error("Could not read file ", path, "due to ", e)
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:18:32 +02:00
|
|
|
|
|
|
|
}
|