From 78fc0c11c854a1d9df75f48cba397b51131d3f8a Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 30 Mar 2022 15:29:06 -0700 Subject: [PATCH] fix: only call npmlog progress methods if explicitly requested Fixes #3314 --- workspaces/arborist/lib/tracker.js | 10 +++++++--- workspaces/arborist/test/tracker.js | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/workspaces/arborist/lib/tracker.js b/workspaces/arborist/lib/tracker.js index c2a456e4832cc..42c401e8799e8 100644 --- a/workspaces/arborist/lib/tracker.js +++ b/workspaces/arborist/lib/tracker.js @@ -1,10 +1,12 @@ const _progress = Symbol('_progress') const _onError = Symbol('_onError') +const _setProgress = Symbol('_setProgess') const npmlog = require('npmlog') module.exports = cls => class Tracker extends cls { constructor (options = {}) { super(options) + this[_setProgress] = !!options.progress this[_progress] = new Map() } @@ -27,7 +29,7 @@ module.exports = cls => class Tracker extends cls { // 1. no existing tracker, no subsection // Create a new tracker from npmlog // starts progress bar - if (this[_progress].size === 0) { + if (this[_setProgress] && this[_progress].size === 0) { npmlog.enableProgress() } @@ -76,7 +78,7 @@ module.exports = cls => class Tracker extends cls { // remove progress bar if all // trackers are finished - if (this[_progress].size === 0) { + if (this[_setProgress] && this[_progress].size === 0) { npmlog.disableProgress() } } else if (!hasTracker && subsection === null) { @@ -92,7 +94,9 @@ module.exports = cls => class Tracker extends cls { } [_onError] (msg) { - npmlog.disableProgress() + if (this[_setProgress]) { + npmlog.disableProgress() + } throw new Error(msg) } } diff --git a/workspaces/arborist/test/tracker.js b/workspaces/arborist/test/tracker.js index 0c2fbb729d45e..229663919aba6 100644 --- a/workspaces/arborist/test/tracker.js +++ b/workspaces/arborist/test/tracker.js @@ -1,8 +1,8 @@ const Tracker = require('../lib/tracker.js')(class {}) const t = require('tap') -t.test('no npmlog', t => { - const tr = new Tracker() +t.test('with progress', t => { + const tr = new Tracker({ progress: true }) t.doesNotThrow(() => { tr.addTracker('testTracker') }) @@ -10,6 +10,10 @@ t.test('no npmlog', t => { tr.finishTracker('testTracker') }) + t.throws(() => { + tr.addTracker() + }, Error, `Tracker can't be null or undefined`) + t.end() })