69 lines
2.2 KiB
JavaScript
Executable File
69 lines
2.2 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
/*eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
|
|
const args = process.argv.slice(2);
|
|
const { DownloaderHelper } = require('../dist');
|
|
const { byteHelper, color, COLOR_RED, COLOR_GREEN,
|
|
COLOR_YELLOW, COLOR_CYAN, COLOR_MAGENTA,
|
|
isValidPath, isValidUrl, inlineLog
|
|
} = require('./helpers');
|
|
const pkg = require('../package.json');
|
|
|
|
// Arguments
|
|
if (!args.length || args.length < 2) {
|
|
return console.log('USAGE: ndh [destination] [url]');
|
|
}
|
|
|
|
const dest = args[0];
|
|
const url = args[1];
|
|
|
|
if (!isValidPath(dest)) {
|
|
return console.error(color(COLOR_RED, 'Please use an existing folder or valid path'));
|
|
}
|
|
|
|
if (!isValidUrl(url)) {
|
|
return console.error(color(COLOR_RED, 'Please use a valid URL'));
|
|
}
|
|
|
|
let progressLog = '';
|
|
const dl = new DownloaderHelper(url, dest, {
|
|
headers: {
|
|
'user-agent': pkg.name + '@' + pkg.version
|
|
},
|
|
});
|
|
|
|
dl
|
|
.on('end', _ =>
|
|
inlineLog(progressLog + ' - ' + color(COLOR_GREEN, 'Download Completed'))
|
|
)
|
|
.on('retry', (attempt, opts) => {
|
|
let count = Math.floor(opts.delay / 1000);
|
|
const retryLog = () => {
|
|
inlineLog(color(COLOR_YELLOW, `Retry Attempt: ${attempt}/${opts.maxRetries} | Starts on: ${count} secs`));
|
|
if (count > 0) {
|
|
setTimeout(() => retryLog(), 1000);
|
|
}
|
|
count--;
|
|
};
|
|
retryLog();
|
|
})
|
|
.on('resume', isResumed => {
|
|
if (!isResumed) {
|
|
console.warn(color(COLOR_YELLOW, "\nThis URL doesn't support resume, it will start from the beginning"));
|
|
}
|
|
})
|
|
.on('progress.throttled', stats => {
|
|
const progress = stats.progress.toFixed(1);
|
|
const speed = byteHelper(stats.speed);
|
|
const downloaded = byteHelper(stats.downloaded);
|
|
const total = byteHelper(stats.total);
|
|
|
|
const cName = color(COLOR_YELLOW, stats.name);
|
|
const cSpeed = color(COLOR_MAGENTA, speed + '/s');
|
|
const cProgress = color(COLOR_MAGENTA, progress + '%');
|
|
const cSize = color(COLOR_CYAN, `[${downloaded}/${total}]`);
|
|
progressLog = `${cName}: ${cSpeed} - ${cProgress} ${cSize}`;
|
|
inlineLog(progressLog);
|
|
});
|
|
|
|
dl.start().catch(err => console.error(color(COLOR_RED, 'Something happend'), err));
|