1 | import { xglobal } from '@polkadot/x-global';
|
2 | import { isFunction } from './is/function.js';
|
3 | const DEDUPE = 'Either remove and explicitly install matching versions or dedupe using your package manager.\nThe following conflicting packages were found:';
|
4 | export const POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG = 'POLKADOTJS_DISABLE_ESM_CJS_WARNING';
|
5 |
|
6 | function 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 |
|
17 | function 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 |
|
27 | function formatInfo(version, { name }) {
|
28 | return [
|
29 | version,
|
30 | name
|
31 | ];
|
32 | }
|
33 |
|
34 | function 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 |
|
51 | function 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 |
|
66 | function warn(pre, all, fmt) {
|
67 | console.warn(`${pre}\n${DEDUPE}\n${formatDisplay(all, fmt)}`);
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | export 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 |
|
81 |
|
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 | }
|