Adjust detectPackage to handle (optional) type/path (#1360)

* Adjust detectPackage to handle (optional) type/path

* Bump dev

* Adjust expectations
This commit is contained in:
Jaco
2022-01-05 11:32:09 +02:00
committed by GitHub
parent 74f2a1e24c
commit a8cda40af9
4 changed files with 21 additions and 18 deletions
+4 -4
View File
@@ -13,8 +13,8 @@ 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:
\tcjs ${VER1}\t${PATH}/01
\tcjs ${VER2} \t${PATH}/02`;
\t ${VER1}\t${PATH}/01
\tesm ${VER2} \t${PATH}/02`;
it('should not log the first time', (): void => {
const spy = jest.spyOn(console, 'warn');
@@ -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, version: VER2 }, `${PATH}/02`);
detectPackage({ name: PKG, type: 'esm', version: VER2 }, `${PATH}/02`);
expect(spy).toHaveBeenCalledWith(MISMATCH);
spy.mockRestore();
});
@@ -35,7 +35,7 @@ The following conflicting packages were found:
it('should allow for function use', (): void => {
const spy = jest.spyOn(console, 'warn');
detectPackage({ name: PKG, version: VER3 }, () => `${PATH}/03`);
detectPackage({ name: PKG, type: 'cjs', version: VER3 }, () => `${PATH}/03`);
expect(spy).toHaveBeenCalledWith(`${MISMATCH}
\tcjs ${VER3} \t${PATH}/03`);
spy.mockRestore();
+11 -8
View File
@@ -6,18 +6,19 @@ import { xglobal } from '@polkadot/x-global';
import { isFunction } from './is/function';
import { isString } from './is/string';
import { assert } from './assert';
import { hasEsm as isEsm } from './has';
type This = typeof globalThis;
interface PackageJson {
name: string;
path?: string;
type?: string;
version: string;
}
interface VersionPath {
isEsm?: boolean;
path?: string;
type?: string;
version: string;
}
@@ -72,15 +73,17 @@ function flattenVersions (entry: VersionPath[]): string {
: version;
const all = entry.map(toPath);
const verLength = getVersionLength(all);
const stringify = ({ isEsm, path, version }: VersionPath) =>
`\t${isEsm ? 'esm' : 'cjs'} ${version.padEnd(verLength)}\t${(!path || path.length < 5) ? '<unknown>' : path}`;
const stringify = ({ path, type, version }: VersionPath) =>
`\t${`${type || ''}`.padStart(3)} ${version.padEnd(verLength)}\t${(!path || path.length < 5) ? '<unknown>' : path}`;
return all.map(stringify).join('\n');
}
/** @internal */
function getPath (pathOrFn?: FnString | string | false): string {
if (isFunction(pathOrFn)) {
function getPath (infoPath?: string, pathOrFn?: FnString | string | false): string {
if (infoPath) {
return infoPath;
} else if (isFunction(pathOrFn)) {
try {
return pathOrFn() || '';
} catch (error) {
@@ -96,12 +99,12 @@ function getPath (pathOrFn?: FnString | string | false): string {
* @summary Checks that a specific package is only imported once
* @description A `@polkadot/*` version detection utility, checking for one occurence of a package in addition to checking for ddependency versions.
*/
export function detectPackage ({ name, version }: PackageJson, pathOrFn?: FnString | string | false, deps: PackageJson[] = []): void {
export function detectPackage ({ name, path, type, version }: PackageJson, pathOrFn?: FnString | string | false, deps: PackageJson[] = []): void {
assert(name.startsWith('@polkadot'), () => `Invalid package descriptor ${name}`);
const entry = getEntry(name);
entry.push({ isEsm, path: getPath(pathOrFn), version });
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)}`);