Backwards compat. on detctPackage (#517)

This commit is contained in:
Jaco Greeff
2020-02-04 11:38:00 +01:00
committed by GitHub
parent 94bbc6f2ee
commit 90ca8a0d78
3 changed files with 12 additions and 8 deletions
+2 -2
View File
@@ -13,7 +13,7 @@ describe('assertSingletonPackage', (): void => {
it('should not log the first time', (): void => {
const spy = jest.spyOn(console, 'warn');
detectPackage(`${PATH}/01`, { name: PKG, version: VER1 });
detectPackage({ name: PKG, version: VER1 }, `${PATH}/01`);
expect(spy).not.toHaveBeenCalled();
spy.mockRestore();
});
@@ -21,7 +21,7 @@ describe('assertSingletonPackage', (): void => {
it('should log the second time', (): void => {
const spy = jest.spyOn(console, 'warn');
detectPackage(`${PATH}/02`, { name: PKG, version: VER2 });
detectPackage({ name: PKG, version: VER2 }, `${PATH}/02`);
expect(spy).toHaveBeenCalledWith(`Multiple instances of @polkadot/util detected, ensure that there is only one package in your dependency tree.\n\t${VER1}\t${PATH}/01\n\t${VER2} \t${PATH}/02`);
spy.mockRestore();
});
+8 -4
View File
@@ -14,7 +14,7 @@ interface PackageJson {
}
interface VersionPath {
path: string;
path?: string;
version: string;
}
@@ -25,17 +25,21 @@ interface PjsChecks extends This {
type PjsGlobal = NodeJS.Global & PjsChecks;
type PjsWindow = Window & PjsChecks;
function expandPath (path?: string): string {
return (!path || path.length < 5) ? '<unknown path>' : path;
}
/** @internal */
function flattenVersions (_all: (VersionPath | string)[]): string {
const all: VersionPath[] = _all.map((version: VersionPath | string): VersionPath =>
isString(version)
? { path: '', version }
? { version }
: version
);
const verLength = all.reduce((max, { version }): number => Math.max(max, version.length), 0);
return all
.map(({ path, version }): string => `\t${version.padEnd(verLength)}\t${path}`)
.map(({ path, version }): string => `\t${version.padEnd(verLength)}\t${expandPath(path)}`)
.join('\n');
}
@@ -43,7 +47,7 @@ function flattenVersions (_all: (VersionPath | string)[]): string {
* @name detectPackage
* @summary Checks that a specific package is only imported once
*/
export default function detectPackage (path: string, { name, version }: PackageJson): void {
export default function detectPackage ({ name, version }: PackageJson, path?: string): void {
const _global = typeof window !== 'undefined'
? window as PjsWindow
: global as PjsGlobal;