Skip to content

Commit 9c7df3a

Browse files
committed
feat(experimental): fall back to local spdx data and filter deprecated
* Added a copy of SPDX data locally to fall back on * This updates rarely so not a huge deal * Using a library would be another dependency to review/audit * can update with `npm run update-local-licenses` * Added cli option to allow deprecated licenses, by default filter out * fixed some tsc/linter warnings * Use aliases where appropriate
1 parent 67663a9 commit 9c7df3a

File tree

7 files changed

+8612
-71
lines changed

7 files changed

+8612
-71
lines changed

experimental/li-cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"start": "ts-node src/cli.ts",
1111
"build": "rimraf dist && tsc --project tsconfig.publish.json && tsc-alias && chmod u+x ./dist/cli.js",
1212
"type-check": "tsc --noEmit",
13-
"test": "jest --forceExit --detectOpenHandles"
13+
"test": "jest --forceExit --detectOpenHandles",
14+
"update-local-licenses": "wget https://spdx.org/licenses/licenses.json -O src/lib/licenses.json"
1415
},
1516
"dependencies": {
1617
"@inquirer/prompts": "^7.5.0",

experimental/li-cli/src/cli.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import yargs from 'yargs';
44
import { hideBin } from 'yargs/helpers';
5-
import addLicenseCMD from './cmds/add-license';
5+
import addLicenseCMD from '@/cmds/add-license';
66
import process from 'node:process';
77

88
yargs(hideBin(process.argv))
@@ -24,12 +24,19 @@ yargs(hideBin(process.argv))
2424
default: false,
2525
describe: 'require successful collection of info from Choose A License',
2626
})
27-
.demandOption('li-url'),
27+
.option('allow-deprecated', {
28+
type: 'boolean',
29+
default: false,
30+
describe: 'allow for adding depricated licenses',
31+
})
32+
.demandOption('li-url')
33+
.strict(),
2834
async (argv) => {
2935
try {
3036
await addLicenseCMD(argv['li-url'], {
3137
spdxID: argv.SPDXID,
3238
requireCal: argv['require-cal'],
39+
allowDeprecated: argv['allow-deprecated'],
3340
});
3441
} catch (e) {
3542
process.exit(1);

experimental/li-cli/src/cmds/add-license.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { setTimeout } from 'node:timers/promises';
22
import { search } from '@inquirer/prompts';
3-
import { getLicenseList, LicensesMap } from '../lib/spdx';
4-
import { CalInfo, InventoryLicense, pushLicense } from '../lib/inventory';
5-
import { getCALData } from '../lib/chooseALicense';
3+
import { getLicenseList, LicensesMap } from '@/lib/spdx';
4+
import { CalInfo, InventoryLicense, pushLicense } from '@/lib/inventory';
5+
import { getCALData } from '@/lib/chooseALicense';
66

77
// const answer = await input({ message: 'Enter your name' });
88

@@ -14,6 +14,7 @@ type Option = {
1414
type AddLicenseCMDOptions = {
1515
spdxID?: string;
1616
requireCal?: boolean;
17+
allowDeprecated?: boolean;
1718
};
1819

1920
async function promptForSPDXID(licenseList: LicensesMap): Promise<string> {
@@ -40,7 +41,7 @@ async function promptForSPDXID(licenseList: LicensesMap): Promise<string> {
4041
// TODO: add multiple licenses at the same time
4142
async function addLicenseCMD(liURL: string, options?: AddLicenseCMDOptions) {
4243
console.info('fetching license list from `spdx.org`');
43-
const licenseList = await getLicenseList();
44+
const licenseList = await getLicenseList(options?.allowDeprecated ?? false);
4445
console.info('done fetching');
4546

4647
const selectedLicenseID = options?.spdxID ?? (await promptForSPDXID(licenseList));
@@ -58,9 +59,13 @@ async function addLicenseCMD(liURL: string, options?: AddLicenseCMDOptions) {
5859
const cal = await getCALData(selectedLicense.licenseId.toLowerCase());
5960
chooseALicenseInfo = cal;
6061
} catch (e) {
61-
console.log('failed to get info from Choose A License');
62+
const errMsg = 'failed to get info from Choose A License';
63+
6264
if (options?.requireCal) {
65+
console.error(errMsg);
6366
throw new Error('forced the need for CAL data');
67+
} else {
68+
console.warn(errMsg + ', continuing...');
6469
}
6570
}
6671

experimental/li-cli/src/lib/chooseALicense.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ const chooseALicenseSchema = z.object({
88
permissions: z.string().array(),
99
});
1010

11-
type ChooseALicenseData = z.infer<typeof chooseALicenseSchema>;
11+
export type ChooseALicenseData = z.infer<typeof chooseALicenseSchema>;
1212

1313
function extractPreamble(text: string): string {
1414
const lines = text.split('\n'); // Split into lines
15-
let result: string[] = [];
15+
const result: string[] = [];
1616
let insideBlock = false;
1717

1818
for (const line of lines) {

0 commit comments

Comments
 (0)