-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
138 lines (121 loc) · 4.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import createDebugMessages from 'debug';
import { addLocalPackageData } from 'license-report/lib/addLocalPackageData.js';
import { addPackageDataFromRepository } from 'license-report/lib/addPackageDataFromRepository.js';
import { packageDataToReportData } from 'license-report/lib/packageDataToReportData.js';
import getDependencies from './lib/getDependencies.js';
import addDependenciesRecursive from './lib/addDependenciesRecursive.js';
import config from './lib/config.js';
import getFormatter from './lib/getFormatter.js';
import listToTree from './lib/listToTree.js';
import util from './lib/util.js';
const debug = createDebugMessages('license-report-recurse');
(async () => {
if (config.help) {
// eslint-disable-next-line security-node/detect-crlf
console.log(util.helpText);
return;
}
if (!config.package) {
config.package = './package.json';
}
if (path.extname(config.package) !== '.json') {
throw new Error('invalid package.json ' + config.package);
}
if (config.output === 'tree' && !config.recurse) {
throw new Error('output=tree requires --recurse option');
}
const outputFormatter = getFormatter(config.output);
try {
const resolvedPackageJson = path.resolve(process.cwd(), config.package);
const projectRootPath = path.dirname(resolvedPackageJson);
debug('loading %s', resolvedPackageJson);
let packageJson;
if (fs.existsSync(resolvedPackageJson)) {
packageJson = await util.readJson(resolvedPackageJson);
} else {
throw new Error(
`Warning: the file '${resolvedPackageJson}' is required to get installed versions of packages`,
);
}
const inclusions = util.isNullOrUndefined(config.only)
? null
: config.only.split(',');
const exclusions = Array.isArray(config.exclude)
? config.exclude
: [config.exclude];
const parentPath = `>${packageJson.name}`;
// an array with all the dependencies in the package.json under inspection
let depsIndexBase = getDependencies(
packageJson,
exclusions,
inclusions,
parentPath,
);
const depsIndex = await Promise.all(
depsIndexBase.map(async (element) => {
const alias = element.alias;
const localDataForPackages = await addLocalPackageData(
element,
projectRootPath,
config.fields,
);
const packagesData =
await addPackageDataFromRepository(localDataForPackages);
const basicFields = {
alias: alias, // to get the local path of the package
isRootNode: true, // to identify the root nodes when generating the tree view
};
return Object.assign(packagesData, basicFields);
}),
);
// add dependencies of dependencies
if (config.recurse === true || config.recurse === 'true') {
let inclusionsSubDeps = ['prod', 'opt', 'peer'];
if (inclusions !== null) {
inclusionsSubDeps = inclusions.filter((entry) => entry !== 'dev');
}
await addDependenciesRecursive(
depsIndex,
projectRootPath,
exclusions,
inclusionsSubDeps,
parentPath,
config.fields,
);
}
const sortedList = depsIndex.sort(util.alphaSort);
// remove duplicates as they are only needed to identify dependency loops
let lastPackage = '';
const dedupedSortedList = sortedList.filter((element) => {
const currentPackage = `${element.name}@${element.installedVersion}`;
if (currentPackage !== lastPackage || element.isRootNode) {
lastPackage = currentPackage;
return true;
}
return false;
});
if (config.output !== 'tree') {
// keep only fields that are defined in the configuration
const packagesList = await Promise.all(
dedupedSortedList.map(async (element) => {
return packageDataToReportData(element, config);
}),
);
// eslint-disable-next-line security-node/detect-crlf
console.log(outputFormatter(packagesList, config));
debug(`emitted list with ${packagesList.length} entries`);
} else {
const packagesTree = await listToTree(dedupedSortedList, config);
// eslint-disable-next-line security-node/detect-crlf
console.log(outputFormatter(packagesTree, config));
debug(`emitted tree with ${packagesTree.length} base nodes`);
}
} catch (e) {
console.error(e.stack);
// eslint-disable-next-line n/no-process-exit
process.exit(1);
}
})();