Skip to content

Commit

Permalink
Merge pull request #6 from snyk-tech-services/feat/introduce-yargs-an…
Browse files Browse the repository at this point in the history
…d-json

feat: json report + cli args
  • Loading branch information
lili2311 authored Sep 9, 2020
2 parents 889148d + b1b41b8 commit f942e8b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 23 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Empty file added src/cmds/generate.ts
Empty file.
25 changes: 25 additions & 0 deletions src/cmds/json.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 7 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
9 changes: 1 addition & 8 deletions src/lib/index.ts → src/lib/generate-org-license-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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[],
Expand Down
9 changes: 9 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -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?
/**
Expand Down
6 changes: 4 additions & 2 deletions test/lib/fecth-non-spdx-license-text.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});

Expand Down
2 changes: 1 addition & 1 deletion test/lib/fetch-spdx-license.test.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/lib/generate-license-report-data.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"sourceMap": true,
"declaration": true
},
"include": ["./src/lib/**/*"]
"include": ["./src/lib/**/*", "./src/**/*", "./src/cmds"]
}

0 comments on commit f942e8b

Please sign in to comment.