fix(reporting): limit the width of lifecycle output

This commit is contained in:
Zoltan Kochan
2018-06-28 22:59:29 +03:00
parent 2e19ae45ae
commit 2d51c0cb41
3 changed files with 18 additions and 9 deletions

View File

@@ -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'

View File

@@ -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) {

View File

@@ -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