1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const _findKey = require("lodash.findkey");
|
4 | const _get = require("lodash.get");
|
5 | const _invert = require("lodash.invert");
|
6 | const _isEmpty = require("lodash.isempty");
|
7 | const types_1 = require("../types");
|
8 | const _ = {
|
9 | get: _get,
|
10 | isEmpty: _isEmpty,
|
11 | invert: _invert,
|
12 | findKey: _findKey,
|
13 | };
|
14 | class ComposerParser {
|
15 | static getVersion(depObj) {
|
16 |
|
17 | const versionFound = _.get(depObj, 'version', '');
|
18 |
|
19 | const availableAliases = _.get(depObj, "extra['branch-alias']", []);
|
20 |
|
21 |
|
22 | return _.get(availableAliases, versionFound) ||
|
23 | _.get(_.invert(availableAliases), versionFound.replace('dev-', '')) &&
|
24 | versionFound.replace('dev-', '') ||
|
25 | versionFound ||
|
26 | _.findKey(_.invert(availableAliases), '0');
|
27 | }
|
28 | static buildDependencies(composerJsonObj, composerLockObj, depObj, systemPackages, includeDev = false, isDevTree = false, depRecursiveArray = [], packageRefCount = {}) {
|
29 | const result = {};
|
30 |
|
31 | const depName = _.get(depObj, 'name');
|
32 | const require = _.get(depObj, 'require', {});
|
33 | const requireDev = includeDev ? _.get(depObj, 'require-dev', {}) : {};
|
34 |
|
35 | const inRecursiveArray = depRecursiveArray.indexOf(depName) > -1;
|
36 | const exceedsMaxRepeats = packageRefCount[depName] >= this.MAX_PACKAGE_REPEATS;
|
37 | const hasNoDependencies = _.isEmpty(require) && _.isEmpty(requireDev);
|
38 |
|
39 | if (inRecursiveArray || exceedsMaxRepeats || hasNoDependencies) {
|
40 | return result;
|
41 | }
|
42 |
|
43 | depRecursiveArray.push(depName);
|
44 |
|
45 | const packages = _.get(composerLockObj, 'packages', []);
|
46 | const packagesDev = includeDev ? _.get(composerLockObj, 'packages-dev', []) : [];
|
47 | const allPackages = [
|
48 | ...packages,
|
49 | ...packagesDev,
|
50 | ];
|
51 |
|
52 | for (const name of Object.keys(require)) {
|
53 | let version = '';
|
54 |
|
55 | const lockedPackage = allPackages.find((dep) => dep.name === name);
|
56 | if (lockedPackage) {
|
57 | version = this.getVersion(lockedPackage);
|
58 | }
|
59 | else {
|
60 |
|
61 | version = _.get(systemPackages, name) || _.get(require, name);
|
62 | }
|
63 |
|
64 | version = version.replace(/^v(\d)/, '$1');
|
65 |
|
66 | packageRefCount[name] = (packageRefCount[name] || 0) + 1;
|
67 | result[name] = {
|
68 | name,
|
69 | version,
|
70 | dependencies: this.buildDependencies(composerJsonObj, composerLockObj, lockedPackage,
|
71 | systemPackages, includeDev, false, depRecursiveArray, packageRefCount),
|
72 | labels: {
|
73 | scope: isDevTree ? types_1.Scope.dev : types_1.Scope.prod,
|
74 | },
|
75 | };
|
76 | }
|
77 |
|
78 | for (const name of Object.keys(requireDev)) {
|
79 | let version = '';
|
80 |
|
81 | const lockedPackage = allPackages.find((dep) => dep.name === name);
|
82 | if (lockedPackage) {
|
83 | version = this.getVersion(lockedPackage);
|
84 | }
|
85 | else {
|
86 |
|
87 | version = _.get(systemPackages, name) || _.get(requireDev, name);
|
88 | }
|
89 |
|
90 | version = version.replace(/^v(\d)/, '$1');
|
91 |
|
92 | packageRefCount[name] = (packageRefCount[name] || 0) + 1;
|
93 | result[name] = {
|
94 | name,
|
95 | version,
|
96 | dependencies: this.buildDependencies(composerJsonObj, composerLockObj, lockedPackage,
|
97 | systemPackages, includeDev, true, depRecursiveArray, packageRefCount),
|
98 | labels: {
|
99 | scope: types_1.Scope.dev,
|
100 | },
|
101 | };
|
102 | }
|
103 |
|
104 | depRecursiveArray.pop();
|
105 |
|
106 | return result;
|
107 | }
|
108 | }
|
109 | exports.ComposerParser = ComposerParser;
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | ComposerParser.MAX_PACKAGE_REPEATS = 150;
|
116 |
|
\ | No newline at end of file |