UNPKG

@cyclonedx/cyclonedx-library

Version:

Core functionality of CycloneDX for JavaScript (Node.js or WebBrowser).

110 lines (106 loc) 4.09 kB
"use strict"; /*! This file is part of CycloneDX JavaScript Library. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SPDX-License-Identifier: Apache-2.0 Copyright (c) OWASP Foundation. All Rights Reserved. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentBuilder = exports.ToolBuilder = void 0; const packageJson_1 = require("../_helpers/packageJson"); const componentType_1 = require("../enums/componentType"); const component_1 = require("../models/component"); const externalReference_1 = require("../models/externalReference"); const license_1 = require("../models/license"); const tool_1 = require("../models/tool"); class ToolBuilder { #extRefFactory; constructor(extRefFactory) { this.#extRefFactory = extRefFactory; } get extRefFactory() { return this.#extRefFactory; } makeTool(data) { const [name, vendor] = typeof data.name === 'string' ? (0, packageJson_1.splitNameGroup)(data.name) : []; return new tool_1.Tool({ vendor, name, version: (typeof data.version === 'string') ? data.version : undefined, externalReferences: new externalReference_1.ExternalReferenceRepository(this.#extRefFactory.makeExternalReferences(data)) }); } } exports.ToolBuilder = ToolBuilder; class ComponentBuilder { #extRefFactory; #licenseFactory; constructor(extRefFactory, licenseFactory) { this.#extRefFactory = extRefFactory; this.#licenseFactory = licenseFactory; } get extRefFactory() { return this.#extRefFactory; } get licenseFactory() { return this.#licenseFactory; } makeComponent(data, type = componentType_1.ComponentType.Library) { if (typeof data.name !== 'string') { return undefined; } const [name, group] = (0, packageJson_1.splitNameGroup)(data.name); if (name.length <= 0) { return undefined; } const author = typeof data.author === 'string' ? data.author : (typeof data.author?.name === 'string' ? data.author.name : undefined); const description = typeof data.description === 'string' ? data.description : undefined; const version = typeof data.version === 'string' ? data.version : undefined; const externalReferences = this.#extRefFactory.makeExternalReferences(data); const licenses = new license_1.LicenseRepository(); if (typeof data.license === 'string') { licenses.add(this.#licenseFactory.makeFromString(data.license)); } if (Array.isArray(data.licenses)) { for (const licenseData of data.licenses) { if (typeof licenseData.type === 'string') { const license = this.#licenseFactory.makeDisjunctive(licenseData.type); license.url = typeof licenseData.url === 'string' ? licenseData.url : undefined; licenses.add(license); } } } return new component_1.Component(type, name, { author, description, externalReferences: new externalReference_1.ExternalReferenceRepository(externalReferences), group, licenses, version }); } } exports.ComponentBuilder = ComponentBuilder; //# sourceMappingURL=fromNodePackageJson.node.js.map