From 703b66195f590143d24885eca0be29f4e7d54140 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Fri, 14 May 2021 17:37:21 +0200 Subject: [PATCH] Add robustness to download script --- scripts/ScriptUtils.ts | 50 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/scripts/ScriptUtils.ts b/scripts/ScriptUtils.ts index 3be6bca2f..fc54759a2 100644 --- a/scripts/ScriptUtils.ts +++ b/scripts/ScriptUtils.ts @@ -17,37 +17,43 @@ export default class ScriptUtils { } return result; } - - public static DownloadJSON(url) : Promise{ - return new Promise((resolve, reject) => { - https.get(url, (res) => { - 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){ - reject(e) - } - }); - }) + public static DownloadJSON(url): Promise { + return new Promise((resolve, reject) => { + try { + + + https.get(url, (res) => { + 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) { + reject(e) + } + }); + }) + } catch (e) { + reject(e) + } }) - + } public static sleep(ms) { - if(ms <= 0){ + if (ms <= 0) { process.stdout.write("\r \r") return; } return new Promise((resolve) => { - process.stdout.write("\r Sleeping for "+(ms/1000)+"s \r") + process.stdout.write("\r Sleeping for " + (ms / 1000) + "s \r") setTimeout(resolve, 1000); }).then(() => ScriptUtils.sleep(ms - 1000)); }