-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
309 lines (277 loc) · 9.34 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
// Original based on https://github.com/shtylman/node-weaklink (with permission)
var fs = require('fs')
var path = require('path')
var markdown = require('markdown').markdown
var spdxLicenses = require('spdx-license-list/spdx-full')
var normalizeText = require('./normtext')
var licenseDir = path.join(__dirname, 'license-files')
var urltoLicense = require('./urltolicense')
// Alternate abbreviations used by package.json files.
var licenseAliases = {
'BSD': 'BSD-2-Clause',
'MIT/X11': 'MIT',
'apache version 2.0': 'Apache-2.0'
}
var licenses = []
// Match a license body or license id against known set of licenses.
function matchLicense (licenseString) {
// Find all textual matches on license text.
var normalized = normalizeText(licenseString)
var matchingLicenses = []
var license
// Check matches of normalized license content against signatures.
for (var i = 0; i < licenses.length; i++) {
license = licenses[i]
var match = false
for (var j = 0; j < license.signatures.length; j++) {
if (normalized.indexOf(license.signatures[j]) >= 0) {
match = true
break
}
}
if (match) {
matchingLicenses.push(license)
}
}
// For single-line license, check if it's a known license id.
if (matchingLicenses.length === 0 && !/[\n\f\r]/.test(licenseString) && licenseString.length < 100) {
var licenseName = normalizeText(licenseString)
// If there's an extra "license" on the end of the name, drop it ("MIT License" -> "MIT").
license = licenseIndex[licenseName] || licenseIndex[licenseName.replace(/ licen[sc]e$/, '')]
if (!license) {
license = {name: licenseName, id: null}
}
matchingLicenses.push(license)
}
if (matchingLicenses.length === 0) {
return null
}
if (matchingLicenses.length > 1) {
console.warn('Multiple matching licenses: ' + matchingLicenses.length, [licenseString, matchingLicenses])
}
return matchingLicenses[0]
}
// Attach "id" field to each license, and add a lookup for accidental variations among SPDX license ids.
var licenseIndex = {}
Object.keys(spdxLicenses).forEach(function (key) {
var license = spdxLicenses[key]
license.id = key
license.signatures = [normalizeText(license.license)]
if (license.url) {
license.url = license.url.replace(/[\n\r\f]+/, ', ')
}
licenses.push(license)
licenseIndex[key] = license
licenseIndex[normalizeText(key)] = license
})
Object.keys(licenseAliases).forEach(function (alias) {
licenseIndex[alias] = licenseIndex[licenseAliases[alias]]
licenseIndex[normalizeText(alias)] = licenseIndex[licenseAliases[alias]]
})
// Read source licenses from license-files directory
fs.readdirSync(licenseDir).forEach(function (name) {
// Add all variant signatures.
var id = name.split(',')[0]
if (licenseIndex[id]) {
licenseIndex[id].signatures.push(normalizeText(fs.readFileSync(path.join(licenseDir, name), 'utf8')))
} else {
console.warn('Unrecognized license for signature: ' + name)
}
})
// Convert package.json format to a known license.
// If a developer specifies just the name, we check it against SPDX, according to NPM guidelines.
// If they supply a URL, we just link to it, preserving name and exact link.
function getJsonLicense (json) {
var license = 'nomatch'
if (typeof json === 'string') {
license = matchLicense(json) || 'nomatch'
} else {
if (json.url) {
license = { name: json.type, url: json.url }
} else {
license = matchLicense(json.type)
}
}
return license
}
function getFileLicense (filename) {
var fileContents = fs.readFileSync(filename, 'utf8')
return matchLicense(fileContents) || 'nomatch'
}
function getReadmeLicense (filename) {
var readmeText = fs.readFileSync(filename, 'utf8')
if (/\.(md|markdown)$/i.test(filename)) {
return parseMarkdownLicense(readmeText)
}
if (/licen[cs]e/i.test(readmeText)) {
return matchLicense(readmeText)
}
return null
}
function parseMarkdownLicense (markdownText) {
var license = getMarkdownLicenseSection(markdownText)
return (
license
? (matchLicense(license) || 'nomatch')
: matchLicense(markdownText)
)
}
function getMarkdownLicenseSection (text) {
// Parse as markdown
var tree = markdown.parse(text)
for (var i = 0; i < tree.length; i++) {
var node = tree[i]
// Find section with "License" in the name
if (node[0] === 'header' && /licen[cs]e/i.test(node[2])) {
var section = []
// Group together all paragraph nodes immediately after the header
for (var j = i + 1; j < tree.length; j++) {
var childNode = tree[j]
if (childNode[0] === 'para') {
section.push(childNode[1])
} else {
break
}
}
// If we got a license, return it
if (section.length) {
return section.join('\n\n')
}
// Otherwise consider using the header contents itself (e.g. "MIT License")
if (/.+licen[cs]e/i.test(node[2])) {
return node[2]
}
}
// Check if paragraph has 'license' in it, and use it as-is
if (node[0] === 'para' && /.+licen[cs]e/i.test(node[1])) {
return node[1]
}
}
return null
}
function formatLicense (license) {
if (typeof license === 'string') {
return license
}
if (license.name && license.url) {
return license.name + ' (' + license.url + ')'
}
if (license.name && license.id) {
return license.name + ' (' + 'https://spdx.org/licenses/' + license.id + ')'
}
if (license.name) {
return license.name
}
if (license.url) {
var licensename = urltoLicense(license.url)
if (licensename) {
return licensename + ' (' + license.url + ')'
}
return 'unknown' + ' (' + license.url + ')'
}
return 'unknown license'
}
module.exports = function checkPath (packageName, basePath, overrides, includeDevDependencies, includeOptDependencies, seenList) {
seenList = seenList || {}
var seenKey = basePath + '|' + packageName
if (seenList[seenKey]) {
return
}
seenList[seenKey] = true
if (!fs.existsSync(basePath)) {
const parentNodeModules = path.join(path.resolve(basePath, '../../../node_modules'), packageName)
if (parentNodeModules !== basePath) {
return checkPath(packageName, parentNodeModules, overrides, includeDevDependencies, includeOptDependencies, seenList)
}
return null
}
var packageJsonPath = path.join(basePath, 'package.json')
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
if (overrides && overrides[packageName]) {
var override = overrides[packageName]
var licenseOverride = matchLicense(override.license)
return {
name: packageName,
version: packageJson.version,
license: formatLicense(licenseOverride),
licenseFile: override.url,
deps: []
}
}
var license = 'unknown'
var licenseFilePath
// Check package.json for "license" or "licenses" fields
if (packageJson.license || packageJson.licenses) {
licenseFilePath = packageJsonPath
var licenses = packageJson.licenses || []
if (!Array.isArray(licenses)) {
// Bad JSON, using "licenses" not as an array
licenses = [licenses]
}
if (Array.isArray(packageJson.license)) {
// Bad JSON, using "license" as an array
licenses = licenses.concat(packageJson.license)
} else if (packageJson.license) {
licenses.push(packageJson.license)
}
license = licenses.map(getJsonLicense).map(formatLicense).join(', ')
} else {
// Look for file with "license" or "copying" in its name
var files = fs.readdirSync(basePath)
files.some(function (name) {
if (/licen[sc]e/i.test(name) || /copying.*/i.test(name)) {
var file = path.join(basePath, name)
if (fs.statSync(file).isFile()) {
license = formatLicense(getFileLicense(file))
licenseFilePath = file
return true
}
}
return false
})
if (!licenseFilePath) {
// Look for a readme file that might have a license in it
files.some(function (name) {
if (/^readme/i.test(name)) {
var file = path.join(basePath, name)
if (fs.statSync(file).isFile()) {
var result = getReadmeLicense(file)
if (result) {
license = formatLicense(result)
licenseFilePath = file
return license !== 'nomatch'
}
}
}
return false
})
}
}
// array of deps
var dependencies = []
var pushDependency = function (dependencyLevel) {
return function (name) {
var res = checkPath(name, path.join(basePath, 'node_modules', name), overrides, includeDevDependencies, includeOptDependencies, seenList)
if (res) {
res.name = name
res.deps = res.deps || []
res.depLevel = dependencyLevel
dependencies.push(res)
}
}
}
Object.keys(packageJson.dependencies || {}).forEach(pushDependency(''))
if (includeDevDependencies || false) {
Object.keys(packageJson.devDependencies || {}).forEach(pushDependency('Dev'))
}
if (includeOptDependencies || false) {
Object.keys(packageJson.optionalDependencies || {}).forEach(pushDependency('Opt'))
}
return {
name: packageJson.name,
version: packageJson.version,
license: license,
licenseFile: licenseFilePath && path.relative(process.cwd(), licenseFilePath),
deps: dependencies.sort(function (dep1, dep2) { return dep1.name.localeCompare(dep2.name) })
}
}