Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/npm.js')
-rw-r--r--lib/npm.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/npm.js b/lib/npm.js
index 763c9b7a3..9fbda9073 100644
--- a/lib/npm.js
+++ b/lib/npm.js
@@ -40,6 +40,7 @@ class Npm extends EventEmitter {
#argvClean = []
#chalk = null
+ #outputBuffer = []
#logFile = new LogFile()
#display = new Display()
#timers = new Timers({
@@ -466,6 +467,37 @@ class Npm extends EventEmitter {
log.showProgress()
}
+ outputBuffer (item) {
+ this.#outputBuffer.push(item)
+ }
+
+ flushOutput (jsonError) {
+ if (!jsonError && !this.#outputBuffer.length) {
+ return
+ }
+
+ if (this.config.get('json')) {
+ const jsonOutput = this.#outputBuffer.reduce((acc, item) => {
+ if (typeof item === 'string') {
+ // try to parse it as json in case its a string
+ try {
+ item = JSON.parse(item)
+ } catch {
+ return acc
+ }
+ }
+ return { ...acc, ...item }
+ }, {})
+ this.output(JSON.stringify({ ...jsonOutput, ...jsonError }, null, 2))
+ } else {
+ for (const item of this.#outputBuffer) {
+ this.output(item)
+ }
+ }
+
+ this.#outputBuffer.length = 0
+ }
+
outputError (...msg) {
log.clearProgress()
// eslint-disable-next-line no-console