UNPKG

3.31 kBJavaScriptView Raw
1import { xglobal } from '@polkadot/x-global';
2import { isFunction } from './is/function.js';
3const DEDUPE = 'Either remove and explicitly install matching versions or dedupe using your package manager.\nThe following conflicting packages were found:';
4export const POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG = 'POLKADOTJS_DISABLE_ESM_CJS_WARNING';
5/** @internal */
6function getEntry(name) {
7 const _global = xglobal;
8 if (!_global.__polkadotjs) {
9 _global.__polkadotjs = {};
10 }
11 if (!_global.__polkadotjs[name]) {
12 _global.__polkadotjs[name] = [];
13 }
14 return _global.__polkadotjs[name];
15}
16/** @internal */
17function formatDisplay(all, fmt) {
18 let max = 0;
19 for (let i = 0, count = all.length; i < count; i++) {
20 max = Math.max(max, all[i].version.length);
21 }
22 return all
23 .map((d) => `\t${fmt(d.version.padEnd(max), d).join('\t')}`)
24 .join('\n');
25}
26/** @internal */
27function formatInfo(version, { name }) {
28 return [
29 version,
30 name
31 ];
32}
33/** @internal */
34function formatVersion(version, { path, type }) {
35 let extracted;
36 if (path && path.length >= 5) {
37 const nmIndex = path.indexOf('node_modules');
38 extracted = nmIndex === -1
39 ? path
40 : path.substring(nmIndex);
41 }
42 else {
43 extracted = '<unknown>';
44 }
45 return [
46 `${`${type || ''}`.padStart(3)} ${version}`,
47 extracted
48 ];
49}
50/** @internal */
51function getPath(infoPath, pathOrFn) {
52 if (infoPath) {
53 return infoPath;
54 }
55 else if (isFunction(pathOrFn)) {
56 try {
57 return pathOrFn() || '';
58 }
59 catch {
60 return '';
61 }
62 }
63 return pathOrFn || '';
64}
65/** @internal */
66function warn(pre, all, fmt) {
67 console.warn(`${pre}\n${DEDUPE}\n${formatDisplay(all, fmt)}`);
68}
69/**
70 * @name detectPackage
71 * @summary Checks that a specific package is only imported once
72 * @description A `@polkadot/*` version detection utility, checking for one occurrence of a package in addition to checking for dependency versions.
73 */
74export function detectPackage({ name, path, type, version }, pathOrFn, deps = []) {
75 if (!name.startsWith('@polkadot')) {
76 throw new Error(`Invalid package descriptor ${name}`);
77 }
78 const entry = getEntry(name);
79 entry.push({ path: getPath(path, pathOrFn), type, version });
80 // if we have more than one entry at DIFFERENT version types then warn. If there is more than one entry at the same
81 // version and ESM/CJS dual warnings are disabled, then do not display warnings
82 const entriesSameVersion = entry.every((e) => e.version === version);
83 const esmCjsWarningDisabled = xglobal.process?.env?.[POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG] === '1';
84 const multipleEntries = entry.length !== 1;
85 const disableWarnings = esmCjsWarningDisabled && entriesSameVersion;
86 if (multipleEntries && !disableWarnings) {
87 warn(`${name} has multiple versions, ensure that there is only one installed.`, entry, formatVersion);
88 }
89 else {
90 const mismatches = deps.filter((d) => d && d.version !== version);
91 if (mismatches.length) {
92 warn(`${name} requires direct dependencies exactly matching version ${version}.`, mismatches, formatInfo);
93 }
94 }
95}