From a3551d1861b7deb80f2f6825afd80d750205b17d Mon Sep 17 00:00:00 2001 From: Bruno Date: Mon, 17 Aug 2020 10:45:38 -0300 Subject: [PATCH] handles overwriting --- README.md | 24 ++++++++++++++++++++++++ lib/downloader.js | 35 ++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 07dfc26..874eb23 100644 --- a/README.md +++ b/README.md @@ -346,6 +346,30 @@ downloader('path/to-binary', function error(err, done) { }) ``` +This script parses a couple of flags from `argv`: + +* `--platform=windows` forces downloading the Windows version of youtube-dl. +* `--overwrite` overwrites the existing youtube-dl executable if it exists. + + +### Update (promise version) + +If you are using promises there's now a promise version. + +``` js +const downloader = require('youtube-dl/lib/downloaderPromise') + +downloader('path/to-binary') +.then((message) => { + console.log(message); +}).catch((err) => { + console.log("err", err); + exit(1); +}); + +``` + + ### Environment Variables Youtube-dl looks for certain environment variables to aid its operations. If Youtube-dl doesn't find them in the environment during the installation step, a lowercased variant of these variables will be used from the [npm config](https://docs.npmjs.com/cli/config) or [yarn config](https://yarnpkg.com/lang/en/docs/cli/config/). diff --git a/lib/downloader.js b/lib/downloader.js index e285fed..567772b 100644 --- a/lib/downloader.js +++ b/lib/downloader.js @@ -8,6 +8,7 @@ const fs = require('fs') const [, , ...flags] = process.argv const isWin = flags.includes('--platform=windows') || require('./util').isWin +const isOverwrite = flags.includes('--overwrite') // First, look for the download link. let dir, filePath @@ -44,16 +45,19 @@ function download (url, callback) { status = new Error('Response Error: ' + res.statusCode) return } - downloadFile.pipe(fs.createWriteStream(filePath, { mode: 493 })) + + const outputStream = fs.createWriteStream(filePath, { mode: 493 }); + outputStream.on( + 'close', + function end() { + callback(status, newVersion); + }); + downloadFile.pipe(outputStream); }) downloadFile.on('error', function error (err) { callback(err) }) - - downloadFile.on('end', function end () { - callback(status, newVersion) - }) }) } @@ -61,8 +65,10 @@ const exec = path => (isWin ? path + '.exe' : path) function createBase (binDir) { dir = binDir || defaultBin - mkdirp.sync(dir) - if (binDir) mkdirp.sync(defaultBin) + if (!fs.existsSync(dir)) { + mkdirp.sync(dir) + if (binDir) mkdirp.sync(defaultBin) + } filePath = path.join(dir, exec('youtube-dl')) } @@ -74,6 +80,21 @@ function downloader (binDir, callback) { createBase(binDir) + // handle overwritin + if (fs.existsSync(filePath)) { + if (!isOverwrite) { + return callback("File exists"); + } + else { + try { + fs.unlinkSync(filePath); + } + catch (e) { + callback(e); + } + } + } + download(url, function error (err, newVersion) { if (err) return callback(err) fs.writeFileSync(