diff --git a/src/cmds/generate.ts b/src/cmds/generate.ts index 865df48..56c3903 100644 --- a/src/cmds/generate.ts +++ b/src/cmds/generate.ts @@ -29,9 +29,11 @@ export const builder = { outputFormat: { default: OutputFormat.HTML, desc: 'Report format', + // TODO: add also PDF when ready options: [OutputFormat.HTML] }, view: { + // TODO: add also dependency based view when ready default: SupportedViews.ORG_LICENSES, desc: 'How should the data be represented. Defaults to a license based view.', }, @@ -47,7 +49,7 @@ export async function handler(argv: { try { const { orgPublicId, outputFormat, template, view } = argv; debug( - 'Options: ' + + 'ℹ️ Options: ' + JSON.stringify({ orgPublicId, outputFormat, template, view }), ); getApiToken(); diff --git a/src/cmds/json.ts b/src/cmds/json.ts index 44252df..fe64052 100644 --- a/src/cmds/json.ts +++ b/src/cmds/json.ts @@ -1,5 +1,10 @@ +import * as debugLib from 'debug'; + import { getApiToken } from '../lib/get-api-token'; import { generateLicenseData } from '../lib/generate-org-license-report'; +import { SupportedViews } from '../lib/generate-output'; + +const debug = debugLib('snyk-licenses:json'); export const command = 'json'; export const desc = 'Generate org licenses & dependencies data in JSON format'; @@ -8,16 +13,27 @@ export const builder = { required: true, default: undefined, }, + view: { + // TODO: add also dependency based view when ready + default: SupportedViews.ORG_LICENSES, + desc: + 'How should the data be represented. Defaults to a license based view.', + }, }; export const aliases = ['j']; -export async function handler(argv: { orgPublicId: string }) { +export async function handler(argv: { + orgPublicId: string; + view: SupportedViews; +}) { try { + const { orgPublicId, view } = argv; + debug('ℹ️ Options: ' + JSON.stringify({ orgPublicId, view })); // 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 generateLicenseData(argv.orgPublicId, {}); + const data = await generateLicenseData(orgPublicId, {}); console.log(JSON.stringify(data)); } catch (e) { console.error(e); diff --git a/src/lib/api/org/dependencies.ts b/src/lib/api/org/dependencies.ts index a54ab2d..ecf4da2 100644 --- a/src/lib/api/org/dependencies.ts +++ b/src/lib/api/org/dependencies.ts @@ -32,7 +32,7 @@ export async function getDependenciesDataForOrg( ); return dependenciesData; } catch (e) { - debug('Failed to fetch dependencies' + e); + debug('❌ Failed to fetch dependencies' + e); throw e; } } diff --git a/src/lib/api/org/licenses.ts b/src/lib/api/org/licenses.ts index 80a298c..9e747a7 100644 --- a/src/lib/api/org/licenses.ts +++ b/src/lib/api/org/licenses.ts @@ -27,7 +27,7 @@ export async function getLicenseDataForOrg( const licenseData = await getAllLicensesData(snykApiClient, body, sortBy, order); return licenseData; } catch (e) { - debug('Failed to fetch licenses' + e); + debug('❌ Failed to fetch licenses' + e); throw e; } } diff --git a/src/lib/generate-org-license-report.ts b/src/lib/generate-org-license-report.ts index c3a56a8..0bfe51a 100644 --- a/src/lib/generate-org-license-report.ts +++ b/src/lib/generate-org-license-report.ts @@ -17,20 +17,20 @@ export async function generateLicenseData( orgPublicId: string, options?, ): Promise { - debug(`Generating license data for Org:${orgPublicId}`); + debug(`ℹ️ Generating license data for Org:${orgPublicId}`); try { const licenseData = await getLicenseDataForOrg(orgPublicId, options); - debug(`Got license API data for Org:${orgPublicId}`); + debug(`✅ Got license API data for Org:${orgPublicId}`); const dependenciesDataRaw = await getDependenciesDataForOrg( orgPublicId, options, ); - debug(`Got dependencies API data for Org:${orgPublicId}`); + debug(`✅ Got dependencies API data for Org:${orgPublicId}`); const licenseReportData: LicenseReportData = {}; const dependenciesData = _.groupBy(dependenciesDataRaw.results, 'id'); // TODO: what if 0? - debug(`Processing ${licenseData.total} licenses`); + debug(`⏳ Processing ${licenseData.total} licenses`); const dependenciesAll = []; for (const license of licenseData.results) { @@ -55,9 +55,11 @@ export async function generateLicenseData( licenseUrl: licenseData?.licenseUrl, }; } + debug(`✅ Done processing ${licenseData.total} licenses`); + return licenseReportData; } catch (e) { - debug('Failed to generate report data', e); + debug('❌ Failed to generate report data', e); throw e; } } @@ -89,12 +91,12 @@ async function getLicenseTextAndUrl( try { return await fetchSpdxLicenseTextAndUrl(id); } catch (e) { - debug(`Failed to get license data for as SPDX, trying non-SPDX: ${id}`); + debug(`❌ Failed to get license data for as SPDX, trying non-SPDX: ${id}`); } try { return await fetchNonSpdxLicenseTextAndUrl(id); } catch (e) { - debug(`Failed to get license data as non-SPDX: ${id}`); + debug(`❌ Failed to get license data as non-SPDX: ${id}`); } return undefined; diff --git a/src/lib/generate-output/html/index.ts b/src/lib/generate-output/html/index.ts index 95cb8f2..05e32e8 100644 --- a/src/lib/generate-output/html/index.ts +++ b/src/lib/generate-output/html/index.ts @@ -1,8 +1,12 @@ import * as Handlebars from 'handlebars'; import * as path from 'path'; import * as fs from 'fs'; +import * as debugLib from 'debug'; + import { LicenseReportData } from '../../generate-org-license-report'; +const debug = debugLib('snyk-licenses:generateHtmlReport'); + export const enum SupportedViews { ORG_LICENSES = 'org-licenses', // TODO: support later diff --git a/test/system/json.test.ts b/test/system/json.test.ts index 61dff7d..6664d97 100644 --- a/test/system/json.test.ts +++ b/test/system/json.test.ts @@ -11,9 +11,9 @@ describe('`snyk-licenses-report json <...>`', () => { }); }); it('Generated JSON data with correct --orgPublicId', async (done) => { - exec(`node ${main} json --orgPublicId=${ORG_ID}`, (err, stdout) => { + exec(`DEBUG=snyk-license* node ${main} json --orgPublicId=${ORG_ID}`, (err, stdout) => { expect(err).toBeNull(); - console.log({err, stdout}) + console.log({err, stdout, ORG_ID}) expect(stdout.trim()).toMatch("BSD-2-Clause"); done(); });