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

Commit

Permalink
build: be possible setup downloader for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 22, 2019
1 parent 41f0a80 commit 8111957
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions lib/downloader.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var request = require('request')
'use strict'

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

const [, , ...flags] = process.argv

const isWin = flags.includes('--platform=windows') || require('./util').isWin

// First, look for the download link.
var dir, filePath
var isWin = !!(
process.platform === 'win32' || process.env.NODE_PLATFORM === 'windows'
)
var defaultBin = path.join(__dirname, '..', 'bin')
var defaultPath = path.join(defaultBin, 'details')
var url = 'https://yt-dl.org/downloads/latest/youtube-dl'
let dir, filePath
const defaultBin = path.join(__dirname, '..', 'bin')
const defaultPath = path.join(defaultBin, 'details')
const url = 'https://yt-dl.org/downloads/latest/youtube-dl'

function download (url, callback) {
'use strict'

var status = null
let status

// download the correct version of the binary based on the platform
url = exec(url)
Expand All @@ -32,9 +33,9 @@ function download (url, callback) {
)
}

var url = res.headers.location
var downloadFile = request.get(url)
var newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(
const url = res.headers.location
const downloadFile = request.get(url)
const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(
url
)[1]

Expand All @@ -56,23 +57,16 @@ function download (url, callback) {
})
}

function exec (path) {
'use strict'
return isWin ? path + '.exe' : path
}
const exec = path => (isWin ? path + '.exe' : path)

function createBase (binDir) {
'use strict'
dir = binDir || defaultBin
mkdirp.sync(dir)
if (binDir) {
mkdirp.sync(defaultBin)
}
if (binDir) mkdirp.sync(defaultBin)
filePath = path.join(dir, exec('youtube-dl'))
}

function downloader (binDir, callback) {
'use strict'
if (typeof binDir === 'function') {
callback = binDir
binDir = null
Expand All @@ -81,9 +75,7 @@ function downloader (binDir, callback) {
createBase(binDir)

download(url, function error (err, newVersion) {
if (err) {
return callback(err)
}
if (err) return callback(err)
fs.writeFileSync(
defaultPath,
JSON.stringify({
Expand All @@ -93,7 +85,7 @@ function downloader (binDir, callback) {
}),
'utf8'
)
callback(null, 'Downloaded youtube-dl ' + newVersion)
return callback(null, 'Downloaded youtube-dl ' + newVersion)
})
}

Expand Down

0 comments on commit 8111957

Please sign in to comment.