Adjust detectPackage to handle (optional) type/path (#1360)
* Adjust detectPackage to handle (optional) type/path * Bump dev * Adjust expectations
This commit is contained in:
+1
-1
@@ -29,7 +29,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.16.7",
|
||||
"@polkadot/dev": "^0.65.12",
|
||||
"@polkadot/dev": "^0.65.13",
|
||||
"@types/jest": "^27.4.0"
|
||||
},
|
||||
"resolutions": {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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)}`);
|
||||
|
||||
@@ -1958,9 +1958,9 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/dev@npm:^0.65.12":
|
||||
version: 0.65.12
|
||||
resolution: "@polkadot/dev@npm:0.65.12"
|
||||
"@polkadot/dev@npm:^0.65.13":
|
||||
version: 0.65.13
|
||||
resolution: "@polkadot/dev@npm:0.65.13"
|
||||
dependencies:
|
||||
"@babel/cli": ^7.16.7
|
||||
"@babel/core": ^7.16.7
|
||||
@@ -2046,7 +2046,7 @@ __metadata:
|
||||
polkadot-exec-rollup: scripts/polkadot-exec-rollup.mjs
|
||||
polkadot-exec-tsc: scripts/polkadot-exec-tsc.mjs
|
||||
polkadot-exec-webpack: scripts/polkadot-exec-webpack.mjs
|
||||
checksum: 8cb36b8057af0bb07d519fc6dcb69a41eb167e62ce01c15041631c3cfeeac76b5c9060ab607e258f00ba6b8a35ca0d204db33671cf54c858f05beda2c5668909
|
||||
checksum: 00508f66616867bf4bacaad6081924397386cb2cdc682ad6d24afe10061417bb07953e379b7027a6eba92e6f34c0b63feace821550675b918843549746b1093e
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -9175,7 +9175,7 @@ resolve@^2.0.0-next.3:
|
||||
resolution: "root-workspace-0b6124@workspace:."
|
||||
dependencies:
|
||||
"@babel/core": ^7.16.7
|
||||
"@polkadot/dev": ^0.65.12
|
||||
"@polkadot/dev": ^0.65.13
|
||||
"@types/jest": ^27.4.0
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
Reference in New Issue
Block a user