fix: print warnings to stderr (#8342)

This commit is contained in:
Munif Tanjim
2024-08-04 05:53:52 +06:00
committed by GitHub
parent d20eed33ab
commit 98c8bd69cb
3 changed files with 11 additions and 23 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/cli-utils": patch
"pnpm": patch
---
Print warnings to stderr [#8342](https://github.com/pnpm/pnpm/pull/8342).

View File

@@ -28,9 +28,8 @@ export async function getConfig (
delete config.reporter // This is a silly workaround because @pnpm/core expects a function as opts.reporter
}
// The warning should not be printed when --json is specified
if (warnings.length > 0 && !cliOptions.json) {
console.log(warnings.map((warning) => formatWarn(warning)).join('\n'))
if (warnings.length > 0) {
console.warn(warnings.map((warning) => formatWarn(warning)).join('\n'))
}
return config

View File

@@ -4,11 +4,11 @@ import { getConfig } from '@pnpm/cli-utils'
import { prepare } from '@pnpm/prepare'
beforeEach(() => {
jest.spyOn(console, 'log')
jest.spyOn(console, 'warn')
})
afterEach(() => {
(console.log as jest.Mock).mockRestore()
(console.warn as jest.Mock).mockRestore()
})
test('console a warning when the .npmrc has an env variable that does not exist', async () => {
@@ -25,22 +25,5 @@ test('console a warning when the .npmrc has an env variable that does not exist'
})
// eslint-disable-next-line no-template-curly-in-string
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Failed to replace env in config: ${ENV_VAR_123}'))
})
test('should not console a warning when --json is specified', async () => {
prepare()
fs.writeFileSync('.npmrc', 'foo=${ENV_VAR_123}', 'utf8') // eslint-disable-line
await getConfig({
json: true,
}, {
workspaceDir: '.',
excludeReporter: false,
rcOptionsTypes: {},
})
// eslint-disable-next-line no-template-curly-in-string
expect(console.log).not.toHaveBeenCalledWith(expect.stringContaining('Failed to replace env in config: ${ENV_VAR_123}'))
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Failed to replace env in config: ${ENV_VAR_123}'))
})