From 2d51c0cb4192feba18f64cb8b3c6328e89cb7e88 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Thu, 28 Jun 2018 22:59:29 +0300 Subject: [PATCH] fix(reporting): limit the width of lifecycle output --- packages/default-reporter/src/index.ts | 1 - .../default-reporter/src/reporterForClient.ts | 20 ++++++++++++++----- packages/default-reporter/test/index.ts | 6 +++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/default-reporter/src/index.ts b/packages/default-reporter/src/index.ts index f6f5ba9b21..2375c6e946 100644 --- a/packages/default-reporter/src/index.ts +++ b/packages/default-reporter/src/index.ts @@ -1,7 +1,6 @@ import createDiffer = require('ansi-diff') import cliCursor = require('cli-cursor') import most = require('most') -import R = require('ramda') import * as supi from 'supi' import PushStream = require('zen-push') import {EOL} from './constants' diff --git a/packages/default-reporter/src/reporterForClient.ts b/packages/default-reporter/src/reporterForClient.ts index 6105b3a941..ceaf436b7a 100644 --- a/packages/default-reporter/src/reporterForClient.ts +++ b/packages/default-reporter/src/reporterForClient.ts @@ -147,6 +147,7 @@ export default function ( outputs.push(most.of(progress)) } + const formatLifecycle = formatLifecycleHideOverflow.bind(null, width) if (!appendOnly) { const tarballsProgressOutput$ = log$.progress .filter((log) => log.status === 'fetching_started' && @@ -495,7 +496,13 @@ function printDiffs (pkgsDiff: PackageDiff[]) { return msg } -function formatLifecycle (cwd: string, logObj: LifecycleLog) { +const ANSI_ESCAPES_LENGTH_OF_PREFIX = hlValue(' ').length - 1 + +function formatLifecycleHideOverflow ( + maxWidth: number, + cwd: string, + logObj: LifecycleLog, +) { const prefix = `${ logObj.wd === logObj.depPath ? rightPad(formatPrefix(cwd, logObj.wd), PREFIX_MAX_LENGTH) @@ -507,21 +514,24 @@ function formatLifecycle (cwd: string, logObj: LifecycleLog) { if (logObj['exitCode'] === 0) { return `${prefix}: done` } - const line = formatLine(logObj) + const maxLineWidth = maxWidth - prefix.length - 2 + ANSI_ESCAPES_LENGTH_OF_PREFIX + const line = formatLine(maxLineWidth, logObj) if (logObj.level === 'error') { return `${prefix}: ${line}` } return `${prefix}: ${line}` } -function formatLine (logObj: LifecycleLog) { +function formatLine (maxWidth: number, logObj: LifecycleLog) { if (typeof logObj['exitCode'] === 'number') return chalk.red(`Exited with ${logObj['exitCode']}`) + const line = stripAnsi(logObj['line']).substr(0, maxWidth) + // TODO: strip only the non-color/style ansi escape codes if (logObj.level === 'error') { - return chalk.gray(stripAnsi(logObj['line'])) + return chalk.gray(line) } - return stripAnsi(logObj['line']) + return line } function formatInstallCheck (logObj: InstallCheckLog) { diff --git a/packages/default-reporter/test/index.ts b/packages/default-reporter/test/index.ts index 087861a649..5492ea11bc 100644 --- a/packages/default-reporter/test/index.ts +++ b/packages/default-reporter/test/index.ts @@ -307,7 +307,7 @@ test('prints summary', t => { }) test('groups lifecycle output', t => { - const output$ = toOutput$(createStreamParser(), {cmd: 'install'}) + const output$ = toOutput$(createStreamParser(), {cmd: 'install', width: 79}) const pkgId = 'registry.npmjs.org/foo/1.0.0' @@ -318,7 +318,7 @@ test('groups lifecycle output', t => { }) lifecycleLogger.debug({ depPath: 'registry.npmjs.org/foo/1.0.0', - line: 'foo', + line: 'foo 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20', stage: 'preinstall', }) lifecycleLogger.debug({ @@ -373,7 +373,7 @@ test('groups lifecycle output', t => { next: output => { t.equal(output, EOL + stripIndents` registry.npmjs.org/foo/1.0.0 | ${PREINSTALL}$ node foo - registry.npmjs.org/foo/1.0.0 | ${PREINSTALL}: foo + registry.npmjs.org/foo/1.0.0 | ${PREINSTALL}: foo 0 1 2 3 4 5 6 7 8 9 registry.npmjs.org/foo/1.0.0 | ${POSTINSTALL}$ node foo registry.npmjs.org/foo/1.0.0 | ${POSTINSTALL}: foo I