Introduce detectPackage (with versioning) (#508)
* Introduce detectPackage (with versioning) * Update expectation
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
- Update schnorrkel to 0.8.5 (Full Substrate 2.x support)
|
||||
- Remove Alice session (ed2551) account from testing keyring (not applicable to Substrate 2.x)
|
||||
- Remove `chainspec`, `db`, `trie-codec`, `trie-db` and `trie-hash` packages (moved to client)
|
||||
- Renamed `assertSingletonPackage` to `detectPackage` with inclusion of version listing
|
||||
|
||||
# 1.8.1
|
||||
|
||||
|
||||
@@ -2,12 +2,20 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { assertSingletonPackage } from '@polkadot/util';
|
||||
import { detectPackage } from '@polkadot/util';
|
||||
import { decodeAddress, encodeAddress, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
import Keyring from './keyring';
|
||||
|
||||
assertSingletonPackage('@polkadot/keyring');
|
||||
let pkgJson;
|
||||
|
||||
try {
|
||||
pkgJson = require('./package.json');
|
||||
} catch (error) {
|
||||
pkgJson = require('../package.json');
|
||||
}
|
||||
|
||||
detectPackage(pkgJson);
|
||||
|
||||
export default Keyring;
|
||||
|
||||
|
||||
+4
-6
@@ -2,15 +2,13 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { assertSingletonPackage } from '.';
|
||||
import { detectPackage } from '.';
|
||||
|
||||
describe('assertSingletonPackage', (): void => {
|
||||
const NAME = 'assertSingletonPackage';
|
||||
|
||||
it('should not log the first time', (): void => {
|
||||
const spy = jest.spyOn(console, 'warn');
|
||||
|
||||
assertSingletonPackage(NAME);
|
||||
detectPackage({ name: '@polkadot/util', version: 'a.b.c-1st' });
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
spy.mockRestore();
|
||||
});
|
||||
@@ -18,8 +16,8 @@ describe('assertSingletonPackage', (): void => {
|
||||
it('should log the second time', (): void => {
|
||||
const spy = jest.spyOn(console, 'warn');
|
||||
|
||||
assertSingletonPackage(NAME);
|
||||
expect(spy).toHaveBeenCalledWith('Multiple versions of assertSingletonPackage detected, ensure that there is only one version in your dependency tree');
|
||||
detectPackage({ name: '@polkadot/util', version: 'a.b.c-2nd' });
|
||||
expect(spy).toHaveBeenCalledWith('Multiple versions of @polkadot/util detected: a.b.c-1st, a.b.c-2nd. Ensure that there is only one version in your dependency tree');
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
@@ -2,23 +2,28 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import isUndefined from './is/undefined';
|
||||
import assert from './assert';
|
||||
|
||||
// eslint-disable-next-line no-undef
|
||||
type This = typeof globalThis;
|
||||
|
||||
interface PjsChecks extends This {
|
||||
__polkadotjs: Record<string, boolean>;
|
||||
__polkadotjs: Record<string, string[]>;
|
||||
}
|
||||
|
||||
interface PackageJson {
|
||||
name: string;
|
||||
version: string;
|
||||
}
|
||||
|
||||
type PjsGlobal = NodeJS.Global & PjsChecks;
|
||||
type PjsWindow = Window & PjsChecks;
|
||||
|
||||
/**
|
||||
* @name assertSingletonPackage
|
||||
* @name detectPackage
|
||||
* @summary Checks that a specific package is only imported once
|
||||
*/
|
||||
export default function assertSingletonPackage (name: string): void {
|
||||
export default function detectPackage ({ name, version }: PackageJson): void {
|
||||
const _global = typeof window !== 'undefined'
|
||||
? window as PjsWindow
|
||||
: global as PjsGlobal;
|
||||
@@ -27,9 +32,11 @@ export default function assertSingletonPackage (name: string): void {
|
||||
_global.__polkadotjs = {};
|
||||
}
|
||||
|
||||
if (!isUndefined(_global.__polkadotjs[name])) {
|
||||
console.warn(`Multiple versions of ${name} detected, ensure that there is only one version in your dependency tree`);
|
||||
}
|
||||
assert(name.startsWith('@polkadot'), `Invalid package descriptor ${name}`);
|
||||
|
||||
_global.__polkadotjs[name] = true;
|
||||
_global.__polkadotjs[name] = [...(_global.__polkadotjs[name] || []), version];
|
||||
|
||||
if (_global.__polkadotjs[name].length !== 1) {
|
||||
console.warn(`Multiple versions of ${name} detected: ${_global.__polkadotjs[name].join(', ')}. Ensure that there is only one version in your dependency tree`);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import './polyfill';
|
||||
*/
|
||||
|
||||
export { default as assert } from './assert';
|
||||
export { default as assertSingletonPackage } from './assertSingletonPackage';
|
||||
export { default as detectPackage } from './detectPackage';
|
||||
export { default as logger } from './logger';
|
||||
export { default as promisify } from './promisify';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user