-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·88 lines (72 loc) · 2.97 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
#!/usr/bin/env node
var fs = require('fs');
var log = require('npmlog');
var program = require('commander');
var output = require('./lib/output');
var licensing = require('./lib/licensing');
var legally = require('legally/src/legally');
var obligationInfo = require('./metadata/obligationInfo');
var licenseObligations = require('./metadata/licenseObligations');
// Define arguments and options
program
.version('1.0.0')
.option('--folder <path>', 'set path to project root with node_modules/ directory')
.option('--closed-source', 'whether the project is being distributed as closed-source (for example as a binary or client-side with webpack)')
.parse(process.argv);
// Main project directory
var projectDirectory = program.folder || process.cwd();
// Is the node_modules directory missing?
if (!fs.existsSync(projectDirectory + '/node_modules') && !projectDirectory.includes('node_modules')) {
return log.error('tldrlegal', 'Please run this tool from within a JavaScript project with a node_modules directory.');
}
// Fetch dependencies and their licenses by directory
legally(projectDirectory).then(function (packages) {
// Result variables
var results = {}, unknownLicenses = [];
// Traverse all dependencies
for (var packageName in packages) {
// Get SPDX license code
var license = licensing.getPreferredPackageLicense(packages[packageName], program.closedSource);
// Get obligations for this license
var obligations = licenseObligations[license];
// No obligations documented for this license?
if (!obligations) {
// Add to list of unknown licenses
unknownLicenses.push([packageName, license]);
// Nothing else to do here
continue;
}
// Traverse obligations for this license
for (var obligation in obligations) {
// Is this an irrelevant obligation?
if (!licensing.isObligationRelevant(obligation, program.closedSource)) {
continue;
}
// Prepare an array of packages for this obligation
if (!results[obligation]) {
results[obligation] = [];
}
// Add current package and its license under this obligation
results[obligation].push({ name: packageName, license: license });
}
}
// Traverse possible license obligations
for (var obligation in obligationInfo) {
// Is this an irrelevant obligation?
if (!licensing.isObligationRelevant(obligation, program.closedSource)) {
continue;
}
// Already have results for this obligation?
if (results[obligation]) {
continue;
}
// Initialize obligation array for summary view
results[obligation] = [];
}
// Output everything
output(results, unknownLicenses, packages);
})
.catch(function(err) {
// Output error
log.error('tldrlegal', 'Running legally failed: ' + err);
});