-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.js
99 lines (86 loc) · 3.04 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
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import createDebugMessages from 'debug';
import { config } from './lib/config.js';
import { getFormatter } from './lib/getFormatter.js';
import { addLocalPackageData } from './lib/addLocalPackageData.js';
import { addPackageDataFromRepository } from './lib/addPackageDataFromRepository.js';
import { getDependencies } from './lib/getDependencies.js';
import { packageDataToReportData } from './lib/packageDataToReportData.js';
import { isNullOrUndefined, helpText, readJson } from './lib/util.js';
const debug = createDebugMessages('license-report');
(async () => {
if (config.help) {
console.log(helpText); // eslint-disable-line security-node/detect-crlf
return;
}
if (!config.package) {
config.package = './package.json';
}
if (path.extname(config.package) !== '.json') {
throw new Error('invalid package.json ' + config.package);
}
const outputFormatter = getFormatter(config.output);
try {
const resolvedPackageJson = path.resolve(process.cwd(), config.package);
debug('loading %s', resolvedPackageJson);
let packageJson;
if (fs.existsSync(resolvedPackageJson)) {
packageJson = await readJson(resolvedPackageJson);
} else {
throw new Error(
`Warning: the file '${resolvedPackageJson}' is required to get installed versions of packages`,
);
}
// Get a list of all the dependencies we want information about.
const inclusions = isNullOrUndefined(config.only)
? null
: config.only.split(',');
const exclusions = Array.isArray(config.exclude)
? config.exclude
: [config.exclude];
let exclusionRegexp;
if (
config.excludeRegex !== undefined &&
typeof config.excludeRegex === 'string' &&
config.excludeRegex !== ''
) {
try {
// TODO how to sanitize regex pattern provided by user?
// eslint-disable-next-line security/detect-non-literal-regexp
exclusionRegexp = new RegExp(config.excludeRegex, 'i');
} catch (error) {
console.error(error.message);
exclusionRegexp = undefined;
}
}
const fieldsList = Array.isArray(config.fields)
? config.fields
: [config.fields];
let depsIndex = getDependencies(
packageJson,
exclusions,
inclusions,
exclusionRegexp,
);
const projectRootPath = path.dirname(resolvedPackageJson);
const packagesData = await Promise.all(
depsIndex.map(async (element) => {
const localDataForPackage = await addLocalPackageData(
element,
projectRootPath,
fieldsList,
);
const completeDataForPackage =
await addPackageDataFromRepository(localDataForPackage);
return packageDataToReportData(completeDataForPackage, config);
}),
);
// eslint-disable-next-line security-node/detect-crlf
console.log(outputFormatter(packagesData, config));
} catch (e) {
console.error(e.stack);
process.exit(1); // eslint-disable-line n/no-process-exit
}
})();