diff --git a/README.md b/README.md index e4a6cb8..9cc0538 100644 --- a/README.md +++ b/README.md @@ -369,6 +369,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, if you don't pass a function as second argument: + +``` js +const downloader = require('youtube-dl/lib/downloader') + +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..1268afa 100644 --- a/lib/downloader.js +++ b/lib/downloader.js @@ -4,10 +4,12 @@ const request = require('request') const mkdirp = require('mkdirp') const path = require('path') const fs = require('fs') +const util = require('util') 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 +46,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 +66,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')) } @@ -71,9 +78,27 @@ function downloader (binDir, callback) { callback = binDir binDir = null } + else if (!callback) { + return util.promisify(downloader)(binDir) + } 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( diff --git a/scripts/download.js b/scripts/download.js index 71f208c..830b917 100644 --- a/scripts/download.js +++ b/scripts/download.js @@ -1,6 +1,7 @@ var downloader = require('../lib/downloader') -downloader(function error (err, done) { - if (err) return console.log(err.stack) - console.log(done) -}) +downloader().then((message) => { + console.log(message); +}).catch((err) => { + console.error(err) +});