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