Skip to content

Commit

Permalink
added check existence before download
Browse files Browse the repository at this point in the history
  • Loading branch information
abgnpr committed Sep 14, 2020
1 parent b3ff3be commit eda8a8c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions download.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs')
const chalk = require("chalk");

/** Returns today's day & date in dash separated format e.g Mon-Aug-31-2020 */
Expand All @@ -21,6 +22,11 @@ module.exports = async function download(
downloadsPath,
showErrorLogs = false
) {
let downloadFile = `${downloadsPath}/${downloadItem.name.split(" ").join("")}_${dateStamp()}`;

if (fs.existsSync(downloadFile))
return chalk.blue("\nAlready exists: ") + downloadItem.name;

console.log(chalk.yellow("\nDownloading: ") + downloadItem.name);
try {
await page.goto(downloadItem.url);
Expand All @@ -29,9 +35,7 @@ module.exports = async function download(
downloadItem.steps(page), // `undefined` if all good
]);
await download.path();
await download.saveAs(
`${downloadsPath}/${downloadItem.name.split(" ").join("")}_${dateStamp()}`
);
await download.saveAs(downloadFile);
return chalk.green("\nFinished: ") + downloadItem.name;

} catch (err) {
Expand Down

0 comments on commit eda8a8c

Please sign in to comment.