Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
handles overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobg committed Aug 17, 2020
1 parent e11831a commit a3551d1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Expand Down
35 changes: 28 additions & 7 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,25 +45,30 @@ 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)
})
})
}

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'))
}

Expand All @@ -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(
Expand Down

0 comments on commit a3551d1

Please sign in to comment.