Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some deps #165

Merged
merged 4 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@
"node-fetch": "^2.6.0",
"node-persist": "^3.0.5",
"ora": "^4.0.3",
"packageurl-js": "^0.0.1",
"parse-packagejson-name": "^1.0.1",
"prettify-xml": "^1.2.0",
"read-installed": "~4.0.3",
"spdx-license-ids": "^3.0.5",
"ssri": "^6.0.0",
Expand Down
38 changes: 31 additions & 7 deletions src/CycloneDX/CycloneDXSbomCreator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="./typings/packageurl-js" />
/// <reference types="./typings/parse-packagejson-name" />
/// <reference types="./typings/read-installed" />
/// <reference types="./typings/spdx-license-ids" />
/*
Expand All @@ -21,8 +19,6 @@ import { Options } from './Options';
import uuidv4 from 'uuid/v4';
import builder from 'xmlbuilder';
import readInstalled from 'read-installed';
import PackageURL from 'packageurl-js';
import parsePackageJsonName from 'parse-packagejson-name';
import * as ssri from 'ssri';
import * as fs from 'fs';
import { LicenseContent } from './Types/LicenseContent';
Expand All @@ -31,6 +27,7 @@ import { ExternalReference } from './Types/ExternalReference';
import { Hash } from './Types/Hash';
import spdxLicensesNonDeprecated = require('spdx-license-ids');
import spdxLicensesDeprecated = require('spdx-license-ids/deprecated');
import { toPurl } from './Helpers/Helpers';

export class CycloneDXSbomCreator {
readonly licenseFilenames: Array<string> = [
Expand Down Expand Up @@ -113,11 +110,11 @@ export class CycloneDXSbomCreator {
return;
}
if (!isRootPkg) {
const pkgIdentifier = parsePackageJsonName(pkg.name);
const group: string = pkgIdentifier.scope == null ? '' : `@${pkgIdentifier.scope}`;
const pkgIdentifier = this.parsePackageJsonName(pkg.name);
const group: string = pkgIdentifier.scope == undefined ? '' : `@${pkgIdentifier.scope}`;
const name: string = pkgIdentifier.fullName as string;
const version: string = pkg.version as string;
const purl: string = new PackageURL('npm', group, name, version, null, null).toString();
const purl: string = toPurl(name, version, group);
const description: GenericDescription = { '#cdata': pkg.description };

const component: Component = {
Expand Down Expand Up @@ -302,4 +299,31 @@ export class CycloneDXSbomCreator {
}
return undefined;
}

private parsePackageJsonName(name: string): Result {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, super nice of them!

const result: Result = {
scope: undefined,
fullName: '',
projectName: '',
moduleName: '',
};

const regexp = new RegExp(/^(?:@([^/]+)\/)?(([^\.]+)(?:\.(.*))?)$/);

const matches = name.match(regexp);
if (matches) {
result.scope = matches[1] || undefined;
result.fullName = matches[2] || matches[0];
result.projectName = matches[3] === matches[2] ? undefined : matches[3];
result.moduleName = matches[4] || matches[2] || undefined;
}
return result;
}
}

interface Result {
scope?: string;
fullName: string;
projectName?: string;
moduleName?: string;
}
22 changes: 22 additions & 0 deletions src/CycloneDX/Helpers/Helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2020-present Sonatype, Inc.
*
* 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.
*/

export const toPurl = (name: string, version: string, group = ''): string => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahaha that's all there was to packageurl-js!

We left-pad'd ourselves! 🤣

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I get why they used it (why not? It's a package that does what they want), but it was simple enough to just implement.

if (group != '') {
return `pkg:npm/${encodeURIComponent(group)}/${name}@${version}`;
}
return `pkg:npm/${name}@${version}`;
};
1 change: 0 additions & 1 deletion src/CycloneDX/typings/packageurl-js/index.d.ts

This file was deleted.