From 6c1eb021bad17a2940c17c275b1b59cbf4c4d72b Mon Sep 17 00:00:00 2001 From: Diana Morales Date: Wed, 27 Oct 2021 16:53:02 +0200 Subject: [PATCH] feat: use warning codes for deprecation warnings (#2595) * feat: use warning codes for deprecation warnings * Update logger.spec.ts * Update package.json --- .changeset/shaggy-parrots-smash.md | 8 ++++ docs/warnings.md | 29 ++++++++++++ package.json | 2 +- packages/cli/package.json | 1 + packages/cli/src/cli.ts | 3 +- packages/cli/src/commands/init.ts | 5 +-- packages/cli/tsconfig.json | 3 ++ packages/core/core/package.json | 3 +- packages/core/core/src/index.ts | 2 + packages/core/core/src/warning-utils.ts | 59 +++++++++++++++++++++++++ packages/logger/package.json | 1 + packages/logger/src/logger.ts | 11 ++--- packages/logger/test/logger.spec.ts | 33 +++++++++----- packages/node-api/src/cli-utils.ts | 8 ++-- pnpm-lock.yaml | 6 +++ 15 files changed, 145 insertions(+), 29 deletions(-) create mode 100644 .changeset/shaggy-parrots-smash.md create mode 100644 docs/warnings.md create mode 100644 packages/core/core/src/warning-utils.ts diff --git a/.changeset/shaggy-parrots-smash.md b/.changeset/shaggy-parrots-smash.md new file mode 100644 index 000000000..b09668d2f --- /dev/null +++ b/.changeset/shaggy-parrots-smash.md @@ -0,0 +1,8 @@ +--- +'@verdaccio/cli': minor +'@verdaccio/core': minor +'@verdaccio/logger': minor +'@verdaccio/node-api': minor +--- + +feat: use warning codes for deprecation warnings diff --git a/docs/warnings.md b/docs/warnings.md new file mode 100644 index 000000000..c4988945c --- /dev/null +++ b/docs/warnings.md @@ -0,0 +1,29 @@ +# Warning Codes + +## VERWAR001 + +Verdaccio doesn't need superuser privileges. Don't run it under root. + +## VERWAR002 + +logger is not defined + +## VERWAR003 + +'rotating-file type is not longer supported, consider use [logrotate] instead' + +## VERWAR004 + +invalid address - xxxxxx, we expect a port (e.g. "4873"), + +## VERDEP001 + +'config.logs is deprecated, rename configuration to "config.log" in singular' + +## VERDEP002 + +'deprecate: multiple logger configuration is deprecated, please check the migration guide.' + +## VERDEP003 + +'multiple addresses will be deprecated in the next major, only use one' diff --git a/package.json b/package.json index d98acc2c4..fee891b67 100644 --- a/package.json +++ b/package.json @@ -112,7 +112,7 @@ "docker": "docker build -t verdaccio/verdaccio:local . --no-cache", "format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,yml,yaml,md}\"", "format:check": "prettier --check \"**/*.{js,jsx,ts,tsx,json,yml,yaml,md}\"", - "lint": "eslint --max-warnings 45 \"**/*.{js,jsx,ts,tsx}\"", + "lint": "eslint --max-warnings 46 \"**/*.{js,jsx,ts,tsx}\"", "test": "pnpm recursive test --filter ./packages", "test:e2e:cli": "pnpm test --filter ...@verdaccio/e2e-cli", "test:e2e:ui": "pnpm test --filter ...@verdaccio/e2e-ui", diff --git a/packages/cli/package.json b/packages/cli/package.json index 0fe249a4d..e730ce99c 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -44,6 +44,7 @@ "start": "ts-node src/index.ts" }, "dependencies": { + "@verdaccio/core": "workspace:6.0.0-6-next.2", "@verdaccio/config": "workspace:6.0.0-6-next.9", "@verdaccio/logger": "workspace:6.0.0-6-next.6", "@verdaccio/node-api": "workspace:6.0.0-6-next.22", diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 05753ed00..bad99b62c 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -1,5 +1,6 @@ import { Cli } from 'clipanion'; +import { warningUtils } from '@verdaccio/core'; import { InfoCommand } from './commands/info'; import { InitCommand } from './commands/init'; import { VersionCommand } from './commands/version'; @@ -7,7 +8,7 @@ import { FastifyServer } from './commands/FastifyServer'; import { isVersionValid, MIN_NODE_VERSION } from './utils'; if (process.getuid && process.getuid() === 0) { - process.emitWarning(`Verdaccio doesn't need superuser privileges. don't run it under root`); + warningUtils.emit(warningUtils.Codes.VERWAR001); } if (!isVersionValid(process.version)) { diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index 41db4bafd..9e65efbb9 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -3,6 +3,7 @@ import { findConfigFile, parseConfigFile } from '@verdaccio/config'; import { setup, logger } from '@verdaccio/logger'; import { initServer } from '@verdaccio/node-api'; import { ConfigRuntime } from '@verdaccio/types'; +import { warningUtils } from '@verdaccio/core'; export const DEFAULT_PROCESS_NAME: string = 'verdaccio'; @@ -46,9 +47,7 @@ export class InitCommand extends Command { private initLogger(logConfig: ConfigRuntime) { try { if (logConfig.logs) { - process.emitWarning( - 'config.logs is deprecated, rename configuration to "config.log" in singular' - ); + warningUtils.emit(warningUtils.Codes.VERDEP001); } // FUTURE: remove fallback when is ready setup(logConfig.log || logConfig.logs); diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index ee836454e..1c2d5b9a2 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -13,6 +13,9 @@ { "path": "../core/cli-ui" }, + { + "path": "../core/core" + }, { "path": "../core/server" }, diff --git a/packages/core/core/package.json b/packages/core/core/package.json index 2655d4bb2..57a64e6f8 100644 --- a/packages/core/core/package.json +++ b/packages/core/core/package.json @@ -36,7 +36,8 @@ "dependencies": { "http-errors": "1.8.0", "http-status-codes": "2.1.4", - "semver": "7.3.5" + "semver": "7.3.5", + "fastify-warning": "0.2.0" }, "devDependencies": { "@verdaccio/types": "workspace:11.0.0-6-next.9" diff --git a/packages/core/core/src/index.ts b/packages/core/core/src/index.ts index bec3a3083..3d9df0ff5 100644 --- a/packages/core/core/src/index.ts +++ b/packages/core/core/src/index.ts @@ -6,6 +6,7 @@ import * as constants from './constants'; import * as pluginUtils from './plugin-utils'; import * as fileUtils from './file-utils'; import * as pkgUtils from './pkg-utils'; +import * as warningUtils from './warning-utils'; export { VerdaccioError, API_ERROR, SUPPORT_ERRORS, APP_ERROR } from './error-utils'; export { @@ -32,4 +33,5 @@ export { validatioUtils, constants, pluginUtils, + warningUtils, }; diff --git a/packages/core/core/src/warning-utils.ts b/packages/core/core/src/warning-utils.ts new file mode 100644 index 000000000..f3b51b77e --- /dev/null +++ b/packages/core/core/src/warning-utils.ts @@ -0,0 +1,59 @@ +import warning from 'fastify-warning'; + +const warningInstance = warning(); +const verdaccioWarning = 'VerdaccioWarning'; +const verdaccioDeprecation = 'VerdaccioDeprecation'; + +export enum Codes { + VERWAR001 = 'VERWAR001', + VERWAR002 = 'VERWAR002', + VERWAR003 = 'VERWAR003', + VERWAR004 = 'VERWAR004', + VERDEP001 = 'VERDEP001', + VERDEP002 = 'VERDEP002', + VERDEP003 = 'VERDEP003', +} + +warningInstance.create( + verdaccioWarning, + Codes.VERWAR001, + `Verdaccio doesn't need superuser privileges. don't run it under root` +); + +warningInstance.create(verdaccioWarning, Codes.VERWAR002, 'logger is not defined'); + +warningInstance.create( + verdaccioWarning, + Codes.VERWAR003, + 'rotating-file type is not longer supported, consider use [logrotate] instead' +); + +warningInstance.create( + verdaccioWarning, + Codes.VERWAR004, + `invalid address - %s, we expect a port (e.g. "4873"), +host:port (e.g. "localhost:4873") or full url '(e.g. "http://localhost:4873/") +https://verdaccio.org/docs/en/configuration#listen-port` +); + +warningInstance.create( + verdaccioDeprecation, + Codes.VERDEP001, + 'config.logs is deprecated, rename configuration to "config.log" in singular' +); + +warningInstance.create( + verdaccioDeprecation, + Codes.VERDEP002, + 'deprecate: multiple logger configuration is deprecated, please check the migration guide.' +); + +warningInstance.create( + verdaccioDeprecation, + Codes.VERDEP003, + 'multiple addresses will be deprecated in the next major, only use one' +); + +export function emit(code, a?: string, b?: string, c?: string) { + warningInstance.emit(code, a, b, c); +} diff --git a/packages/logger/package.json b/packages/logger/package.json index e7ed6ea15..d587a588d 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -39,6 +39,7 @@ "build": "cross-env BABEL_ENV=registry babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps" }, "dependencies": { + "@verdaccio/core": "workspace:6.0.0-6-next.2", "@verdaccio/logger-prettify": "workspace:6.0.0-6-next.4", "debug": "4.3.2", "lodash": "4.17.21", diff --git a/packages/logger/src/logger.ts b/packages/logger/src/logger.ts index 4ba82a207..e8e4353f9 100644 --- a/packages/logger/src/logger.ts +++ b/packages/logger/src/logger.ts @@ -1,6 +1,7 @@ import pino from 'pino'; import _ from 'lodash'; import buildDebug from 'debug'; +import { warningUtils } from '@verdaccio/core'; const debug = buildDebug('verdaccio:logger'); @@ -75,7 +76,7 @@ export function createLogger( export function getLogger() { if (_.isNil(logger)) { // FIXME: not sure about display here a warning - process.emitWarning('logger is not defined'); + warningUtils.emit(warningUtils.Codes.VERWAR002); return; } @@ -102,9 +103,7 @@ export function setup(options: LoggerConfig | LoggerConfigItem = DEFAULT_LOGGER_ debug('setup logger'); const isLegacyConf = Array.isArray(options); if (isLegacyConf) { - const deprecateMessage = - 'deprecate: multiple logger configuration is deprecated, please check the migration guide.'; - process.emitWarning(deprecateMessage); + warningUtils.emit(warningUtils.Codes.VERDEP002); } // verdaccio 5 does not allow multiple logger configuration @@ -125,9 +124,7 @@ export function setup(options: LoggerConfig | LoggerConfigItem = DEFAULT_LOGGER_ debug('logging file enabled'); logger = createLogger(pinoConfig, pino.destination(loggerConfig.path), loggerConfig.format); } else if (loggerConfig.type === 'rotating-file') { - process.emitWarning( - 'rotating-file type is not longer supported, consider use [logrotate] instead' - ); + warningUtils.emit(warningUtils.Codes.VERWAR003); debug('logging stdout enabled'); logger = createLogger(pinoConfig, pino.destination(1), loggerConfig.format); } else { diff --git a/packages/logger/test/logger.spec.ts b/packages/logger/test/logger.spec.ts index 43b789201..fb6682779 100644 --- a/packages/logger/test/logger.spec.ts +++ b/packages/logger/test/logger.spec.ts @@ -1,7 +1,26 @@ +import { warningUtils } from '@verdaccio/core'; import { logger, setup } from '../src'; +const mockWarningUtils = jest.fn(); + +jest.mock('@verdaccio/core', () => { + const original = jest.requireActual('@verdaccio/core'); + return { + warningUtils: { + ...original.warningUtils, + emit: (...args) => { + mockWarningUtils(...args); + }, + }, + }; +}); + describe('logger', () => { - test('dsadasd', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + test.skip('should write message logger', () => { jest.spyOn(process.stdout, 'write'); setup([ { @@ -15,7 +34,6 @@ describe('logger', () => { }); test('throw deprecation warning if multiple loggers configured', () => { - const spy = jest.spyOn(process, 'emitWarning'); setup([ { level: 'info', @@ -24,18 +42,11 @@ describe('logger', () => { level: 'http', }, ]); - expect(spy).toHaveBeenCalledWith( - 'deprecate: multiple logger configuration is deprecated, please check the migration guide.' - ); - spy.mockRestore(); + expect(mockWarningUtils).toHaveBeenCalledWith(warningUtils.Codes.VERDEP002); }); test('regression: do not throw deprecation warning if no logger config is provided', () => { - const spy = jest.spyOn(process, 'emitWarning'); setup(); - expect(spy).not.toHaveBeenCalledWith( - 'deprecate: multiple logger configuration is deprecated, please check the migration guide.' - ); - spy.mockRestore(); + expect(mockWarningUtils).not.toHaveBeenCalled(); }); }); diff --git a/packages/node-api/src/cli-utils.ts b/packages/node-api/src/cli-utils.ts index ea0dd4805..56a2f1740 100644 --- a/packages/node-api/src/cli-utils.ts +++ b/packages/node-api/src/cli-utils.ts @@ -1,3 +1,5 @@ +import { warningUtils } from '@verdaccio/core'; + export const DEFAULT_PORT = '4873'; export const DEFAULT_PROTOCOL = 'http'; export const DEFAULT_DOMAIN = 'localhost'; @@ -71,11 +73,7 @@ export function getListListenAddresses(argListen: string | void, configListen: a const parsedAddr = parseAddress(addr); if (!parsedAddr) { - process.emitWarning( - // eslint-disable-next-line max-len - `invalid address - ${addr}, we expect a port (e.g. "4873"), host:port (e.g. "localhost:4873") or full url '(e.g. "http://localhost:4873/")` - ); - process.emitWarning('https://verdaccio.org/docs/en/configuration#listen-port'); + warningUtils.emit(warningUtils.Codes.VERWAR004, addr); } return parsedAddr; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fa81c7e50..d310b05ab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -265,6 +265,7 @@ importers: packages/cli: specifiers: '@verdaccio/config': workspace:6.0.0-6-next.9 + '@verdaccio/core': workspace:6.0.0-6-next.2 '@verdaccio/fastify-migration': workspace:6.0.0-6-next.14 '@verdaccio/logger': workspace:6.0.0-6-next.6 '@verdaccio/node-api': workspace:6.0.0-6-next.22 @@ -275,6 +276,7 @@ importers: ts-node: 10.2.1 dependencies: '@verdaccio/config': link:../config + '@verdaccio/core': link:../core/core '@verdaccio/fastify-migration': link:../core/server '@verdaccio/logger': link:../logger '@verdaccio/node-api': link:../node-api @@ -311,10 +313,12 @@ importers: packages/core/core: specifiers: '@verdaccio/types': workspace:11.0.0-6-next.9 + fastify-warning: 0.2.0 http-errors: 1.8.0 http-status-codes: 2.1.4 semver: 7.3.5 dependencies: + fastify-warning: 0.2.0 http-errors: 1.8.0 http-status-codes: 2.1.4 semver: 7.3.5 @@ -481,12 +485,14 @@ importers: packages/logger: specifiers: '@types/pino': 6.3.12 + '@verdaccio/core': workspace:6.0.0-6-next.2 '@verdaccio/logger-prettify': workspace:6.0.0-6-next.4 '@verdaccio/types': workspace:11.0.0-6-next.9 debug: 4.3.2 lodash: 4.17.21 pino: 6.13.3 dependencies: + '@verdaccio/core': link:../core/core '@verdaccio/logger-prettify': link:../logger-prettify debug: 4.3.2 lodash: 4.17.21