From 0708b3b2dbfc811b0e692133557b6b2e698519fd Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 24 Apr 2024 15:02:04 -0700 Subject: [PATCH] fix: use name of level instead of label for logging prefixes (#7414) --- lib/utils/display.js | 6 +-- .../tap-snapshots/test/index.js.test.cjs | 50 +++++++++---------- test/fixtures/mock-logs.js | 14 ++---- 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/lib/utils/display.js b/lib/utils/display.js index 4e0ba81ca956e..67c745f310415 100644 --- a/lib/utils/display.js +++ b/lib/utils/display.js @@ -38,11 +38,9 @@ const LEVEL_OPTIONS = { }, error: { index: 1, - label: 'ERR!', }, warn: { index: 2, - label: 'WARN', }, notice: { index: 3, @@ -55,11 +53,9 @@ const LEVEL_OPTIONS = { }, verbose: { index: 6, - label: 'verb', }, silly: { index: 7, - label: 'sill', }, } @@ -338,7 +334,7 @@ class Display { const title = args.shift() const prefix = [ this.#logColors.heading(this.#heading), - this.#logColors[level](levelOpts.label ?? level), + this.#logColors[level](level), title ? this.#logColors.title(title) : null, ] this.#stderrWrite({ prefix }, ...args) diff --git a/smoke-tests/tap-snapshots/test/index.js.test.cjs b/smoke-tests/tap-snapshots/test/index.js.test.cjs index c90e376dee20e..c889c830e845a 100644 --- a/smoke-tests/tap-snapshots/test/index.js.test.cjs +++ b/smoke-tests/tap-snapshots/test/index.js.test.cjs @@ -43,31 +43,31 @@ npm {NPM} ` exports[`test/index.js TAP basic npm ci > should throw mismatch deps in lock file error 1`] = ` -npm ERR! code EUSAGE -npm ERR! -npm ERR! \`npm ci\` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with \`npm install\` before continuing. -npm ERR! -npm ERR! Invalid: lock file's abbrev@1.0.4 does not satisfy abbrev@1.1.1 -npm ERR! -npm ERR! Clean install a project -npm ERR! -npm ERR! Usage: -npm ERR! npm ci -npm ERR! -npm ERR! Options: -npm ERR! [--install-strategy ] [--legacy-bundling] -npm ERR! [--global-style] [--omit [--omit ...]] -npm ERR! [--include [--include ...]] -npm ERR! [--strict-peer-deps] [--foreground-scripts] [--ignore-scripts] [--no-audit] -npm ERR! [--no-bin-links] [--no-fund] [--dry-run] -npm ERR! [-w|--workspace [-w|--workspace ...]] -npm ERR! [-ws|--workspaces] [--include-workspace-root] [--install-links] -npm ERR! -npm ERR! aliases: clean-install, ic, install-clean, isntall-clean -npm ERR! -npm ERR! Run "npm help ci" for more info - -npm ERR! A complete log of this run can be found in: {NPM}/{TESTDIR}/cache/_logs/{LOG} +npm error code EUSAGE +npm error +npm error \`npm ci\` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with \`npm install\` before continuing. +npm error +npm error Invalid: lock file's abbrev@1.0.4 does not satisfy abbrev@1.1.1 +npm error +npm error Clean install a project +npm error +npm error Usage: +npm error npm ci +npm error +npm error Options: +npm error [--install-strategy ] [--legacy-bundling] +npm error [--global-style] [--omit [--omit ...]] +npm error [--include [--include ...]] +npm error [--strict-peer-deps] [--foreground-scripts] [--ignore-scripts] [--no-audit] +npm error [--no-bin-links] [--no-fund] [--dry-run] +npm error [-w|--workspace [-w|--workspace ...]] +npm error [-ws|--workspaces] [--include-workspace-root] [--install-links] +npm error +npm error aliases: clean-install, ic, install-clean, isntall-clean +npm error +npm error Run "npm help ci" for more info + +npm error A complete log of this run can be found in: {NPM}/{TESTDIR}/cache/_logs/{LOG} ` exports[`test/index.js TAP basic npm diff > should have expected diff output 1`] = ` diff --git a/test/fixtures/mock-logs.js b/test/fixtures/mock-logs.js index c29928130088c..346944d7405b0 100644 --- a/test/fixtures/mock-logs.js +++ b/test/fixtures/mock-logs.js @@ -1,13 +1,7 @@ const { log: { LEVELS } } = require('proc-log') const { stripVTControlCharacters: stripAnsi } = require('util') -const labels = new Map([ - ['error', 'ERR!'], - ['warn', 'WARN'], - ['verbose', 'verb'], - ['silly', 'sill'], -].reduce((acc, v) => acc.concat([v, v.slice(0).reverse()]), [])) -const logPrefix = new RegExp(`^npm (${LEVELS.map(l => labels.get(l) ?? l).join('|')})\\s`) +const logPrefix = new RegExp(`^npm (${LEVELS.join('|')})\\s`) const isLog = (str) => logPrefix.test(stripAnsi(str)) // We only strip trailing newlines since some output will @@ -63,15 +57,15 @@ module.exports = () => { // Split on spaces for the heading and level/label. We know that // none of those have spaces but could be colorized so there's no // other good way to get each of those including control chars - const [rawHeading, rawLabel] = str.split(' ') - const rawPrefix = `${rawHeading} ${rawLabel} ` + const [rawHeading, rawLevel] = str.split(' ') + const rawPrefix = `${rawHeading} ${rawLevel} ` // If message is colorized we can just replaceAll with the string since // it will be unique due to control chars. Otherwise we create a regex // that will only match the beginning of each line. const prefix = stripAnsi(str) !== str ? rawPrefix : new RegExp(`^${rawPrefix}`, 'gm') // The level needs color stripped always because we use it to filter logs - const level = labels.get(stripAnsi(rawLabel)) ?? stripAnsi(rawLabel) + const level = stripAnsi(rawLevel) logs.push(str.replaceAll(prefix, `${level} `)) levelLogs.push({ level, message: str.replaceAll(prefix, '') })