1 | 'use strict'
|
2 | var __importDefault =
|
3 | (this && this.__importDefault) ||
|
4 | function (mod) {
|
5 | return mod && mod.__esModule ? mod : { default: mod }
|
6 | }
|
7 | Object.defineProperty(exports, '__esModule', { value: true })
|
8 | exports.getLicenseSection = exports.getLicenseFile = void 0
|
9 | const util_js_1 = require('./util.js')
|
10 | const spdx_expression_parse_1 = __importDefault(
|
11 | require('spdx-expression-parse')
|
12 | )
|
13 | const full_1 = __importDefault(require('spdx-license-list/full'))
|
14 | function 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 |
|
30 | .replace('\nCopyright (c) <year> <copyright holders>\n', '')
|
31 |
|
32 | .replace(/\s?Copyright.+holders>/gi, '')
|
33 |
|
34 | .replace(/^[\s\S]+<<endOptional>>\s*/m, '')
|
35 |
|
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 | }
|
53 | function getLicensesHTML(spdxString, output) {
|
54 | const sdpxObject = spdx_expression_parse_1.default(spdxString)
|
55 | return renderSpdxObject(sdpxObject, output)
|
56 | }
|
57 | function getLicenseIntroduction(data) {
|
58 |
|
59 | if (!data.license) {
|
60 | throw new Error('License file was requested, but no license was specified')
|
61 | }
|
62 |
|
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 |
|
76 | return result
|
77 | }
|
78 | function getLicenseFile(data) {
|
79 |
|
80 | if (!data.license) {
|
81 | throw new Error('License file was requested, but no license was specified')
|
82 | }
|
83 |
|
84 | const result = [
|
85 | '<h1>License</h1>',
|
86 | '',
|
87 | getLicenseIntroduction(data),
|
88 | '',
|
89 | getLicensesHTML(data.license, 'body'),
|
90 | ].join('\n')
|
91 |
|
92 | return result
|
93 | }
|
94 | exports.getLicenseFile = getLicenseFile
|
95 | function getLicenseSection(data) {
|
96 |
|
97 | if (!data.license) {
|
98 | throw new Error('License file was requested, but no license was specified')
|
99 | }
|
100 |
|
101 | const result = ['<h2>License</h2>', '', getLicenseIntroduction(data)].join(
|
102 | '\n'
|
103 | )
|
104 |
|
105 | return result
|
106 | }
|
107 | exports.getLicenseSection = getLicenseSection
|