From 7cfca5d1eb9f63c3455d1f4d9958c5a1da673b03 Mon Sep 17 00:00:00 2001 From: rasheed Date: Wed, 17 Apr 2024 23:58:26 +0100 Subject: [PATCH] Extracts status code checks into a separate function to simplify the main download logic --- extra/download-dist.js | 48 ++++++++---------------------------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/extra/download-dist.js b/extra/download-dist.js index fa7912d2..d508b6b9 100644 --- a/extra/download-dist.js +++ b/extra/download-dist.js @@ -23,45 +23,15 @@ function download(url) { console.log(url); https.get(url, (response) => { - if (response.statusCode === 200) { - console.log("Extracting dist..."); - -function handleDirectoryCleanup() { - if (fs.existsSync("./dist")) { - if (fs.existsSync("./dist-backup")) { - rmSync("./dist-backup", { recursive: true }); - } - fs.renameSync("./dist", "./dist-backup"); +function checkResponseStatus(response, tarStream) { + if (response.statusCode === 200) { + console.log("Extracting dist..."); + handleDirectoryCleanup(); + response.pipe(tarStream); + } else if (response.statusCode === 302) { + download(response.headers.location); + } else { + console.log("dist not found"); } } - } - - const tarStream = tar.x({ - cwd: "./", - }); - - tarStream.on("close", () => { - if (fs.existsSync("./dist-backup")) { - rmSync("./dist-backup", { - recursive: true - }); - } - console.log("Done"); - process.exit(0); - }); - - tarStream.on("error", () => { - if (fs.existsSync("./dist-backup")) { - fs.renameSync("./dist-backup", "./dist"); - } - console.error("Error from tarStream"); - }); - - response.pipe(tarStream); - } else if (response.statusCode === 302) { - download(response.headers.location); - } else { - console.log("dist not found"); - } - }); }