From b1b41b8de44a10a3d463d0dc610a1e09dead9507 Mon Sep 17 00:00:00 2001 From: ghe Date: Wed, 9 Sep 2020 22:29:35 +0100 Subject: [PATCH] feat: json report + cli args --- package.json | 4 ++- src/cmds/generate.ts | 0 src/cmds/json.ts | 25 +++++++++++++++++++ src/index.ts | 16 ++++++------ ...ndex.ts => generate-org-license-report.ts} | 9 +------ src/lib/types.ts | 9 +++++++ test/lib/fecth-non-spdx-license-text.test.ts | 6 +++-- test/lib/fetch-spdx-license.test.ts | 2 +- test/lib/generate-license-report-data.test.ts | 2 +- tsconfig.json | 2 +- 10 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 src/cmds/generate.ts create mode 100644 src/cmds/json.ts rename src/lib/{index.ts => generate-org-license-report.ts} (93%) diff --git a/package.json b/package.json index 2de1481..de7f220 100644 --- a/package.json +++ b/package.json @@ -37,12 +37,14 @@ "axios": "^0.19.2", "cheerio": "1.0.0-rc.3", "debug": "4.1.1", + "handlebars": "4.7.6", "lodash": "4.17.20", "node-fetch": "2.6.0", "snyk-api-ts-client": "1.5.0", "snyk-config": "^3.0.0", "source-map-support": "^0.5.16", - "tslib": "2.0.1" + "tslib": "2.0.1", + "yargs": "16.0.2" }, "devDependencies": { "@types/jest": "26.0.13", diff --git a/src/cmds/generate.ts b/src/cmds/generate.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/cmds/json.ts b/src/cmds/json.ts new file mode 100644 index 0000000..d222446 --- /dev/null +++ b/src/cmds/json.ts @@ -0,0 +1,25 @@ +import { getApiToken } from '../lib/get-api-token'; +import { generateOrgLicensesReport } from '../lib/generate-org-license-report'; + +export const command = 'json'; +export const desc = 'Generate org licenses & dependencies data in JSON format'; +export const builder = { + orgPublicId: { + required: true, + default: undefined, + }, +}; +export const aliases = ['j']; + +export async function handler(argv: { orgPublicId: string }) { + try { + // check SNYK_TOKEN is set as the sdk uses it + getApiToken(); + // TODO: define and pass options to help filter the response + // based on filters available in API + const data = await generateOrgLicensesReport(argv.orgPublicId, {}); + console.log(JSON.stringify(data)); + } catch (e) { + console.error(e); + } +} diff --git a/src/index.ts b/src/index.ts index 329d8a3..5df6424 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,8 @@ -import { generateOrgLicensesReport, getApiToken } from './lib'; +#!/usr/bin/env node +import * as yargs from 'yargs'; -export async function main() { - // check SNYK_TOKEN is set as the sdk uses it - getApiToken(); - // check CLI properties passed in - const orgPublicId = 'blah'; - const options = {}; - await generateOrgLicensesReport(orgPublicId, options); -} +yargs + .commandDir('cmds') + .demandCommand() + .help() + .argv diff --git a/src/lib/index.ts b/src/lib/generate-org-license-report.ts similarity index 93% rename from src/lib/index.ts rename to src/lib/generate-org-license-report.ts index 9fedd78..e9365ad 100644 --- a/src/lib/index.ts +++ b/src/lib/generate-org-license-report.ts @@ -5,7 +5,7 @@ export * from './license-text'; export * from './get-api-token'; import { getLicenseDataForOrg, getDependenciesDataForOrg } from './api/org'; import { fetchSpdxLicenseTextAndUrl, fetchNonSpdxLicenseTextAndUrl } from './license-text'; -import { LicenseReportDataEntry } from './types'; +import { LicenseReportDataEntry, EnrichedDependency, Dependency } from './types'; const debug = debugLib('generateOrgLicensesReport'); @@ -75,13 +75,6 @@ export async function generateLicenseData( } } -interface Dependency { - id: string; // example: pako@1.0.11 - name: string; - version: string; - packageManager: string; -} -interface EnrichedDependency extends Dependency {} function enrichDependencies( dependencies: Dependency[], diff --git a/src/lib/types.ts b/src/lib/types.ts index e82e286..78b4320 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,3 +1,12 @@ +export interface Dependency { + id: string; // example: pako@1.0.11 + name: string; + version: string; + packageManager: string; +} +//TODO: finish the type +export interface EnrichedDependency extends Dependency {} + export interface LicenseReportDataEntry { // TODO: what if it is a dual license? /** diff --git a/test/lib/fecth-non-spdx-license-text.test.ts b/test/lib/fecth-non-spdx-license-text.test.ts index df0d673..815c576 100644 --- a/test/lib/fecth-non-spdx-license-text.test.ts +++ b/test/lib/fecth-non-spdx-license-text.test.ts @@ -1,7 +1,9 @@ -import { fetchNonSpdxLicenseTextAndUrl } from '../../src/lib'; +import { fetchNonSpdxLicenseTextAndUrl } from '../../src/lib/license-text'; test('ASPSecurityKit-Khosla-Tech license fetched locally as expected', async () => { - const licenseText = await fetchNonSpdxLicenseTextAndUrl('ASPSecurityKit-Khosla-Tech'); + const licenseText = await fetchNonSpdxLicenseTextAndUrl( + 'ASPSecurityKit-Khosla-Tech', + ); expect(licenseText).toMatchSnapshot(); }); diff --git a/test/lib/fetch-spdx-license.test.ts b/test/lib/fetch-spdx-license.test.ts index fad21c1..589e576 100644 --- a/test/lib/fetch-spdx-license.test.ts +++ b/test/lib/fetch-spdx-license.test.ts @@ -1,4 +1,4 @@ -import { fetchSpdxLicenseTextAndUrl } from '../../src/lib'; +import { fetchSpdxLicenseTextAndUrl } from '../../src/lib/generate-org-license-report'; test('AFL-1.1 license is as expected', async () => { const licenseText = await fetchSpdxLicenseTextAndUrl('AFL-1.1'); diff --git a/test/lib/generate-license-report-data.test.ts b/test/lib/generate-license-report-data.test.ts index fff8263..d514bbf 100644 --- a/test/lib/generate-license-report-data.test.ts +++ b/test/lib/generate-license-report-data.test.ts @@ -1,4 +1,4 @@ -import { generateLicenseData } from '../../src/lib'; +import { generateLicenseData } from '../../src/lib/generate-org-license-report'; describe('Get org licenses', () => { const OLD_ENV = process.env; diff --git a/tsconfig.json b/tsconfig.json index 4bd59e2..55bc439 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,5 +6,5 @@ "sourceMap": true, "declaration": true }, - "include": ["./src/lib/**/*"] + "include": ["./src/lib/**/*", "./src/**/*", "./src/cmds"] }