forked from MapComplete/MapComplete
Scripts(nsi): stabilize and make scripts more log-friendly
This commit is contained in:
parent
53106cc0bf
commit
2b10c715b0
5 changed files with 71 additions and 25 deletions
|
@ -7,7 +7,11 @@ import { LayerConfigJson } from "../src/Models/ThemeConfig/Json/LayerConfigJson"
|
|||
import xml2js from "xml2js"
|
||||
|
||||
export default class ScriptUtils {
|
||||
public static fixUtils() {
|
||||
|
||||
public static verbose = true
|
||||
|
||||
public static fixUtils(verbose = true) {
|
||||
ScriptUtils.verbose = verbose
|
||||
Utils.externalDownloadFunction = ScriptUtils.Download
|
||||
}
|
||||
|
||||
|
@ -54,16 +58,26 @@ export default class ScriptUtils {
|
|||
}
|
||||
|
||||
public static DownloadFileTo(url, targetFilePath: string): Promise<void> {
|
||||
ScriptUtils.erasableLog("Downloading", url, "to", targetFilePath)
|
||||
return new Promise<void>((resolve) => {
|
||||
https.get(url, (res) => {
|
||||
if (this.verbose) {
|
||||
ScriptUtils.erasableLog("Downloading", url, "to", targetFilePath)
|
||||
}
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const req = https.get(url, (res) => {
|
||||
if (res.statusCode === 429) {
|
||||
console.error(`RATE LIMITED on ${url}`)
|
||||
ScriptUtils.sleep(50000).then(() => reject("rate limited"))
|
||||
}
|
||||
if (res.statusCode !== 200) {
|
||||
req.destroy()
|
||||
return reject(new Error(`HTTP ${res.statusCode} for ${url}`))
|
||||
}
|
||||
const filePath = fs.createWriteStream(targetFilePath)
|
||||
res.pipe(filePath)
|
||||
filePath.on("finish", () => {
|
||||
filePath.close()
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}).on("error", (e) => reject(e))
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -222,7 +236,9 @@ export default class ScriptUtils {
|
|||
if (!headers.Accept) {
|
||||
headers.accept ??= "application/json"
|
||||
}
|
||||
ScriptUtils.erasableLog(" > ScriptUtils.Download(", url, ")")
|
||||
if (ScriptUtils.verbose) {
|
||||
ScriptUtils.erasableLog(" > ScriptUtils.Download(", url, ")")
|
||||
}
|
||||
const urlObj = new URL(url)
|
||||
const request = https.get(
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue