-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
386 lines (356 loc) · 10.6 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
module.exports = licensee
var blueOakList = require('@blueoak/list')
var correctLicenseMetadata = require('correct-license-metadata')
var has = require('has')
var npmLicenseCorrections = require('npm-license-corrections')
var osi = require('spdx-osi')
var parse = require('spdx-expression-parse')
var parseJSON = require('json-parse-errback')
var readPackageTree = require('read-package-tree')
var runParallel = require('run-parallel')
var satisfies = require('semver').satisfies
var simpleConcat = require('simple-concat')
var spawn = require('child_process').spawn
var spdxWhitelisted = require('spdx-whitelisted')
function licensee (configuration, path, callback) {
if (!validConfiguration(configuration)) {
return callback(new Error('Invalid configuration'))
}
configuration.licenses = compileLicenseWhitelist(configuration)
configuration.licensesParsed = (configuration.licenses || [])
.reduce(function (whitelist, element) {
try {
var parsed = parse(element)
if (has(parsed, 'conjunction')) {
throw new Error('Cannot match against "' + JSON.stringify(element) + '".')
}
return whitelist.concat(parsed)
} catch (e) {
return whitelist
}
}, [])
if (
configuration.licenses.length === 0 &&
(!configuration.packages || Object.keys(configuration.packages).length === 0)
) {
callback(new Error('No licenses or packages whitelisted.'))
} else {
if (configuration.productionOnly) {
// In order to ignore devDependencies, we need to read:
//
// 1. the dependencies-only dependency graph, from
// `npm ls --json --production`
//
// 2. the structure of `node_modules` and `package.json`
// files within it, with read-package-tree.
//
// `npm ls` calls read-package-tree internally, but does
// lots of npm-specific post-processing to produce the
// dependency tree. Calling read-package-tree twice, at
// the same time, is far from efficient. But it works,
// and doing so helps keep this package small.
runParallel({
dependencies: readDependencyList,
packages: readFilesystemTree
}, function (error, trees) {
if (error) callback(error)
else withTrees(trees.packages, trees.dependencies)
})
} else {
// If we are analyzing _all_ installed dependencies,
// and don't care whether they're devDependencies
// or not, just read `node_modules`. We don't need
// the dependency graph.
readFilesystemTree(function (error, packages) {
if (error) callback(error)
else {
if (configuration.filterPackages) {
packages = configuration.filterPackages(packages)
}
withTrees(packages, false)
}
})
}
}
function withTrees (packages, dependencies) {
callback(null, findIssues(
configuration, packages, dependencies, []
))
}
function readDependencyList (done) {
var executable = process.platform === 'win32' ? 'npm.cmd' : 'npm'
var child = spawn(
executable, ['ls', '--production', '--json'], { cwd: path }
)
var outputError
var json
simpleConcat(child.stdout, function (error, buffer) {
if (error) outputError = error
else json = buffer
})
child.once('error', function (error) {
outputError = error
})
child.once('close', function (code) {
if (outputError) {
done(outputError)
} else {
if (code !== 0) console.error('Warning: npm exited with status ' + code)
parseJSON(json, function (error, graph) {
if (error) return done(error)
if (!has(graph, 'dependencies')) {
done(new Error('cannot interpret npm ls --json output'))
} else {
var flattened = {}
flattenDependencyTree(graph.dependencies, flattened)
done(null, flattened)
}
})
}
})
}
function readFilesystemTree (done) {
readPackageTree(path, function (error, tree) {
if (error) return done(error)
done(null, tree.children)
})
}
}
var KEY_PREFIX = '.'
function flattenDependencyTree (graph, object) {
Object.keys(graph).forEach(function (name) {
var node = graph[name]
var version = node.version
var key = KEY_PREFIX + name
if (
has(object, key) &&
object[key].indexOf(version) === -1
) {
object[key].push(version)
} else {
object[key] = [version]
}
if (has(node, 'dependencies')) {
flattenDependencyTree(node.dependencies, object)
}
})
}
function validConfiguration (configuration) {
return (
isObject(configuration) &&
has(configuration, 'licenses') &&
isObject(configuration.licenses) &&
has(configuration, 'packages')
? (
// Validate `packages` property.
isObject(configuration.packages) &&
Object.keys(configuration.packages)
.every(function (key) {
return isString(configuration.packages[key])
})
) : true
)
}
function isObject (argument) {
return argument && typeof argument === 'object'
}
function isString (argument) {
return typeof argument === 'string'
}
function findIssues (configuration, children, dependencies, results) {
if (Array.isArray(children)) {
children.forEach(function (child) {
if (
!configuration.productionOnly ||
appearsIn(child, dependencies)
) {
var result = resultForPackage(configuration, child)
// Deduplicate.
var existing = results.find(function (existing) {
return (
existing.name === result.name &&
existing.version === result.version
)
})
if (existing) {
if (existing.duplicates) {
existing.duplicates.push(result)
} else {
existing.duplicates = [result]
}
} else {
results.push(result)
}
findIssues(configuration, child, dependencies, results)
}
if (child.children) {
findIssues(configuration, child.children, dependencies, results)
}
})
return results
} else return results
}
function appearsIn (installed, dependencies) {
var name = installed.package.name
var key = KEY_PREFIX + name
var version = installed.package.version
return (
has(dependencies, key) &&
dependencies[key].indexOf(version) !== -1
)
}
function resultForPackage (configuration, tree) {
var packageWhitelist = configuration.packages || {}
var result = {
name: tree.package.name,
license: tree.package.license,
author: tree.package.author,
contributors: tree.package.contributors,
repository: tree.package.repository,
homepage: tree.package.homepage,
version: tree.package.version,
parent: tree.parent,
path: tree.path
}
// Find and apply any manual license metadata correction.
var manualCorrection = (
configuration.corrections &&
npmLicenseCorrections.find(function (correction) {
return (
correction.name === result.name &&
correction.version === result.version
)
})
)
if (manualCorrection) {
result.license = manualCorrection.license
result.corrected = 'manual'
}
// Find and apply any automatic license metadata correction.
var automaticCorrection = (
configuration.corrections &&
correctLicenseMetadata(tree.package)
)
if (automaticCorrection) {
result.license = automaticCorrection
result.corrected = 'automatic'
}
// Check if ignored.
var ignore = configuration.ignore
if (ignore && Array.isArray(ignore)) {
var ignored = ignore.some(function (ignore) {
if (typeof ignore !== 'object') return false
if (
ignore.prefix &&
typeof ignore.prefix === 'string' &&
startsWith(result.name, ignore.prefix)
) return true
if (
ignore.scope &&
typeof ignore.scope === 'string' &&
startsWith(result.name, '@' + ignore.scope + '/')
) return true
if (
ignore.author &&
typeof ignore.author === 'string' &&
personMatches(result.author, ignore.author)
) return true
return false
})
if (ignored) {
result.approved = true
result.ignored = ignored
return result
}
}
result.approved = false
var packageWhitelisted = Object.keys(packageWhitelist)
.some(function (name) {
return (
result.name === name &&
satisfies(result.version, packageWhitelist[name]) === true
)
})
if (packageWhitelisted) {
result.approved = true
result.package = true
return result
}
if (!result.license || typeof result.license !== 'string') {
return result
}
var validSPDX = true
var parsed
try {
parsed = parse(result.license)
} catch (e) {
validSPDX = false
}
var licenseWhitelist = configuration.licensesParsed
// Check against licensing rule.
var licenseWhitelisted = (
validSPDX &&
spdxWhitelisted(parsed, licenseWhitelist)
)
if (licenseWhitelisted) {
result.approved = true
}
return result
}
function startsWith (string, prefix) {
return string.toLowerCase().indexOf(prefix.toLowerCase()) === 0
}
function personMatches (person, string) {
if (!person) return false
if (typeof person === 'string') {
return contains(person, string)
}
if (typeof person === 'object') {
if (matches('name')) return true
if (matches('email')) return true
if (matches('url')) return true
}
return false
function matches (key) {
return (
person[key] &&
typeof person[key] === 'string' &&
contains(person[key], string)
)
}
}
function contains (string, substring) {
return string.toLowerCase().indexOf(substring.toLowerCase()) !== -1
}
function licensesFromBlueOak (rating) {
rating = rating.toLowerCase()
var ids = []
for (var index = 0; index < blueOakList.length; index++) {
var element = blueOakList[index]
element.licenses.forEach(function (license) {
try {
parse(license.id)
ids.push(license.id)
} catch (e) {
// pass
}
})
if (rating === element.name.toLowerCase()) break
}
return ids
}
function compileLicenseWhitelist (configuration) {
var licenses = configuration.licenses
var whitelist = []
var spdx = licenses.spdx
if (spdx) pushMissing(spdx, whitelist)
var blueOak = licenses.blueOak
if (blueOak) pushMissing(licensesFromBlueOak(blueOak), whitelist)
if (licenses.osi) pushMissing(osi, whitelist)
return whitelist
}
function pushMissing (source, sink) {
source.forEach(function (element) {
if (sink.indexOf(element) === -1) sink.push(element)
})
}