From e5b18c189f1ae4395717e404b276af5793483fdd Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sun, 5 Sep 2021 16:07:05 +0200 Subject: [PATCH] Support the newer pino-pretty as well as the older pino-pretty will change its export in v7. This change makes it possible to support pino-pretty v4, v5, v6 and upcoming v7 at the same time. It also bumps the pino-pretty devDependency to v5. --- lib/tools.js | 2 +- package.json | 2 +- test/pretty.test.js | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/tools.js b/lib/tools.js index 57398a73f..f8112f846 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -180,7 +180,7 @@ function getPrettyStream (opts, prettifier, dest, instance) { return prettifierMetaWrapper(prettifier(opts), dest, opts) } try { - const prettyFactory = require('pino-pretty') + const prettyFactory = require('pino-pretty').prettyFactory || require('pino-pretty') prettyFactory.asMetaWrapper = prettifierMetaWrapper return prettifierMetaWrapper(prettyFactory(opts), dest, opts) } catch (e) { diff --git a/package.json b/package.json index bd613712c..7f41df97b 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "import-fresh": "^3.2.1", "log": "^6.0.0", "loglevel": "^1.6.7", - "pino-pretty": "^4.1.0", + "pino-pretty": "^5.0.0", "pre-commit": "^1.2.2", "proxyquire": "^2.1.3", "pump": "^3.0.0", diff --git a/test/pretty.test.js b/test/pretty.test.js index a3133cb72..43074916c 100644 --- a/test/pretty.test.js +++ b/test/pretty.test.js @@ -54,12 +54,26 @@ test('throws when prettyPrint is true but pino-pretty module is not installed', // pino pretty *is* installed, and probably also cached, so rather than // messing with the filesystem the simplest way to generate a not found // error is to simulate it: - const prettyFactory = require('pino-pretty') + const pinoPretty = require('pino-pretty') require.cache[require.resolve('pino-pretty')].exports = () => { throw Error('Cannot find module \'pino-pretty\'') } throws(() => pino({ prettyPrint: true }), 'Missing `pino-pretty` module: `pino-pretty` must be installed separately') + require.cache[require.resolve('pino-pretty')].exports = pinoPretty +}) + +test('support older pino-pretty', async ({ doesNotThrow }) => { + // pino pretty *is* installed, and probably also cached, so rather than + // messing with the filesystem the simplest way to generate a not found + // error is to simulate it: + const pinoPretty = require('pino-pretty') + const prettyFactory = pinoPretty.prettyFactory + pinoPretty.prettyFactory = undefined require.cache[require.resolve('pino-pretty')].exports = prettyFactory + + doesNotThrow(() => pino({ prettyPrint: true })) + + delete require.cache[require.resolve('pino-pretty')] }) test('throws when prettyPrint has invalid options', async ({ throws }) => {