Skip to content

Commit 9e41864

Browse files
committed
feat(spinner): add spinner and details information when error happens
"closes #9"
1 parent 54320d0 commit 9e41864

4 files changed

Lines changed: 85 additions & 23 deletions

File tree

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 Nur Rony and Contributors
3+
Copyright (c) 2019 Nur Rony and Contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package-lock.json

Lines changed: 59 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
},
6868
"dependencies": {
6969
"hlsdownloader": "^2.2.1",
70-
"minimist": "^1.2.0"
70+
"minimist": "^1.2.0",
71+
"ora": "^3.4.0"
7172
}
7273
}

src/lib.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createReadStream } from 'fs';
22
import HLSDownloader from 'hlsdownloader';
3+
import Ora from 'ora';
34
import { version } from './../package.json';
45

56
export function prepareExtraParams(requestParams) {
@@ -17,10 +18,29 @@ export function prepareExtraParams(requestParams) {
1718
return requestObject;
1819
}
1920

21+
const _downloadHls = params =>
22+
new Promise((resolve, reject) =>
23+
new HLSDownloader({ ...params }).startDownload((err, msg) => (err ? reject(err) : resolve(msg)))
24+
);
25+
2026
const commandHandlers = {
21-
download(params) {
22-
const downloader = new HLSDownloader({ ...params });
23-
downloader.startDownload((err, msg) => (err ? console.log(err) : console.log(msg)));
27+
async download(params) {
28+
const spinner = new Ora({ text: 'Downloading available TS and Playlist files...' });
29+
try {
30+
spinner.start();
31+
let { message, errors = null } = await _downloadHls(params);
32+
spinner.text = message;
33+
if (errors) {
34+
spinner.warn();
35+
console.log('Details:\r\n Following URLs can not be downloaded');
36+
console.log(`${errors.join('\r\n')}`);
37+
} else {
38+
spinner.succeed();
39+
}
40+
} catch (error) {
41+
spinner.text = `Downloading failed.\r\nDetails: ${error.statusCode} - ${error.message}. \r\n`;
42+
spinner.fail();
43+
}
2444
},
2545
help() {
2646
createReadStream('./help.txt').pipe(process.stdout);

0 commit comments

Comments
 (0)