Skip to content

Commit 78f0289

Browse files
authored
Show progress bar while downloading (#31)
Closes #17
1 parent 09ee840 commit 78f0289

File tree

3 files changed

+201
-117
lines changed

3 files changed

+201
-117
lines changed

install.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ const extractZip = require('extract-zip');
55
const got = require('got');
66
const tmp = require('tmp');
77
const debug = require('debug')('node-chromium');
8+
const ProgressBar = require('progress');
89

910
const config = require('./config');
1011
const utils = require('./utils');
1112

13+
let progressBar = null;
14+
1215
/* eslint unicorn/prevent-abbreviations: ["off"] */
1316

1417
function createTempFile() {
@@ -36,6 +39,7 @@ async function downloadChromiumRevision(revision) {
3639
function _downloadFile(url, destPath) {
3740
return new Promise((resolve, reject) => {
3841
got.stream(url, utils.getRequestOptions(url))
42+
.on('downloadProgress', onProgress)
3943
.on('error', error => {
4044
console.error('An error occurred while trying to download file', error.message);
4145
reject(error);
@@ -51,6 +55,32 @@ function _downloadFile(url, destPath) {
5155
});
5256
}
5357

58+
/**
59+
* Handles download progress events.
60+
* @param progress Information about progress so far.
61+
*/
62+
function onProgress(progress) {
63+
try {
64+
if (!progressBar) {
65+
const formatBytes = bytes => {
66+
const mb = bytes / 1024 / 1024;
67+
return `${Math.round(mb * 10) / 10} MB`;
68+
};
69+
70+
progressBar = new ProgressBar(`Downloading Chromium - ${formatBytes(progress.total)} [:bar] :percent :etas `, {
71+
width: 20,
72+
total: progress.total
73+
});
74+
}
75+
76+
progressBar.tick(progress.transferred - progressBar.curr);
77+
} catch (error) {
78+
// Don't die on progress bar failure, log it and stop progress
79+
console.error('Error displaying progress bar. Continuing anyway...', error);
80+
progressBar = {tick: () => {}};
81+
}
82+
}
83+
5484
function unzipArchive(archivePath, outputFolder) {
5585
debug('Started extracting archive', archivePath);
5686

@@ -73,7 +103,7 @@ async function install() {
73103
console.info('Step 1. Retrieving Chromium revision number');
74104
const revision = chromiumRevision || await utils.getLatestRevisionNumber();
75105

76-
console.info(`Step 2. Downloading Chromium (this might take a while). Revision number: ${revision}`);
106+
console.info(`Step 2. Downloading Chromium revision ${revision}`);
77107
const tmpPath = await downloadChromiumRevision(revision);
78108

79109
console.info('Step 3. Setting up Chromium binaries');

0 commit comments

Comments
 (0)