-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
78 lines (62 loc) · 2.16 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
'use strict'
const path = require('path')
function LicensesPlugin(conf) {
this.options = Object.assign({
acceptable: /Apache|BSD|MIT|ISC/,
unacceptable: /unlicensed/i,
selected: {},
title: 'Licenses',
filename: 'Licenses.txt',
format: 'text',
failWithError: true
}, conf)
}
function selectLicenseForPackage(info, options) {
let license
if (options.selected[info.name]) {
license = options.selected[info.name]
} else if (info.license && info.license.match(options.acceptable)) {
license = info.license
} else if (info.licenses) {
const l = info.licenses.find(t => t.type.match(options.acceptable))
license = l ? l.type : null
}
if (options.failWithError) {
if (!license ) {
throw new Error(`Missing or unrecognized license for ${info.name}\n`)
} else if (options.unacceptable && license.match(options.unacceptable)) {
throw new Error(`Forbiddent license [${license}] for ${info.name}\n`)
}
}
return license || 'UNKNOWN'
}
function generateTextFile(title, licenses, format) {
let header
let list
if (format === 'markdown') {
header = `# ${title}`
list = licenses.map(l => `[${l.name}](${l.link}) licensed under ${l.license}`)
} else {
header = title
list = licenses.map(l => `${l.name} licensed under ${l.license} (${l.link})`)
}
return [header].concat(list).join('\n\n')
}
LicensesPlugin.prototype.apply = function apply(compiler) {
const vendors = require(path.join(compiler.context, 'package.json')).dependencies
const licenses = Object.keys(vendors).map(name => {
const info = require(path.join(compiler.context, 'node_modules', name, 'package.json'))
const license = selectLicenseForPackage(info, this.options)
const link = info.homepage || (info.repository && info.repository.url) || `https://www.npmjs.com/package/${v}`
return { name, license, link }
})
compiler.plugin('emit', (compilation, cb) => {
const result = generateTextFile(this.options.title, licenses, this.options.format)
compilation.assets[this.options.filename] = {
source: () => result,
size: () => result.length
}
cb()
})
}
module.exports = LicensesPlugin