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

Commit

Permalink
build: improve workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 22, 2019
1 parent af85bf0 commit 9ff909e
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .bumpedrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins:
plugin: bumped-changelog
Committing new version:
plugin: bumped-terminal
command: 'git add CHANGELOG.md package.json && git commit -m "Release $newVersion"'
command: 'npx git-authors-cli && npx finepack && git add CHANGELOG.md package.json && git commit -m "Release $newVersion"'
Detecting problems before publish:
plugin: bumped-terminal
command: 'git-dirty'
Expand Down
118 changes: 70 additions & 48 deletions lib/downloader.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,98 @@
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var request = require('request');
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var request = require('request')

// First, look for the download link.
/*jshint maxlen:false */
var dir, filePath;
var isWin = (process.platform === 'win32' || process.env.NODE_PLATFORM === 'windows') ? true : false;
var defaultBin = path.join(__dirname, '..', 'bin');
var defaultPath = path.join(defaultBin, 'details');
var url = 'https://yt-dl.org/downloads/latest/youtube-dl';
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'

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

var status = null;
var status = null

// download the correct version of the binary based on the platform
url = exec(url);
url = exec(url)

request.get(url, { followRedirect: false }, function (err, res) {
if (res.statusCode !== 302) {
return callback(new Error('Did not get redirect for the latest version link. Status: ' + res.statusCode));
return callback(
new Error(
'Did not get redirect for the latest version link. Status: ' +
res.statusCode
)
)
}

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(url)[1];
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(
url
)[1]

downloadFile.on('response', function response(res) {
downloadFile.on('response', function response (res) {
if (res.statusCode !== 200) {
status = new Error('Response Error: ' + res.statusCode);
return;
status = new Error('Response Error: ' + res.statusCode)
return
}
downloadFile.pipe(fs.createWriteStream(filePath, { mode: 493 }));
});
downloadFile.pipe(fs.createWriteStream(filePath, { mode: 493 }))
})

downloadFile.on('error', function error(err) { callback(err); });
downloadFile.on('error', function error (err) {
callback(err)
})

downloadFile.on('end', function end() { callback(status, newVersion); });

});
downloadFile.on('end', function end () {
callback(status, newVersion)
})
})
}

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

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

function downloader(binDir, callback) {

'use strict';
function downloader (binDir, callback) {
'use strict'
if (typeof binDir === 'function') {
callback = binDir;
binDir = null;
callback = binDir
binDir = null
}

createBase(binDir);
createBase(binDir)

download(url, function error(err, newVersion) {
if (err) { return callback(err); }
fs.writeFileSync(defaultPath, JSON.stringify({ version: newVersion, path: ((binDir) ? filePath : binDir), exec: exec('youtube-dl') }), 'utf8');
callback(null, 'Downloaded youtube-dl ' + newVersion);
});
download(url, function error (err, newVersion) {
if (err) {
return callback(err)
}
fs.writeFileSync(
defaultPath,
JSON.stringify({
version: newVersion,
path: binDir ? filePath : binDir,
exec: exec('youtube-dl')
}),
'utf8'
)
callback(null, 'Downloaded youtube-dl ' + newVersion)
})
}

module.exports = downloader;
module.exports = downloader
60 changes: 34 additions & 26 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
// Arguments we dont want users to use with youtube-dl
// because they will break the module.
var badArgs = [
'-h', '--help',
'-v', '--version',
'-U', '--update',
'-q', '--quiet',
'-s', '--simulate',
'-g', '--get-url',
'-e', '--get-title',
'-h',
'--help',
'-v',
'--version',
'-U',
'--update',
'-q',
'--quiet',
'-s',
'--simulate',
'-g',
'--get-url',
'-e',
'--get-title',
'--get-id',
'--get-thumbnail',
'--get-description',
'--get-duration',
'--get-filename',
'--get-format',
'-j', '--dump-json',
'-j',
'--dump-json',
'--newline',
'--no-progress',
'--console-title',
'-v', '--verbose',
'-v',
'--verbose',
'--dump-intermediate-pages',
'--write-pages',
'--print-traffic',
];
'--print-traffic'
]

/**
* Helps parse options used in youtube-dl command.
Expand All @@ -31,15 +40,14 @@ var badArgs = [
* @return {Array.<String>}
*/
exports.parseOpts = function (args) {
var pos;
var pos
for (var i = 0, len = badArgs.length; i < len; i++) {
if ((pos = args.indexOf(badArgs[i])) !== -1) {
args.splice(pos, 1);
args.splice(pos, 1)
}
}
return args;
};

return args
}

/**
* Converts seconds to format hh:mm:ss
Expand All @@ -48,26 +56,26 @@ exports.parseOpts = function (args) {
* @return {String}
*/
exports.formatDuration = function (seconds) {
var parts = [];
parts.push(seconds % 60);
var minutes = Math.floor(seconds / 60);
var parts = []
parts.push(seconds % 60)
var minutes = Math.floor(seconds / 60)
if (minutes > 0) {
parts.push(minutes % 60);
var hours = Math.floor(minutes / 60);
parts.push(minutes % 60)
var hours = Math.floor(minutes / 60)
if (hours > 0) {
parts.push(hours);
parts.push(hours)
}
}
return parts.reverse().join(':');
};
return parts.reverse().join(':')
}

/**
* Checks wether str is a string or not
*
* @param {String} str
* @return {Boolean}
*/
exports.isString = str => typeof str === "string";
exports.isString = str => typeof str === 'string'

/**
* Checks arr contains value
Expand All @@ -76,4 +84,4 @@ exports.isString = str => typeof str === "string";
* @param {string|number} arr
* @return {Boolean}
*/
exports.has = (arr, value) => arr && arr.indexOf(value) > -1;
exports.has = (arr, value) => arr && arr.indexOf(value) > -1
Loading

0 comments on commit 9ff909e

Please sign in to comment.