-
Notifications
You must be signed in to change notification settings - Fork 18
/
cli.js
191 lines (172 loc) · 4.74 KB
/
cli.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Based on code shamelessly lifted from https://github.com/shtylman/node-weaklink (with permission)
require('colors')
var treeify = require('treeify')
var fs = require('fs')
var stripJsonComments = require('strip-json-comments')
var licensecheck = require('./index.js')
var path = '.'
var overridesPath = './licenses.json'
var missingOnly = false
var flat = false
var format = 'color'
var highlight = null
var includeDevDependencies = false
var includeOptDependencies = false
var once = false
var seen = {}
var hidelist = [] // list of licenses to omit from output
function isOmittedLicense (license) {
for (var i = 0; i < hidelist.length; i++) {
if (license.split(' ')[0] === hidelist[i]) {
return true
}
}
return false
}
for (var i = 2; i < process.argv.length; i++) {
var arg = process.argv[i]
switch (arg) {
case '--dev':
includeDevDependencies = true
break
case '--opt':
includeOptDependencies = true
break
case '-m':
case '--missing-only':
missingOnly = true
break
case '-h':
case '--highlight':
highlight = new RegExp(process.argv[++i], 'i')
break
case '-f':
case '--flat':
flat = true
break
case '--tsv':
format = 'tsv'
flat = true
break
case '-o':
case '--once':
once = true
break
case '--hide':
i++
hidelist = (process.argv[i] || '').split(',')
break
default:
path = arg
break
}
}
var overrides = fs.existsSync(overridesPath) &&
JSON.parse(stripJsonComments(fs.readFileSync(overridesPath, 'utf8'))) || {}
var licenses = licensecheck('.', path, overrides, includeDevDependencies, includeOptDependencies)
if (flat) {
var dependencies = makeFlatDependencyMap(licenses)
Object.keys(dependencies).sort().forEach(function (key) {
console.log(dependencies[key])
})
} else {
treeify.asLines(makeDependencyTree(licenses), false, console.log)
}
function isMissing (info) {
return !info.licenseFile || info.license === 'nomatch'
}
function simplifyFile (file) {
return file.replace(/\/node_modules\//g, ' ~ ')
}
function getDescription (info) {
var key
var sep
var file
if (format === 'color') {
sep = ' ── '
key = info.name + (' (' + info.version + ')').grey
if (info.depLevel) {
key = key + (' [' + info.depLevel + ']').bold.yellow
}
key = key + sep
file = info.licenseFile
if (file) {
file = simplifyFile(file)
if (info.license.startsWith('unknown')) {
key += info.license.yellow
} else if (info.license === 'nomatch') {
key += ('unmatched license file: ' + file).yellow
} else if (highlight && highlight.test(info.license)) {
key += info.license.magenta + sep + file.grey
} else {
key += info.license.green + sep + file.grey
}
} else {
key += 'no license found'.red
}
} else if (format === 'tsv') {
sep = '\t'
key = info.name + ' (' + info.version + ')' + sep
file = info.licenseFile
if (file) {
if (info.license.startsWith('unknown')) {
key += info.license.yellow
} else if (info.license === 'nomatch') {
key += 'unmatched: ' + file + sep
} else {
key += info.license + sep + file
}
} else {
key += 'MISSING' + sep
}
} else {
throw Error('invalid format: ' + format)
}
return key
}
function makeDependencyTree (info) {
if (missingOnly && !isMissing(info) && !info.deps.some(isMissing)) {
return {}
}
var key = getDescription(info)
var tree = {}
var dependencies = tree[key] = {}
info.deps.map(makeDependencyTree).forEach(function (dependency) {
Object.keys(dependency).forEach(function (key) {
dependencies[key] = dependency[key]
})
})
// Check if the license of the package is to be hidden
if (isOmittedLicense(info.license)) {
// Omit the package if it has no dependancies with non hidden license
if (Object.keys(tree[key]).length === 0) {
return {}
}
// Otherwise include only the name and version of the package with its dependancies
dependencies = tree[key]
key = info.name + (' (' + info.version + ')').grey
tree = {}
tree[key] = dependencies
return tree
}
return tree
}
function makeFlatDependencyMap (info) {
var map = {}
if ((!missingOnly || isMissing(info)) && (!isOmittedLicense(info.license))) {
map[info.name + '@' + info.version] = getDescription(info)
}
if (once && seen[info.name + '@' + info.version]) {
return {}
}
seen[info.name + '@' + info.version] = true
info.deps.forEach(function (dep) {
var subMap = makeFlatDependencyMap(dep)
Object.keys(subMap).forEach(function (key) {
if (!map[key]) {
map[key] = subMap[key]
}
})
})
return map
}