UNPKG

projectz

Version:

Stop wasting time syncing and updating your project's README and Package Files!

95 lines (94 loc) 3.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLicenseSection = exports.getLicenseFile = void 0; // external const github_api_1 = require("@bevry/github-api"); const spdx_expression_parse_1 = __importDefault(require("spdx-expression-parse")); const full_js_1 = __importDefault(require("spdx-license-list/full.js")); const render_1 = require("@bevry/render"); function renderSpdxObject(spdxObject, output, depth = 0) { if (spdxObject.license) { const code = spdxObject.license; const details = full_js_1.default[code]; if (!details) { throw new Error(`Could not find the details for the license ${code}`); } const url = `http://spdx.org/licenses/${code}.html`; return output === 'description' ? depth === 0 ? (0, render_1.mul)([(0, render_1.ma)({ url, inner: details.name })]) : (0, render_1.ma)({ url, inner: details.name }) : details.licenseText // Remove useless copyright headers - (spdx-license-list@5.x) .replace('\nCopyright (c) <year> <copyright holders>\n', '') // Remove useless copyright headers - (spdx-license-list@6.x) .replace(/\s?Copyright.+holders>/gi, '') // Remove license introductions .replace(/^[\s\S]+<<endOptional>>\s*/m, '') // Render the license .replace(/^(.+?)\n\s*([\s\S]+)\s*$/, (match, license, body) => (0, render_1.lines)([(0, render_1.mh2)(license), (0, render_1.pre)(body)])); } else if (spdxObject.conjunction) { const left = renderSpdxObject(spdxObject.left, output, depth + 1); const middle = spdxObject.conjunction; const right = renderSpdxObject(spdxObject.right, output, depth + 1); return output === 'description' ? (0, render_1.mul)([left, middle, right]) : (0, render_1.lines)([left, right]); } else { throw new Error(`Unknown spdx object value: ${JSON.stringify(spdxObject, null, ' ')}`); } } function getRenderedLicenses(spdxString, output) { const sdpxObject = (0, spdx_expression_parse_1.default)(spdxString); return renderSpdxObject(sdpxObject, output); } function getLicenseIntroduction(data) { // Check if (!data.license) { throw new Error('License file was requested, but no license was specified'); } // Prepare const renderedBackers = (0, github_api_1.renderBackers)({ authors: data.authors }, { format: github_api_1.BackersRenderFormat.copyright }); const result = (0, render_1.lines)([ (0, render_1.mp)('Unless stated otherwise all works are:'), (0, render_1.mul)(renderedBackers.authors || []), (0, render_1.mp)('and licensed under:'), getRenderedLicenses(data.license, 'description'), ]); // Return return result; } function getLicenseFile(data) { // Check if (!data.license) { throw new Error('License file was requested, but no license was specified'); } // Prepare const result = (0, render_1.lines)([ (0, render_1.mh1)('License'), getLicenseIntroduction(data), getRenderedLicenses(data.license, 'body'), ]); // Return return result; } exports.getLicenseFile = getLicenseFile; function getLicenseSection(data) { // Check if (!data.license) { throw new Error('License file was requested, but no license was specified'); } // Prepare const result = (0, render_1.lines)([ (0, render_1.mh2)('License'), getLicenseIntroduction(data), ]); // Return return result; } exports.getLicenseSection = getLicenseSection;