Skip to content

Commit

Permalink
fix: add download cleanup on error:
Browse files Browse the repository at this point in the history
  • Loading branch information
lightpohl committed May 9, 2020
1 parent a8c6583 commit 978db00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
36 changes: 22 additions & 14 deletions bin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,28 @@ let download = async ({ url, outputPath, key, archive }) => {
return;
}

await pipeline(
got
.stream(url)
.on("downloadProgress", (progress) => {
printProgress(progress);
})
.on("end", () => {
endPrintProgress();
}),
fs.createWriteStream(outputPath)
);

if (key && archive && !getIsInArchive({ key, archive })) {
writeToArchive({ key, archive });
try {
await pipeline(
got
.stream(url)
.on("downloadProgress", (progress) => {
printProgress(progress);
})
.on("end", () => {
endPrintProgress();

if (key && archive && !getIsInArchive({ key, archive })) {
writeToArchive({ key, archive });
}
}),
fs.createWriteStream(outputPath)
);
} catch (error) {
if (fs.existsSync(outputPath)) {
fs.unlinkSync(outputPath);
}

throw error;
}
};

Expand Down
2 changes: 1 addition & 1 deletion bin/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const logError = (msg, error) => {
console.error(msg);

if (error) {
console.error(error);
console.error(error.message);
}
};

Expand Down

0 comments on commit 978db00

Please sign in to comment.