UNPKG

2.91 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.detectPackage = detectPackage;
7
8var _xGlobal = require("@polkadot/x-global");
9
10var _function = require("./is/function");
11
12var _assert = require("./assert");
13
14// Copyright 2017-2022 @polkadot/util authors & contributors
15// SPDX-License-Identifier: Apache-2.0
16const DEDUPE = 'Either remove and explicitly install matching versions or dedupe using your package manager.\nThe following conflicting packages were found:';
17/** @internal */
18
19function 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/** @internal */
33
34
35function 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/** @internal */
45
46
47function formatInfo(version, _ref) {
48 let {
49 name
50 } = _ref;
51 return [version, name];
52}
53/** @internal */
54
55
56function 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/** @internal */
73
74
75function 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/** @internal */
89
90
91function warn(pre, all, fmt) {
92 console.warn(`${pre}\n${DEDUPE}\n${formatDisplay(all, fmt)}`);
93}
94/**
95 * @name detectPackage
96 * @summary Checks that a specific package is only imported once
97 * @description A `@polkadot/*` version detection utility, checking for one occurence of a package in addition to checking for ddependency versions.
98 */
99
100
101function 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