1 |
|
2 |
|
3 | import { xglobal } from '@polkadot/x-global';
|
4 | import { isFunction } from "./is/function.js";
|
5 | import { assert } from "./assert.js";
|
6 | const DEDUPE = 'Either remove and explicitly install matching versions or dedupe using your package manager.\nThe following conflicting packages were found:';
|
7 |
|
8 |
|
9 | function getEntry(name) {
|
10 | const _global = xglobal;
|
11 |
|
12 | if (!_global.__polkadotjs) {
|
13 | _global.__polkadotjs = {};
|
14 | }
|
15 |
|
16 | if (!_global.__polkadotjs[name]) {
|
17 | _global.__polkadotjs[name] = [];
|
18 | }
|
19 |
|
20 | return _global.__polkadotjs[name];
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 | function formatDisplay(all, fmt) {
|
26 | let max = 0;
|
27 |
|
28 | for (let i = 0; i < all.length; i++) {
|
29 | max = Math.max(max, all[i].version.length);
|
30 | }
|
31 |
|
32 | return all.map(d => `\t${fmt(d.version.padEnd(max), d).join('\t')}`).join('\n');
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 | function formatInfo(version, {
|
38 | name
|
39 | }) {
|
40 | return [version, name];
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 | function formatVersion(version, {
|
46 | path,
|
47 | type
|
48 | }) {
|
49 | let extracted;
|
50 |
|
51 | if (path && path.length >= 5) {
|
52 | const nmIndex = path.indexOf('node_modules');
|
53 | extracted = nmIndex === -1 ? path : path.substring(nmIndex);
|
54 | } else {
|
55 | extracted = '<unknown>';
|
56 | }
|
57 |
|
58 | return [`${`${type || ''}`.padStart(3)} ${version}`, extracted];
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 | function getPath(infoPath, pathOrFn) {
|
64 | if (infoPath) {
|
65 | return infoPath;
|
66 | } else if (isFunction(pathOrFn)) {
|
67 | try {
|
68 | return pathOrFn() || '';
|
69 | } catch (error) {
|
70 | return '';
|
71 | }
|
72 | }
|
73 |
|
74 | return pathOrFn || '';
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | function warn(pre, all, fmt) {
|
80 | console.warn(`${pre}\n${DEDUPE}\n${formatDisplay(all, fmt)}`);
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | export function detectPackage({
|
90 | name,
|
91 | path,
|
92 | type,
|
93 | version
|
94 | }, pathOrFn, deps = []) {
|
95 | assert(name.startsWith('@polkadot'), () => `Invalid package descriptor ${name}`);
|
96 | const entry = getEntry(name);
|
97 | entry.push({
|
98 | path: getPath(path, pathOrFn),
|
99 | type,
|
100 | version
|
101 | });
|
102 |
|
103 | if (entry.length !== 1) {
|
104 | warn(`${name} has multiple versions, ensure that there is only one installed.`, entry, formatVersion);
|
105 | } else {
|
106 | const mismatches = deps.filter(d => d && d.version !== version);
|
107 |
|
108 | if (mismatches.length) {
|
109 | warn(`${name} requires direct dependencies exactly matching version ${version}.`, mismatches, formatInfo);
|
110 | }
|
111 | }
|
112 | } |
\ | No newline at end of file |