Skip to content

Commit

Permalink
Bump dev w/ detectPackage adjustments (#1903)
Browse files Browse the repository at this point in the history
* Bump dev w/ detectPackage adjustments

* Fix spec import

* Adjust line flow
  • Loading branch information
jacogr authored Dec 19, 2023
1 parent 4da1e8b commit 2dc1716
Show file tree
Hide file tree
Showing 23 changed files with 192 additions and 219 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"test:one": "polkadot-dev-run-test --env browser"
},
"devDependencies": {
"@polkadot/dev": "^0.78.1",
"@polkadot/dev": "^0.78.2",
"@types/node": "^20.10.5"
},
"resolutions": {
Expand Down
4 changes: 0 additions & 4 deletions packages/hw-ledger-transports/src/detectOther.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

// Do not edit, auto-generated by @polkadot/dev
// (packageInfo imports will be kept as-is, user-editable)

import { detectPackage } from '@polkadot/util';

import others from './detectOther.js';
import { packageInfo } from './packageInfo.js';

detectPackage(packageInfo, null, others);
detectPackage(packageInfo, null, []);
11 changes: 0 additions & 11 deletions packages/hw-ledger/src/detectPackage.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/hw-ledger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2017-2023 @polkadot/hw-ledger authors & contributors
// SPDX-License-Identifier: Apache-2.0

import './detectPackage.js';
import './packageDetect.js';

export * from './bundle.js';
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Copyright 2017-2023 @polkadot/hw-ledger authors & contributors
// SPDX-License-Identifier: Apache-2.0

// Do not edit, auto-generated by @polkadot/dev
// (packageInfo imports will be kept as-is, user-editable)

import { packageInfo as transportInfo } from '@polkadot/hw-ledger-transports/packageInfo';
import { detectPackage } from '@polkadot/util';
import { packageInfo as utilInfo } from '@polkadot/util/packageInfo';

export default [transportInfo, utilInfo];
import { packageInfo } from './packageInfo.js';

detectPackage(packageInfo, null, [transportInfo, utilInfo]);
7 changes: 0 additions & 7 deletions packages/keyring/src/detectOther.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/keyring/src/detectPackage.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/keyring/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017-2023 @polkadot/keyring authors & contributors
// SPDX-License-Identifier: Apache-2.0

import './detectPackage.js';
import './packageDetect.js';

import { Keyring } from './bundle.js';

Expand Down
13 changes: 13 additions & 0 deletions packages/keyring/src/packageDetect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2017-2023 @polkadot/keyring authors & contributors
// SPDX-License-Identifier: Apache-2.0

// Do not edit, auto-generated by @polkadot/dev
// (packageInfo imports will be kept as-is, user-editable)

import { detectPackage } from '@polkadot/util';
import { packageInfo as utilInfo } from '@polkadot/util/packageInfo';
import { packageInfo as cryptoInfo } from '@polkadot/util-crypto/packageInfo';

import { packageInfo } from './packageInfo.js';

detectPackage(packageInfo, null, [cryptoInfo, utilInfo]);
4 changes: 0 additions & 4 deletions packages/networks/src/detectOther.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/networks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// TODO: This was removed, really cannot recall the reason...
// ... put it back, but keep it removed
// import './detectPackage';
// import './packageDetect.js';

export * from './interfaces.js';
export { packageInfo } from './packageInfo.js';
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

// Do not edit, auto-generated by @polkadot/dev
// (packageInfo imports will be kept as-is, user-editable)

import { detectPackage } from '@polkadot/util';

import others from './detectOther.js';
import { packageInfo } from './packageInfo.js';

detectPackage(packageInfo, null, others);
detectPackage(packageInfo, null, []);
11 changes: 0 additions & 11 deletions packages/util-crypto/src/detectPackage.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/util-crypto/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0

import './detectPackage.js';
import './packageDetect.js';

export * from './bundle.js';
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0

// Do not edit, auto-generated by @polkadot/dev
// (packageInfo imports will be kept as-is, user-editable)

import { packageInfo as netInfo } from '@polkadot/networks/packageInfo';
import { detectPackage } from '@polkadot/util';
import { packageInfo as utilInfo } from '@polkadot/util/packageInfo';
import { packageInfo as randomInfo } from '@polkadot/x-randomvalues';

export default [netInfo, utilInfo, randomInfo];
import { packageInfo } from './packageInfo.js';

detectPackage(packageInfo, null, [netInfo, randomInfo, utilInfo]);
2 changes: 1 addition & 1 deletion packages/util/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from './bi/index.js';
export * from './bn/index.js';
export * from './buffer/index.js';
export * from './compact/index.js';
export * from './detectPackage.js';
export * from './extractTime.js';
export * from './float/index.js';
export * from './format/index.js';
Expand All @@ -32,4 +33,3 @@ export * from './promisify.js';
export * from './string/index.js';
export * from './stringify.js';
export * from './u8a/index.js';
export * from './versionDetect.js';
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// <reference types="@polkadot/dev-test/globals.d.ts" />

import { detectPackage, POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG } from './versionDetect.js';
import { detectPackage, POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG } from './detectPackage.js';

describe('detectPackage', (): void => {
const PKG = '@polkadot/util';
Expand Down
138 changes: 134 additions & 4 deletions packages/util/src/detectPackage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,138 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import others from './detectOther.js';
import { packageInfo } from './packageInfo.js';
import { detectPackage } from './versionDetect.js';
import { xglobal } from '@polkadot/x-global';

detectPackage(packageInfo, null, others);
import { isFunction } from './is/function.js';

type This = typeof globalThis;

interface VersionPath {
path: string;
type: string;
version: string;
}

interface PackageInfo extends VersionPath {
name: string;
}

interface PjsChecks extends This {
__polkadotjs: Record<string, VersionPath[]>;
}

type PjsGlobal = This & PjsChecks & Record<string, unknown>;
type FnString = () => string | undefined;

const DEDUPE = 'Either remove and explicitly install matching versions or dedupe using your package manager.\nThe following conflicting packages were found:';

export const POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG = 'POLKADOTJS_DISABLE_ESM_CJS_WARNING';

/** @internal */
function getEntry (name: string): VersionPath[] {
const _global = xglobal as PjsGlobal;

if (!_global.__polkadotjs) {
_global.__polkadotjs = {};
}

if (!_global.__polkadotjs[name]) {
_global.__polkadotjs[name] = [];
}

return _global.__polkadotjs[name];
}

/** @internal */
function formatDisplay <T extends { version: string }> (all: T[], fmt: (version: string, data: T) => string[]): string {
let max = 0;

for (let i = 0, count = all.length; i < count; i++) {
max = Math.max(max, all[i].version.length);
}

return all
.map((d) => `\t${fmt(d.version.padEnd(max), d).join('\t')}`)
.join('\n');
}

/** @internal */
function formatInfo (version: string, { name }: PackageInfo): string[] {
return [
version,
name
];
}

/** @internal */
function formatVersion (version: string, { path, type }: VersionPath): string[] {
let extracted: string;

if (path && path.length >= 5) {
const nmIndex = path.indexOf('node_modules');

extracted = nmIndex === -1
? path
: path.substring(nmIndex);
} else {
extracted = '<unknown>';
}

return [
`${`${type || ''}`.padStart(3)} ${version}`,
extracted
];
}

/** @internal */
function getPath (infoPath?: string, pathOrFn?: FnString | string | false | null): string {
if (infoPath) {
return infoPath;
} else if (isFunction(pathOrFn)) {
try {
return pathOrFn() || '';
} catch {
return '';
}
}

return pathOrFn || '';
}

/** @internal */
function warn <T extends { version: string }> (pre: string, all: T[], fmt: (version: string, data: T) => string[]): void {
console.warn(`${pre}\n${DEDUPE}\n${formatDisplay(all, fmt)}`);
}

/**
* @name detectPackage
* @summary Checks that a specific package is only imported once
* @description A `@polkadot/*` version detection utility, checking for one occurrence of a package in addition to checking for dependency versions.
*/
export function detectPackage ({ name, path, type, version }: PackageInfo, pathOrFn?: FnString | string | false | null, deps: PackageInfo[] = []): void {
if (!name.startsWith('@polkadot')) {
throw new Error(`Invalid package descriptor ${name}`);
}

const entry = getEntry(name);

entry.push({ path: getPath(path, pathOrFn), type, version });

// if we have more than one entry at DIFFERENT version types then warn. If there is
// more than one entry at the same version and ESM/CJS dual warnings are disabled,
// then do not display warnings
const entriesSameVersion = entry.every((e) => e.version === version);
const esmCjsWarningDisabled = xglobal.process?.env?.[POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG] === '1';
const multipleEntries = entry.length !== 1;
const disableWarnings = esmCjsWarningDisabled && entriesSameVersion;

if (multipleEntries && !disableWarnings) {
warn(`${name} has multiple versions, ensure that there is only one installed.`, entry, formatVersion);
} else {
const mismatches = deps.filter((d) => d && d.version !== version);

if (mismatches.length) {
warn(`${name} requires direct dependencies exactly matching version ${version}.`, mismatches, formatInfo);
}
}
}
2 changes: 1 addition & 1 deletion packages/util/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import './detectPackage.js';
import './packageDetect.js';

export * from './bundle.js';
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
import { packageInfo as decoderInfo } from '@polkadot/x-textdecoder';
import { packageInfo as encoderInfo } from '@polkadot/x-textencoder';

export default [decoderInfo, encoderInfo];
import { detectPackage } from './detectPackage.js';
import { packageInfo } from './packageInfo.js';

detectPackage(packageInfo, null, [decoderInfo, encoderInfo]);
Loading

0 comments on commit 2dc1716

Please sign in to comment.