forked from marcello3d/node-licensecheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (146 loc) · 5.13 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
// 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 licenseDir = __dirname+"/license-files/"
var licenses = []
function normalizeText(text) {
return text.replace(/[^a-z0-9\s]/ig,'').toLowerCase().split(/[\s\n]+/).join(' ')
}
function matchLicense(licenseString) {
var normalized = normalizeText(licenseString)
for (var i=0; i<licenses.length; i++) {
var license = licenses[i]
if (normalized.indexOf(license.contents) >= 0) {
return license.name
}
}
if (!/[\n\f\r]/.test(licenseString) && licenseString.length < 100) {
return licenseString.trim()
}
return null
}
// Read source licenses from license-files directory
fs.readdirSync(licenseDir).forEach(function(name) {
licenses.push({
name:name,
contents:normalizeText(fs.readFileSync(licenseDir + name, "utf8"))
})
})
function getLicenseType(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
}
module.exports = function checkPath(basePath) {
if (!fs.existsSync(basePath)) return null
var packageJsonPath = path.join(basePath, 'package.json')
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
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 (packageJson.license) licenses.push(packageJson.license)
license = licenses.map(function(license) {
return typeof license == 'string' ? license : (license.type + " (" + license.url + ")")
}).join(', ')
} else {
// Look for file with "license" in its name
var files = fs.readdirSync(basePath)
files.some(function(name) {
if (/license/i.test(name)) {
var file = path.join(basePath, name)
if (fs.statSync(file).isFile()) {
license = getLicenseType(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 = result
licenseFilePath = file
return license !== 'nomatch'
}
}
}
return false
})
}
}
// array of deps
var dependencies = []
Object.keys(packageJson.dependencies || {}).sort().forEach(function(name) {
var res = checkPath(path.join(basePath, 'node_modules', name))
if (res) {
res.name = name
res.deps = res.deps || []
dependencies.push(res)
}
})
return {
name: packageJson.name,
version: packageJson.version,
license: license,
licenseFile: licenseFilePath,
deps: dependencies
}
}