UNPKG

3.22 kBJavaScriptView Raw
1'use strict'
2var __importDefault =
3 (this && this.__importDefault) ||
4 function (mod) {
5 return mod && mod.__esModule ? mod : { default: mod }
6 }
7Object.defineProperty(exports, '__esModule', { value: true })
8exports.getLicenseSection = exports.getLicenseFile = void 0
9const util_js_1 = require('./util.js')
10const spdx_expression_parse_1 = __importDefault(
11 require('spdx-expression-parse')
12)
13const full_1 = __importDefault(require('spdx-license-list/full'))
14function renderSpdxObject(spdxObject, output, depth = 0) {
15 if (spdxObject.license) {
16 const code = spdxObject.license
17 const details = full_1.default[code]
18 if (!details) {
19 throw new Error(`Could not find the details for the license ${code}`)
20 }
21 const name = details.name
22 const body = details.licenseText
23 const url = `http://spdx.org/licenses/${code}.html`
24 return output === 'description'
25 ? depth === 0
26 ? `<ul><li><a href="${url}">${name}</a></li></ul>`
27 : `<a href="${url}">${name}</a>`
28 : body
29 // Remove useless copyright headers - (spdx-license-list@5.x)
30 .replace('\nCopyright (c) <year> <copyright holders>\n', '')
31 // Remove useless copyright headers - (spdx-license-list@6.x)
32 .replace(/\s?Copyright.+holders>/gi, '')
33 // Remove license introductions
34 .replace(/^[\s\S]+<<endOptional>>\s*/m, '')
35 // Convert the license into HTML
36 .replace(
37 /^(.+?)\n\s*([\s\S]+)\s*$/,
38 '<h2>$1</h2>\n\n<pre>\n$2\n</pre>'
39 )
40 } else if (spdxObject.conjunction) {
41 const left = renderSpdxObject(spdxObject.left, output, depth + 1)
42 const middle = spdxObject.conjunction
43 const right = renderSpdxObject(spdxObject.right, output, depth + 1)
44 return output === 'description'
45 ? `<ul><li>${left}</li>\n<li>${middle}</li>\n<li>${right}</li></ul>`
46 : `${left}\n\n${right}\n\n`.trim()
47 } else {
48 throw new Error(
49 `Unknown spdx object value: ${JSON.stringify(spdxObject, null, ' ')}`
50 )
51 }
52}
53function getLicensesHTML(spdxString, output) {
54 const sdpxObject = spdx_expression_parse_1.default(spdxString)
55 return renderSpdxObject(sdpxObject, output)
56}
57function getLicenseIntroduction(data) {
58 // Check
59 if (!data.license) {
60 throw new Error('License file was requested, but no license was specified')
61 }
62 // Prepare
63 const result = [
64 'Unless stated otherwise all works are:',
65 '',
66 util_js_1.getPeopleHTML(data.authors, {
67 displayCopyright: true,
68 displayYears: true,
69 }),
70 '',
71 'and licensed under:',
72 '',
73 getLicensesHTML(data.license, 'description'),
74 ].join('\n')
75 // Return
76 return result
77}
78function getLicenseFile(data) {
79 // Check
80 if (!data.license) {
81 throw new Error('License file was requested, but no license was specified')
82 }
83 // Prepare
84 const result = [
85 '<h1>License</h1>',
86 '',
87 getLicenseIntroduction(data),
88 '',
89 getLicensesHTML(data.license, 'body'),
90 ].join('\n')
91 // Return
92 return result
93}
94exports.getLicenseFile = getLicenseFile
95function getLicenseSection(data) {
96 // Check
97 if (!data.license) {
98 throw new Error('License file was requested, but no license was specified')
99 }
100 // Prepare
101 const result = ['<h2>License</h2>', '', getLicenseIntroduction(data)].join(
102 '\n'
103 )
104 // Return
105 return result
106}
107exports.getLicenseSection = getLicenseSection