Allow detectPackage with a function (#811)
This commit is contained in:
@@ -7,7 +7,10 @@ describe('assertSingletonPackage', (): void => {
|
||||
const PKG = '@polkadot/util';
|
||||
const VER1 = '9.8.0-beta.45';
|
||||
const VER2 = '9.7.1';
|
||||
const VER3 = '9.6.1';
|
||||
const PATH = '/Users/jaco/Projects/polkadot-js/api/node_modules/@polkadot/util';
|
||||
const RES2 = `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`;
|
||||
const RES3 = `${RES2}\n\t${VER3} \t${PATH}/03`;
|
||||
|
||||
it('should not log the first time', (): void => {
|
||||
const spy = jest.spyOn(console, 'warn');
|
||||
@@ -21,7 +24,15 @@ describe('assertSingletonPackage', (): void => {
|
||||
const spy = jest.spyOn(console, 'warn');
|
||||
|
||||
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`);
|
||||
expect(spy).toHaveBeenCalledWith(RES2);
|
||||
spy.mockRestore();
|
||||
});
|
||||
|
||||
it('should allow for function use', (): void => {
|
||||
const spy = jest.spyOn(console, 'warn');
|
||||
|
||||
detectPackage({ name: PKG, version: VER3 }, () => `${PATH}/03`);
|
||||
expect(spy).toHaveBeenCalledWith(RES3);
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isFunction } from './is/function';
|
||||
import { isString } from './is/string';
|
||||
import { assert } from './assert';
|
||||
|
||||
@@ -23,6 +24,7 @@ interface PjsChecks extends This {
|
||||
|
||||
type PjsGlobal = NodeJS.Global & PjsChecks;
|
||||
type PjsWindow = Window & PjsChecks;
|
||||
type FnString = () => string | undefined;
|
||||
|
||||
function expandPath (path?: string): string {
|
||||
return (!path || path.length < 5) ? '<unknown>' : path;
|
||||
@@ -42,11 +44,24 @@ function flattenVersions (_all: (VersionPath | string)[]): string {
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function getPath (pathOrFn?: FnString | string | false): false | string | undefined {
|
||||
if (isFunction(pathOrFn)) {
|
||||
try {
|
||||
return pathOrFn();
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
return pathOrFn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name detectPackage
|
||||
* @summary Checks that a specific package is only imported once
|
||||
*/
|
||||
export function detectPackage ({ name, version }: PackageJson, path?: string | false): void {
|
||||
export function detectPackage ({ name, version }: PackageJson, pathOrFn?: FnString | string | false): void {
|
||||
const _global = typeof window !== 'undefined'
|
||||
? window as PjsWindow
|
||||
: global as PjsGlobal;
|
||||
@@ -57,6 +72,8 @@ export function detectPackage ({ name, version }: PackageJson, path?: string | f
|
||||
|
||||
assert(name.startsWith('@polkadot'), `Invalid package descriptor ${name}`);
|
||||
|
||||
const path = getPath(pathOrFn);
|
||||
|
||||
_global.__polkadotjs[name] = [...(_global.__polkadotjs[name] || []), { path: path || '', version }];
|
||||
|
||||
if (_global.__polkadotjs[name].length !== 1) {
|
||||
|
||||
Reference in New Issue
Block a user