-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicense.js
35 lines (33 loc) · 1.29 KB
/
license.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
'use strict'
const path = require('path')
const spdxSet = require('spdx-license-list/simple')
const spdx = require('spdx-license-list/full')
const normalizeData = require('normalize-package-data')
module.exports = function (sections, pluginOptions = {}) {
let long = Boolean(pluginOptions.long)
let pkg = require(path.join(process.cwd(), 'package.json'))
normalizeData(pkg)
if (!spdxSet.has(pkg.license)) {
throw new Error(`'${pkg.license}' is not a valid value for the 'license' field in package.json. Please set it to an official SPDX value.`)
}
let year = (new Date()).getFullYear()
let licenseName = spdx[pkg.license].name
let licenseUrl = spdx[pkg.license].url
// I put this ugly logic in here simply because I like The Unlicense a lot.
let hasThe = licenseName.toLowerCase().startsWith('the')
let licenseText = long ? spdx[pkg.license].licenseText : `Licensed under ${hasThe ? '' : 'the '}[${licenseName}](${licenseUrl}).`
let licenseSectionText = `## License
Copyright ${year} ${pkg.author.name}.
${licenseText}
`
// Update existing license section
for (let i in sections) {
if (sections[i].match(/^#{1,2}\s*License/i)) {
sections[i] = licenseSectionText
return sections
}
}
// or append one to the end
sections.push(licenseSectionText)
return sections
}