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

Commit

Permalink
fix: extract into a module
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 10, 2019
1 parent 9e44925 commit 1125b0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
19 changes: 19 additions & 0 deletions lib/get-binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const path = require('path')
const fs = require('fs')

const binPath = path.join(__dirname, '..', 'bin')
const detailsPath = path.join(binPath, 'details')

module.exports = () => {
if (!fs.existsSync(detailsPath)) {
throw new Error('ERROR: unable to locate `youtube-dl` at ' + binPath)
}

const details = JSON.parse(fs.readFileSync(detailsPath))

return details.path
? details.path
: path.resolve(__dirname, '..', 'bin', details.exec)
}
21 changes: 1 addition & 20 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ const execFile = require('child_process').execFile
const streamify = require('streamify')
const request = require('request')
const hms = require('hh-mm-ss')
const path = require('path')
const http = require('http')
const url = require('url')
const fs = require('fs')

const {
isYouTubeRegex,
Expand All @@ -17,29 +15,12 @@ const {
isString
} = require('./util')

const detailsPath = path.join(__dirname, '..', 'bin/details')
let ytdlBinary = require('./get-binary')()

const TEN_MEGABYTES = 1000 * 1000 * 10

const execFileOpts = { maxBuffer: TEN_MEGABYTES }

let ytdlBinary = (() => {
if (fs.existsSync(detailsPath)) {
const details = JSON.parse(fs.readFileSync(detailsPath))
return details.path
? details.path
: path.resolve(__dirname, '..', 'bin', details.exec)
}

if (!fs.existsSync(ytdlBinary)) {
console.error(
'ERROR: unable to locate youtube-dl details in ' +
path.dirname(ytdlBinary)
)
process.exit(1)
}
})()

function youtubeDl (args, options, cb) {
execFile(ytdlBinary, args, { ...execFileOpts, ...options }, function done (
err,
Expand Down

0 comments on commit 1125b0e

Please sign in to comment.