diff --git a/cli/lib/tasks/download.js b/cli/lib/tasks/download.js index c2a5f77858..03ca27b259 100644 --- a/cli/lib/tasks/download.js +++ b/cli/lib/tasks/download.js @@ -139,10 +139,13 @@ const downloadFromUrl = ({ url, downloadDestination, progress }) => { // starting on our first progress notification const elapsed = new Date() - started - const eta = util.calculateEta(state.percent, elapsed) + // request-progress sends a value between 0 and 1 + const percentage = util.convertPercentToPercentage(state.percent) + + const eta = util.calculateEta(percentage, elapsed) // send up our percent and seconds remaining - progress.onProgress(state.percent, util.secsRemaining(eta)) + progress.onProgress(percentage, util.secsRemaining(eta)) }) // save this download here .pipe(fs.createWriteStream(downloadDestination)) diff --git a/cli/lib/util.js b/cli/lib/util.js index 369f29c031..40a189e481 100644 --- a/cli/lib/util.js +++ b/cli/lib/util.js @@ -189,7 +189,7 @@ const util = { calculateEta (percent, elapsed) { // returns the number of seconds remaining - // if we're at 100 already just return 0 + // if we're at 100% already just return 0 if (percent === 100) { return 0 } @@ -200,6 +200,13 @@ const util = { return elapsed * (1 / (percent / 100)) - elapsed }, + convertPercentToPercentage (num) { + // convert a percent with values between 0 and 1 + // with decimals, so that it is between 0 and 100 + // and has no decimal places + return Math.round(_.isFinite(num) ? (num * 100) : 0) + }, + secsRemaining (eta) { // calculate the seconds reminaing with no decimal places return (_.isFinite(eta) ? (eta / 1000) : 0).toFixed(0) diff --git a/cli/package.json b/cli/package.json index 22d5fcd856..7a9751e862 100644 --- a/cli/package.json +++ b/cli/package.json @@ -58,7 +58,7 @@ "moment": "2.24.0", "ramda": "0.24.1", "request": "2.88.0", - "request-progress": "0.4.0", + "request-progress": "3.0.0", "supports-color": "5.5.0", "tmp": "0.1.0", "url": "0.11.0", diff --git a/cli/test/lib/util_spec.js b/cli/test/lib/util_spec.js index 210655babd..e248252d1a 100644 --- a/cli/test/lib/util_spec.js +++ b/cli/test/lib/util_spec.js @@ -287,6 +287,30 @@ describe('util', () => { }) }) + describe('.calculateEta', () => { + it('Remaining eta is same as elapsed when 50%', () => { + expect(util.calculateEta('50', 1000)).to.equal(1000) + }) + + it('Remaining eta is 0 when 100%', () => { + expect(util.calculateEta('100', 500)).to.equal(0) + }) + }) + + describe('.convertPercentToPercentage', () => { + it('converts to 100 when 1', () => { + expect(util.convertPercentToPercentage(1)).to.equal(100) + }) + + it('strips out extra decimals', () => { + expect(util.convertPercentToPercentage(0.37892)).to.equal(38) + }) + + it('returns 0 if null num', () => { + expect(util.convertPercentToPercentage(null)).to.equal(0) + }) + }) + context('.printNodeOptions', () => { describe('NODE_OPTIONS is not set', () => {