From f763a66219fbb1989db12ff7ffcec01b84194c4f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 17 Dec 2020 12:07:00 +0100 Subject: [PATCH] Allow detectPackage with a function (#811) --- packages/keyring/package.json | 1 + packages/keyring/src/detectPackage.js | 2 +- packages/keyring/src/detectPackage.mjs | 2 +- packages/networks/package.json | 1 + packages/util-crypto/package.json | 1 + packages/util-crypto/src/detectPackage.js | 2 +- packages/util-crypto/src/detectPackage.mjs | 2 +- packages/util/package.json | 1 + packages/util/src/detectPackage.spec.ts | 13 ++++++++++++- packages/util/src/detectPackage.ts | 19 ++++++++++++++++++- 10 files changed, 38 insertions(+), 6 deletions(-) diff --git a/packages/keyring/package.json b/packages/keyring/package.json index 3d830a3a7..ba899dfd1 100644 --- a/packages/keyring/package.json +++ b/packages/keyring/package.json @@ -7,6 +7,7 @@ "contributors": [], "license": "Apache-2.0", "main": "index.js", + "module": "index.mjs", "sideEffects": false, "repository": "github:polkadot-js/common", "bugs": "https://github.com/polkadot-js/common/issues", diff --git a/packages/keyring/src/detectPackage.js b/packages/keyring/src/detectPackage.js index 761c27e23..306a6e6d9 100644 --- a/packages/keyring/src/detectPackage.js +++ b/packages/keyring/src/detectPackage.js @@ -5,6 +5,6 @@ const util = require('@polkadot/util'); const packageInfo = require('./package-info.json'); -util.detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname); +util.detectPackage(packageInfo, () => __dirname); exports.packageInfo = packageInfo; diff --git a/packages/keyring/src/detectPackage.mjs b/packages/keyring/src/detectPackage.mjs index 9a79f7e72..d7ffdc8c7 100644 --- a/packages/keyring/src/detectPackage.mjs +++ b/packages/keyring/src/detectPackage.mjs @@ -5,6 +5,6 @@ import { detectPackage } from '@polkadot/util'; import packageInfo from './package-info.json'; -detectPackage(packageInfo, import.meta.url); +detectPackage(packageInfo, () => import.meta.url); export { packageInfo }; diff --git a/packages/networks/package.json b/packages/networks/package.json index 50a5a25cd..7f2dc7338 100644 --- a/packages/networks/package.json +++ b/packages/networks/package.json @@ -3,6 +3,7 @@ "version": "5.0.2-2", "description": "A list of all available Substrate networks and their applicable prefixes", "main": "index.js", + "module": "index.mjs", "sideEffects": false, "repository": "github:polkadot-js/common", "author": "Jaco Greeff ", diff --git a/packages/util-crypto/package.json b/packages/util-crypto/package.json index 2c19e81f7..303117c44 100644 --- a/packages/util-crypto/package.json +++ b/packages/util-crypto/package.json @@ -3,6 +3,7 @@ "version": "5.0.2-2", "description": "A collection of useful crypto utilities for @polkadot", "main": "index.js", + "module": "index.mjs", "sideEffects": false, "author": "Jaco Greeff ", "maintainers": [], diff --git a/packages/util-crypto/src/detectPackage.js b/packages/util-crypto/src/detectPackage.js index af7f9d378..2421b28f8 100644 --- a/packages/util-crypto/src/detectPackage.js +++ b/packages/util-crypto/src/detectPackage.js @@ -5,6 +5,6 @@ const util = require('@polkadot/util'); const packageInfo = require('./package-info.json'); -util.detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname); +util.detectPackage(packageInfo, () => __dirname); exports.packageInfo = packageInfo; diff --git a/packages/util-crypto/src/detectPackage.mjs b/packages/util-crypto/src/detectPackage.mjs index afc2668da..482f45949 100644 --- a/packages/util-crypto/src/detectPackage.mjs +++ b/packages/util-crypto/src/detectPackage.mjs @@ -5,6 +5,6 @@ import { detectPackage } from '@polkadot/util'; import packageInfo from './package-info.json'; -detectPackage(packageInfo, import.meta.url); +detectPackage(packageInfo, () => import.meta.url); export { packageInfo }; diff --git a/packages/util/package.json b/packages/util/package.json index 13ca3202d..ae9f3fb67 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -3,6 +3,7 @@ "version": "5.0.2-2", "description": "A collection of useful utilities for @polkadot", "main": "index.js", + "module": "index.mjs", "author": "Jaco Greeff ", "maintainers": [], "contributors": [], diff --git a/packages/util/src/detectPackage.spec.ts b/packages/util/src/detectPackage.spec.ts index 9f83fffd9..2290aaf8d 100644 --- a/packages/util/src/detectPackage.spec.ts +++ b/packages/util/src/detectPackage.spec.ts @@ -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(); }); }); diff --git a/packages/util/src/detectPackage.ts b/packages/util/src/detectPackage.ts index ae894673a..fc9f6305e 100644 --- a/packages/util/src/detectPackage.ts +++ b/packages/util/src/detectPackage.ts @@ -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) ? '' : 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) {