diff --git a/CHANGELOG.md b/CHANGELOG.md index 9761bcb7b..bc87d3aba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Changes: + +- Adjust/dedupe output from `detectPackage` + + ## 9.0.1 Apr 9, 2022 - **Breaking change** In this major version the commonjs outputs are moved to a sub-folder. Since the export map and main field in package.json does reflect this change, there should be no usage changes. However the packages here will all need to be on the same version for internal linkage. diff --git a/packages/util/src/versionDetect.spec.ts b/packages/util/src/versionDetect.spec.ts index 52be17a32..9fda316f7 100644 --- a/packages/util/src/versionDetect.spec.ts +++ b/packages/util/src/versionDetect.spec.ts @@ -13,13 +13,13 @@ describe('detectPackage', (): void => { const MISMATCH = `@polkadot/util has multiple versions, ensure that there is only one installed. Either remove and explicitly install matching versions or dedupe using your package manager. The following conflicting packages were found: -\t ${VER1}\t${PATH}/01 -\tesm ${VER2} \tpath/02`; +\t ${VER1}\tnode_modules/@polkadot/util +\tesm ${VER2} \tnode_modules/@polkadot/api/node_modules/@polkadot/util`; it('should not log the first time', (): void => { const spy = jest.spyOn(console, 'warn'); - detectPackage({ name: PKG, version: VER1 }, `${PATH}/01`); + detectPackage({ name: PKG, version: VER1 }, PATH); expect(spy).not.toHaveBeenCalled(); spy.mockRestore(); }); @@ -27,7 +27,7 @@ The following conflicting packages were found: it('should log the second time', (): void => { const spy = jest.spyOn(console, 'warn'); - detectPackage({ name: PKG, path: 'path/02', type: 'esm', version: VER2 }); + detectPackage({ name: PKG, path: '/Users/jaco/Projects/polkadot-js/api/node_modules/@polkadot/api/node_modules/@polkadot/util', type: 'esm', version: VER2 }); expect(spy).toHaveBeenCalledWith(MISMATCH); spy.mockRestore(); }); @@ -35,9 +35,9 @@ The following conflicting packages were found: it('should allow for function use', (): void => { const spy = jest.spyOn(console, 'warn'); - detectPackage({ name: PKG, type: 'cjs', version: VER3 }, () => `${PATH}/03`); + detectPackage({ name: PKG, type: 'cjs', version: VER3 }, () => PATH); expect(spy).toHaveBeenCalledWith(`${MISMATCH} -\tcjs ${VER3} \t${PATH}/03`); +\tcjs ${VER3} \tnode_modules/@polkadot/util`); spy.mockRestore(); }); }); diff --git a/packages/util/src/versionDetect.ts b/packages/util/src/versionDetect.ts index 3307e4647..84f63dbd7 100644 --- a/packages/util/src/versionDetect.ts +++ b/packages/util/src/versionDetect.ts @@ -4,7 +4,6 @@ import { xglobal } from '@polkadot/x-global'; import { isFunction } from './is/function'; -import { isString } from './is/string'; import { assert } from './assert'; type This = typeof globalThis; @@ -26,14 +25,14 @@ interface PjsChecks extends This { __polkadotjs: Record; } -type PjsWindow = (Window & This) & PjsChecks; +type PjsGlobal = (Window & This) & PjsChecks; type FnString = () => string | undefined; const DEDUPE = 'Either remove and explicitly install matching versions or dedupe using your package manager.\nThe following conflicting packages were found:'; /** @internal */ function getEntry (name: string): VersionPath[] { - const _global = xglobal as PjsWindow; + const _global = xglobal as PjsGlobal; if (!_global.__polkadotjs) { _global.__polkadotjs = {}; @@ -46,37 +45,45 @@ function getEntry (name: string): VersionPath[] { return _global.__polkadotjs[name]; } -function getVersionLength (all: { version: string }[]): number { - let length = 0; +/** @internal */ +function formatDisplay (all: T[], fmt: (version: string, data: T) => string[]): string { + let max = 0; - for (const { version } of all) { - length = Math.max(length, version.length); + for (let i = 0; i < all.length; i++) { + max = Math.max(max, all[i].version.length); } - return length; + return all + .map((d) => `\t${fmt(d.version.padEnd(max), d).join('\t')}`) + .join('\n'); } /** @internal */ -function flattenInfos (all: PackageJson[]): string { - const verLength = getVersionLength(all); - const stringify = ({ name, version }: PackageJson) => - `\t${version.padEnd(verLength)}\t${name}`; - - return all.map(stringify).join('\n'); +function formatInfo (version: string, { name }: PackageJson): string[] { + return [ + version, + name + ]; } /** @internal */ -function flattenVersions (entry: VersionPath[]): string { - const toPath = (version: VersionPath | string): VersionPath => - isString(version) - ? { version } - : version; - const all = entry.map(toPath); - const verLength = getVersionLength(all); - const stringify = ({ path, type, version }: VersionPath) => - `\t${`${type || ''}`.padStart(3)} ${version.padEnd(verLength)}\t${(!path || path.length < 5) ? '' : path}`; +function formatVersion (version: string, { path, type }: VersionPath): string[] { + let extracted: string; - return all.map(stringify).join('\n'); + if (path && path.length >= 5) { + const nmIndex = path.indexOf('node_modules'); + + extracted = nmIndex === -1 + ? path + : path.substring(nmIndex); + } else { + extracted = ''; + } + + return [ + `${`${type || ''}`.padStart(3)} ${version}`, + extracted + ]; } /** @internal */ @@ -94,6 +101,11 @@ function getPath (infoPath?: string, pathOrFn?: FnString | string | false | null return pathOrFn || ''; } +/** @internal */ +function warn (pre: string, all: T[], fmt: (version: string, data: T) => string[]): void { + console.warn(`${pre}\n${DEDUPE}\n${formatDisplay(all, fmt)}`); +} + /** * @name detectPackage * @summary Checks that a specific package is only imported once @@ -107,12 +119,12 @@ export function detectPackage ({ name, path, type, version }: PackageJson, pathO entry.push({ path: getPath(path, pathOrFn), type, version }); if (entry.length !== 1) { - console.warn(`${name} has multiple versions, ensure that there is only one installed.\n${DEDUPE}\n${flattenVersions(entry)}`); + warn(`${name} has multiple versions, ensure that there is only one installed.`, entry, formatVersion); } else { const mismatches = deps.filter((d) => d && d.version !== version); if (mismatches.length) { - console.warn(`${name} requires direct dependencies exactly matching version ${version}.\n${DEDUPE}\n${flattenInfos(mismatches)}`); + warn(`${name} requires direct dependencies exactly matching version ${version}.`, mismatches, formatInfo); } } }