@repometry/cli 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +8 -5
  2. package/package.json +3 -3
  3. package/target/main/resources/report/json/report-v2.schema.json +1 -0
  4. package/target/main/resources/report/json/repository-summary-v1.schema.json +12 -0
  5. package/target/main/resources/report/json/viewer-inspection-v1.schema.json +31 -0
  6. package/target/main/resources/viewer/index.html +9 -9
  7. package/target/main/resources/viewer/styles.css +8 -9
  8. package/target/main/ts/cli/distribution.d.ts +8 -0
  9. package/target/main/ts/cli/distribution.js +23 -0
  10. package/target/main/ts/cli/imports.js +21 -17
  11. package/target/main/ts/cli/run.d.ts +2 -2
  12. package/target/main/ts/cli/run.js +7 -4
  13. package/target/main/ts/compose/analysis-inventory.d.ts +12 -0
  14. package/target/main/ts/compose/analysis-inventory.js +18 -0
  15. package/target/main/ts/compose/analysis-report.js +3 -16
  16. package/target/main/ts/compose/analysis-results.d.ts +1 -0
  17. package/target/main/ts/compose/analysis-results.js +14 -12
  18. package/target/main/ts/compose/built-ins.d.ts +2 -2
  19. package/target/main/ts/compose/built-ins.js +2 -2
  20. package/target/main/ts/compose/load-plugins.js +9 -6
  21. package/target/main/ts/plugins/built-in.d.ts +2 -0
  22. package/target/main/ts/plugins/built-in.js +2 -1
  23. package/target/main/ts/plugins/json-spans.js +56 -42
  24. package/target/main/ts/plugins/legal/composer-lock.js +1 -1
  25. package/target/main/ts/plugins/legal/cyclonedx.d.ts +2 -0
  26. package/target/main/ts/plugins/legal/cyclonedx.js +61 -0
  27. package/target/main/ts/plugins/legal/dependencies.d.ts +53 -7
  28. package/target/main/ts/plugins/legal/dependencies.js +34 -14
  29. package/target/main/ts/plugins/legal/expression.d.ts +2 -0
  30. package/target/main/ts/plugins/legal/expression.js +69 -0
  31. package/target/main/ts/plugins/legal/licenses.d.ts +1 -0
  32. package/target/main/ts/plugins/legal/licenses.js +55 -28
  33. package/target/main/ts/plugins/legal/npm-lock.js +2 -1
  34. package/target/main/ts/plugins/legal/policy.d.ts +7 -11
  35. package/target/main/ts/plugins/legal/policy.js +23 -102
  36. package/target/main/ts/plugins/legal/sbom-model.d.ts +7 -0
  37. package/target/main/ts/plugins/legal/sbom-model.js +11 -0
  38. package/target/main/ts/plugins/legal/sbom.js +2 -116
  39. package/target/main/ts/plugins/legal/spdx-catalog.d.ts +9 -0
  40. package/target/main/ts/plugins/legal/spdx-catalog.js +28 -0
  41. package/target/main/ts/plugins/legal/spdx-sbom.d.ts +2 -0
  42. package/target/main/ts/plugins/legal/spdx-sbom.js +65 -0
  43. package/target/main/ts/report/html/app/viewer-metric-format.d.ts +2 -0
  44. package/target/main/ts/report/html/app/viewer-metric-format.js +17 -0
  45. package/target/main/ts/report/html/app/viewer-metrics.d.ts +1 -2
  46. package/target/main/ts/report/html/app/viewer-metrics.js +1 -17
  47. package/target/main/ts/report/html/app/viewer-overview-cards.js +15 -37
  48. package/target/main/ts/report/html/app/viewer-overview-lead.js +1 -1
  49. package/target/main/ts/report/html/site/asset-versions.d.ts +2 -0
  50. package/target/main/ts/report/html/site/asset-versions.js +36 -0
  51. package/target/main/ts/report/html/site/badges.d.ts +5 -1
  52. package/target/main/ts/report/html/site/badges.js +17 -11
  53. package/target/main/ts/report/html/site/export-site.js +2 -1
  54. package/target/main/ts/report/html/site/local-server.js +2 -0
  55. package/target/main/ts/report/html/site/projects.js +4 -3
  56. package/target/main/ts/report/json/projections/inspection-details.js +3 -0
  57. package/target/main/ts/report/json/wire/inspection-details-model.d.ts +2 -0
@@ -22,50 +22,64 @@ export function locateJsonSpans(text, wanted, depth) {
22
22
  if (path !== undefined && wanted.has(path))
23
23
  spans.set(path, { line: start, endLine: line });
24
24
  };
25
- for (let index = 0; index < text.length;) {
26
- const char = text[index];
27
- if (char === '\n')
28
- line++;
29
- if (char === '\n' || char === ' ' || char === '\t' || char === '\r' || char === ':') {
30
- index++;
31
- }
32
- else if (char === ',') {
33
- const parent = stack.at(-1);
34
- if (parent?.array)
35
- parent.index++;
36
- else if (parent)
37
- parent.expectKey = true;
38
- index++;
39
- }
40
- else if (char === '"') {
41
- const end = stringEnd(text, index);
42
- const parent = stack.at(-1);
43
- if (parent && !parent.array && parent.expectKey) {
44
- const raw = text.slice(index + 1, end);
45
- parent.key = raw.includes('\\') ? JSON.parse(text.slice(index, end + 1)) : raw;
46
- parent.keyLine = line;
47
- parent.expectKey = false;
48
- }
49
- else {
50
- scalar();
51
- }
52
- index = end + 1;
53
- }
54
- else if (char === '{' || char === '[') {
55
- const { path, line: start } = child();
56
- stack.push({ path, line: start, array: char === '[', index: 0, key: '', keyLine: line, expectKey: char === '{' });
57
- index++;
58
- }
59
- else if (char === '}' || char === ']') {
60
- const frame = stack.pop();
61
- if (frame?.path !== undefined && wanted.has(frame.path))
62
- spans.set(frame.path, { line: frame.line, endLine: line });
63
- index++;
25
+ const nextMember = () => {
26
+ const parent = stack.at(-1);
27
+ if (parent?.array)
28
+ parent.index++;
29
+ else if (parent)
30
+ parent.expectKey = true;
31
+ };
32
+ const readString = (index) => {
33
+ const end = stringEnd(text, index);
34
+ const parent = stack.at(-1);
35
+ if (parent && !parent.array && parent.expectKey) {
36
+ const raw = text.slice(index + 1, end);
37
+ parent.key = raw.includes('\\') ? JSON.parse(text.slice(index, end + 1)) : raw;
38
+ parent.keyLine = line;
39
+ parent.expectKey = false;
64
40
  }
65
- else {
41
+ else
66
42
  scalar();
67
- while (index < text.length && !/[\s,\]}]/.test(text[index]))
68
- index++;
43
+ return end + 1;
44
+ };
45
+ const open = (array) => {
46
+ const { path, line: start } = child();
47
+ stack.push({ path, line: start, array, index: 0, key: '', keyLine: line, expectKey: !array });
48
+ };
49
+ const close = () => {
50
+ const frame = stack.pop();
51
+ if (frame?.path !== undefined && wanted.has(frame.path))
52
+ spans.set(frame.path, { line: frame.line, endLine: line });
53
+ };
54
+ for (let index = 0; index < text.length; index++) {
55
+ const char = text[index];
56
+ switch (char) {
57
+ case '\n':
58
+ line++;
59
+ break;
60
+ case ' ':
61
+ case '\t':
62
+ case '\r':
63
+ case ':':
64
+ break;
65
+ case ',':
66
+ nextMember();
67
+ break;
68
+ case '"':
69
+ index = readString(index) - 1;
70
+ break;
71
+ case '{':
72
+ case '[':
73
+ open(char === '[');
74
+ break;
75
+ case '}':
76
+ case ']':
77
+ close();
78
+ break;
79
+ default:
80
+ scalar();
81
+ while (index + 1 < text.length && !/[\s,\]}]/.test(text[index + 1]))
82
+ index++;
69
83
  }
70
84
  }
71
85
  return spans;
@@ -12,7 +12,7 @@ export function readComposerLock(text) {
12
12
  if (!isRecord(entry) || typeof entry.name !== 'string' || !entry.name.trim())
13
13
  return [];
14
14
  const license = declaredLicense(entry.license);
15
- return [{ at: pointer([section, index]), name: entry.name, dev: section === 'packages-dev',
15
+ return [{ at: pointer([section, index]), name: entry.name, groups: [section],
16
16
  ...(typeof entry.version === 'string' ? { version: entry.version } : {}),
17
17
  ...(license === undefined ? {} : { license }) }];
18
18
  }));
@@ -0,0 +1,2 @@
1
+ import type { Positioned } from './sbom-model.js';
2
+ export declare function cyclone(value: Record<string, unknown>): Positioned[];
@@ -0,0 +1,61 @@
1
+ import { isRecord } from '@repometry/core/internal/engine/plugin.js';
2
+ import { entries, text } from './sbom-model.js';
3
+ export function cyclone(value) {
4
+ if (value.bomFormat !== 'CycloneDX' || !['1.4', '1.5', '1.6'].some(version => version === value.specVersion)) {
5
+ throw new Error('Expected CycloneDX JSON version 1.4, 1.5 or 1.6');
6
+ }
7
+ const root = isRecord(value.metadata) && isRecord(value.metadata.component) ? value.metadata.component : undefined;
8
+ const rootId = text(root?.['bom-ref']);
9
+ const stack = [];
10
+ addComponents(stack, root?.components, ['metadata', 'component', 'components']);
11
+ addComponents(stack, value.components, ['components']);
12
+ const dependencies = [];
13
+ const seen = new Set(rootId === undefined ? [] : [rootId]);
14
+ while (stack.length) {
15
+ const { entry, at } = stack.pop();
16
+ if (!isRecord(entry))
17
+ throw new Error('A CycloneDX component must be an object');
18
+ addComponents(stack, entry.components, [...at, 'components']);
19
+ const component = readComponent(entry, at, seen);
20
+ if (component)
21
+ dependencies.push(component);
22
+ }
23
+ return dependencies;
24
+ }
25
+ function addComponents(stack, components, prefix) {
26
+ const array = entries(components, 'CycloneDX components');
27
+ for (let index = array.length - 1; index >= 0; index--)
28
+ stack.push({ entry: array[index], at: [...prefix, index] });
29
+ }
30
+ function readComponent(entry, at, seen) {
31
+ const name = text(entry.name);
32
+ if (name === undefined)
33
+ throw new Error('A CycloneDX component needs a name');
34
+ const id = text(entry['bom-ref']);
35
+ if (id !== undefined && seen.has(id))
36
+ return undefined;
37
+ if (id !== undefined)
38
+ seen.add(id);
39
+ const group = text(entry.group);
40
+ const version = text(entry.version);
41
+ const license = cycloneLicense(entry.licenses);
42
+ const scope = text(entry.scope);
43
+ return { at, name: group && !name.startsWith(group + '/') ? group + '/' + name : name,
44
+ ...(version === undefined ? {} : { version }), ...(license === undefined ? {} : { license }), groups: scope === undefined ? [] : [scope] };
45
+ }
46
+ function cycloneLicense(value) {
47
+ const licenses = entries(value, 'CycloneDX licenses');
48
+ const declarations = new Set(licenses.flatMap(entry => {
49
+ if (!isRecord(entry))
50
+ return [];
51
+ if (isRecord(entry.license)) {
52
+ const license = entry.license;
53
+ const value = text(license.id) ?? text(license.name);
54
+ return license.acknowledgement !== 'concluded' && value !== undefined ? [value] : [];
55
+ }
56
+ const expression = text(entry.expression);
57
+ return entry.acknowledgement !== 'concluded' && expression !== undefined ? [expression] : [];
58
+ }));
59
+ return declarations.size ? [...declarations].join(', ') : undefined;
60
+ }
61
+ //# sourceMappingURL=cyclonedx.js.map
@@ -1,4 +1,4 @@
1
- import type { LicenseImportFormat } from '@repometry/core/internal/model/license-evidence.js';
1
+ import type { LicenseGroupCount, LicenseImportFormat, LicenseKindCounts } from '@repometry/core/internal/model/license-evidence.js';
2
2
  import type { DataContract } from '@repometry/core/internal/engine/plugin.js';
3
3
  export declare const maxLicenseLength = 256;
4
4
  export interface Dependency {
@@ -6,23 +6,27 @@ export interface Dependency {
6
6
  readonly version?: string;
7
7
  readonly license?: string;
8
8
  readonly overlongLicense?: true;
9
- readonly dev: boolean;
9
+ readonly groups: readonly string[];
10
10
  readonly line: number;
11
11
  readonly endLine: number;
12
12
  }
13
- export interface DependencySource {
13
+ export interface RawDependencySource {
14
14
  readonly path: string;
15
15
  readonly packages: readonly Dependency[];
16
16
  readonly unverified?: true;
17
17
  readonly format?: LicenseImportFormat;
18
18
  }
19
+ export interface DependencySource extends RawDependencySource {
20
+ readonly groups: readonly LicenseGroupCount[];
21
+ readonly licenseKinds: LicenseKindCounts;
22
+ }
19
23
  export declare const dependencySources: DataContract<readonly DependencySource[]>;
20
24
  export declare const dependencySourcesSchema: {
21
25
  readonly type: 'array';
22
26
  readonly items: {
23
27
  readonly type: 'object';
24
28
  readonly additionalProperties: false;
25
- readonly required: readonly ['path', 'packages'];
29
+ readonly required: readonly ['path', 'packages', 'groups', 'licenseKinds'];
26
30
  readonly properties: {
27
31
  readonly path: {
28
32
  readonly type: 'string';
@@ -33,7 +37,7 @@ export declare const dependencySourcesSchema: {
33
37
  readonly items: {
34
38
  readonly type: 'object';
35
39
  readonly additionalProperties: false;
36
- readonly required: readonly ['name', 'dev', 'line', 'endLine'];
40
+ readonly required: readonly ['name', 'groups', 'line', 'endLine'];
37
41
  readonly properties: {
38
42
  readonly name: {
39
43
  readonly type: 'string';
@@ -48,8 +52,13 @@ export declare const dependencySourcesSchema: {
48
52
  readonly overlongLicense: {
49
53
  readonly const: true;
50
54
  };
51
- readonly dev: {
52
- readonly type: 'boolean';
55
+ readonly groups: {
56
+ readonly type: 'array';
57
+ readonly items: {
58
+ readonly type: 'string';
59
+ readonly minLength: 1;
60
+ };
61
+ readonly uniqueItems: true;
53
62
  };
54
63
  readonly line: {
55
64
  readonly type: 'integer';
@@ -68,6 +77,43 @@ export declare const dependencySourcesSchema: {
68
77
  readonly format: {
69
78
  readonly enum: readonly ["cyclonedx", "spdx"];
70
79
  };
80
+ readonly groups: {
81
+ readonly type: 'array';
82
+ readonly items: {
83
+ readonly type: 'object';
84
+ readonly additionalProperties: false;
85
+ readonly required: readonly ['name', 'count'];
86
+ readonly properties: {
87
+ readonly name: {
88
+ readonly type: 'string';
89
+ readonly minLength: 1;
90
+ };
91
+ readonly count: {
92
+ readonly type: 'integer';
93
+ readonly minimum: 0;
94
+ };
95
+ };
96
+ };
97
+ };
98
+ readonly licenseKinds: {
99
+ readonly type: 'object';
100
+ readonly additionalProperties: false;
101
+ readonly required: readonly ['osi', 'other', 'unknown'];
102
+ readonly properties: {
103
+ readonly osi: {
104
+ readonly type: 'integer';
105
+ readonly minimum: 0;
106
+ };
107
+ readonly other: {
108
+ readonly type: 'integer';
109
+ readonly minimum: 0;
110
+ };
111
+ readonly unknown: {
112
+ readonly type: 'integer';
113
+ readonly minimum: 0;
114
+ };
115
+ };
116
+ };
71
117
  };
72
118
  };
73
119
  };
@@ -1,27 +1,47 @@
1
- import { licenseImportFormats } from '@repometry/core/internal/model/license-evidence.js';
1
+ import { licenseDependencyInventory, licenseImportFormats, licenseInputSummary } from '@repometry/core/internal/model/license-evidence.js';
2
2
  import { isRecord } from '@repometry/core/internal/engine/plugin.js';
3
3
  export const maxLicenseLength = 256;
4
+ const count = { type: 'integer', minimum: 0 };
5
+ const group = { type: 'object', additionalProperties: false, required: ['name', 'count'],
6
+ properties: { name: { type: 'string', minLength: 1 }, count } };
7
+ const licenseKinds = { type: 'object', additionalProperties: false, required: ['osi', 'other', 'unknown'],
8
+ properties: { osi: count, other: count, unknown: count } };
4
9
  const dependency = {
5
- type: 'object', additionalProperties: false, required: ['name', 'dev', 'line', 'endLine'],
10
+ type: 'object', additionalProperties: false, required: ['name', 'groups', 'line', 'endLine'],
6
11
  properties: {
7
12
  name: { type: 'string', minLength: 1 }, version: { type: 'string' }, license: { type: 'string' }, overlongLicense: { const: true },
8
- dev: { type: 'boolean' }, line: { type: 'integer', minimum: 1 }, endLine: { type: 'integer', minimum: 1 },
13
+ groups: { type: 'array', items: { type: 'string', minLength: 1 }, uniqueItems: true }, line: { type: 'integer', minimum: 1 }, endLine: { type: 'integer', minimum: 1 },
9
14
  },
10
15
  };
11
16
  export const dependencySources = {
12
- id: 'repometry.license-dependencies/v1',
13
- accepts: (value) => Array.isArray(value) && value.every(source => isRecord(source)
14
- && (source.format === undefined || licenseImportFormats.some(format => format === source.format))
15
- && (source.unverified === undefined || source.unverified === true) && typeof source.path === 'string' && source.path.length > 0 && Array.isArray(source.packages)
16
- && source.packages.every(item => isRecord(item) && typeof item.name === 'string' && item.name.length > 0
17
- && (item.version === undefined || typeof item.version === 'string') && (item.license === undefined || typeof item.license === 'string')
18
- && (item.overlongLicense === undefined || item.overlongLicense === true)
19
- && typeof item.dev === 'boolean' && typeof item.line === 'number' && Number.isSafeInteger(item.line) && item.line >= 1
20
- && typeof item.endLine === 'number' && Number.isSafeInteger(item.endLine) && item.endLine >= item.line)),
17
+ id: licenseDependencyInventory,
18
+ accepts: (value) => Array.isArray(value) && value.every(isDependencySource),
21
19
  };
20
+ function isDependencySource(value) {
21
+ if (!isRecord(value) || !Array.isArray(value.packages) || !licenseInputSummary(value))
22
+ return false;
23
+ const format = value.format === undefined || licenseImportFormats.some(format => format === value.format);
24
+ return format && (value.unverified === undefined || value.unverified === true) && value.packages.every(isDependency);
25
+ }
26
+ function isDependency(value) {
27
+ if (!isRecord(value) || typeof value.name !== 'string' || !value.name)
28
+ return false;
29
+ const groups = Array.isArray(value.groups) && value.groups.every(group => typeof group === 'string' && group.length > 0);
30
+ const declaration = optionalText(value.version) && optionalText(value.license) && (value.overlongLicense === undefined || value.overlongLicense === true);
31
+ return groups && declaration && validRange(value.line, value.endLine);
32
+ }
33
+ function optionalText(value) {
34
+ return value === undefined || typeof value === 'string';
35
+ }
36
+ function validRange(line, endLine) {
37
+ return isLine(line) && isLine(endLine) && endLine >= line;
38
+ }
39
+ function isLine(value) {
40
+ return typeof value === 'number' && Number.isSafeInteger(value) && value >= 1;
41
+ }
22
42
  export const dependencySourcesSchema = {
23
43
  type: 'array',
24
- items: { type: 'object', additionalProperties: false, required: ['path', 'packages'],
25
- properties: { path: { type: 'string', minLength: 1 }, packages: { type: 'array', items: dependency }, unverified: { const: true }, format: { enum: licenseImportFormats } } },
44
+ items: { type: 'object', additionalProperties: false, required: ['path', 'packages', 'groups', 'licenseKinds'],
45
+ properties: { path: { type: 'string', minLength: 1 }, packages: { type: 'array', items: dependency }, unverified: { const: true }, format: { enum: licenseImportFormats }, groups: { type: 'array', items: group }, licenseKinds } },
26
46
  };
27
47
  //# sourceMappingURL=dependencies.js.map
@@ -0,0 +1,2 @@
1
+ export type Decision = 'allowed' | 'denied' | 'unknown' | 'unclassified';
2
+ export declare function evaluateLicenseExpression(expression: string | undefined, evaluate: (id: string) => Decision): Decision;
@@ -0,0 +1,69 @@
1
+ import { canonicalLicense, exceptions } from './spdx-catalog.js';
2
+ import { maxLicenseLength } from './dependencies.js';
3
+ const operators = new Set(['AND', 'OR', 'WITH', '(', ')']);
4
+ const expressionToken = /\s+|[()]|(?:[A-Za-z0-9.-]+:)?[A-Za-z0-9.-]+\+?/y;
5
+ export function evaluateLicenseExpression(expression, evaluate) {
6
+ if (expression === undefined || expression.length > maxLicenseLength)
7
+ return 'unknown';
8
+ const parsed = tokenize(expression);
9
+ if (parsed === undefined)
10
+ return 'unknown';
11
+ const tokens = parsed;
12
+ let cursor = 0;
13
+ function operand(depth) {
14
+ if (depth > 64)
15
+ return undefined;
16
+ const token = tokens[cursor++];
17
+ if (token === '(') {
18
+ const result = composite(depth + 1, 'OR');
19
+ return tokens[cursor++] === ')' ? result : undefined;
20
+ }
21
+ if (token === undefined || operators.has(token))
22
+ return undefined;
23
+ if (tokens[cursor] === 'WITH') {
24
+ cursor++;
25
+ const exception = tokens[cursor++];
26
+ if (!exception || !exceptions.has(exception.toLowerCase()))
27
+ return undefined;
28
+ }
29
+ const id = canonicalLicense(token);
30
+ return id === undefined ? undefined : evaluate(id);
31
+ }
32
+ function composite(depth, operator) {
33
+ const next = () => operator === 'AND' ? operand(depth) : composite(depth, 'AND');
34
+ let result = next();
35
+ while (result !== undefined && tokens[cursor] === operator) {
36
+ cursor++;
37
+ const right = next();
38
+ if (right === undefined)
39
+ return undefined;
40
+ result = combine(result, right, operator);
41
+ }
42
+ return result;
43
+ }
44
+ const decision = composite(0, 'OR');
45
+ return cursor === tokens.length ? decision ?? 'unknown' : 'unknown';
46
+ }
47
+ function tokenize(text) {
48
+ const tokens = [];
49
+ for (let index = 0; index < text.length; index = expressionToken.lastIndex) {
50
+ expressionToken.lastIndex = index;
51
+ const token = expressionToken.exec(text)?.[0];
52
+ if (!token)
53
+ return undefined;
54
+ if (token === 'WITH' && (!/\s/.test(text[index - 1] ?? '') || !/\s/.test(text[expressionToken.lastIndex] ?? '')))
55
+ return undefined;
56
+ if (token.trim())
57
+ tokens.push(token);
58
+ }
59
+ return tokens;
60
+ }
61
+ function combine(left, right, operator) {
62
+ const decisive = operator === 'AND' ? 'denied' : 'allowed';
63
+ if (left === decisive || right === decisive)
64
+ return decisive;
65
+ if (left === right)
66
+ return left;
67
+ return left === 'unknown' || right === 'unknown' ? 'unknown' : 'unclassified';
68
+ }
69
+ //# sourceMappingURL=expression.js.map
@@ -1,4 +1,5 @@
1
1
  import type { JsonValue } from '@repometry/core/internal/model/plugin-data.js';
2
+ export declare function rejectRemovedLicenseOptions(options: JsonValue | undefined): void;
2
3
  export declare function licenses(options?: JsonValue | undefined): {
3
4
  id: string;
4
5
  version: string;
@@ -1,12 +1,13 @@
1
1
  import { checkedDependencies, licenseFiles, licenseLockKind, licenseLockNames } from '@repometry/core/internal/model/license-evidence.js';
2
2
  import { bundledPlugin, pluginManifestSchema } from '@repometry/core/internal/engine/plugin-manifest.js';
3
3
  import { findingResult } from '@repometry/core/internal/engine/author-contracts.js';
4
+ import { isRecord } from '@repometry/core/internal/model/plugin-data.js';
4
5
  import { dependencySources, dependencySourcesSchema, maxLicenseLength } from './dependencies.js';
5
6
  import { readComposerLock } from './composer-lock.js';
6
7
  import { listsDependencies } from './unverified-lock.js';
7
8
  import { readSbom } from './sbom.js';
8
9
  import { readNpmLock } from './npm-lock.js';
9
- import { licenseDecision, licenseListVersion, licensePolicy, policySchema } from './policy.js';
10
+ import { licenseDecision, licenseKind, licenseListVersion, licensePolicy, policySchema } from './policy.js';
10
11
  const id = 'licenses';
11
12
  const checks = [
12
13
  { id: 'denied-license', description: 'A dependency declares a license outside the configured policy.' },
@@ -17,7 +18,13 @@ const checked = {
17
18
  id: checkedDependencies, label: 'Checked dependencies', unit: 'count', scope: 'file',
18
19
  aggregate: 'sum', better: 'none', group: 'Legal',
19
20
  };
21
+ export function rejectRemovedLicenseOptions(options) {
22
+ if (isRecord(options) && Object.hasOwn(options, 'dev')) {
23
+ throw new Error('The licenses dev option was removed. Every dependency is inspected. Switch categories in the report view.');
24
+ }
25
+ }
20
26
  export function licenses(options = undefined) {
27
+ rejectRemovedLicenseOptions(options);
21
28
  const policy = licensePolicy(options);
22
29
  return {
23
30
  ...bundledPlugin({
@@ -35,7 +42,7 @@ export function licenses(options = undefined) {
35
42
  try {
36
43
  const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
37
44
  const sources = format === undefined ? readLockfile(path, text) : [{ path, format, packages: readSbom(format, text) }];
38
- return sources.map(source => ({ ...source, packages: source.packages.map(boundLicense) }));
45
+ return sources.map(inventorySource);
39
46
  }
40
47
  catch (error) {
41
48
  context.emitDiagnostic?.('Unable to read ' + (format ?? 'lockfile') + ' license metadata: ' + (error instanceof Error ? error.message : String(error)), path);
@@ -45,32 +52,7 @@ export function licenses(options = undefined) {
45
52
  },
46
53
  }, {
47
54
  id: 'check', role: 'inspection', scope: 'repository', inputs: { dependencies: dependencySources }, output: findingResult,
48
- execute(context) {
49
- const findings = [];
50
- const measurements = [];
51
- const ignored = [];
52
- for (const { path, packages, unverified } of context.read('dependencies', dependencySources)) {
53
- const inspected = policy.dev ? packages : packages.filter(dependency => !dependency.dev);
54
- measurements.push({ definition: checkedDependencies, target: 'file', path, value: inspected.length });
55
- if (unverified)
56
- findings.push({ check: 'unverified-dependencies', level: 'low', path, anchor: '(file)',
57
- message: 'This lockfile has no supported dependency license data. Import an SBOM to check the dependencies.' });
58
- for (const dependency of inspected) {
59
- const { name, version, license, line, endLine } = dependency;
60
- const decision = licenseDecision(license, policy);
61
- if (decision === 'allowed')
62
- continue;
63
- const check = decision === 'denied' ? 'denied-license' : 'unknown-license';
64
- const label = version === undefined ? name : name + '@' + version;
65
- const reason = policy.exceptions.get(label) ?? policy.exceptions.get(name);
66
- if (reason !== undefined)
67
- ignored.push({ index: findings.length, reason });
68
- findings.push({ check, level: decision === 'denied' ? 'high' : 'medium', path, line, endLine, anchor: name,
69
- message: findingMessage(label, dependency, decision, policy.preset) });
70
- }
71
- }
72
- return { findings, measurements, ...(ignored.length ? { ignored } : {}) };
73
- },
55
+ execute: context => checkDependencies(context.read('dependencies', dependencySources), policy),
74
56
  }],
75
57
  }),
76
58
  }),
@@ -78,6 +60,51 @@ export function licenses(options = undefined) {
78
60
  ...(options === undefined ? {} : { settings: options }),
79
61
  };
80
62
  }
63
+ function inventorySource(source) {
64
+ const kinds = { osi: 0, other: 0, unknown: 0 };
65
+ const groups = new Map();
66
+ const packages = source.packages.map(boundLicense);
67
+ for (const item of packages) {
68
+ kinds[licenseKind(item.license)]++;
69
+ for (const group of item.groups)
70
+ groups.set(group, (groups.get(group) ?? 0) + 1);
71
+ }
72
+ return { ...source, packages, groups: [...groups].map(([name, count]) => ({ name, count })), licenseKinds: kinds };
73
+ }
74
+ function checkDependencies(sources, policy) {
75
+ const findings = [];
76
+ const measurements = [];
77
+ const ignored = [];
78
+ for (const { path, packages, unverified } of sources) {
79
+ measurements.push({ definition: checkedDependencies, target: 'file', path, value: packages.length });
80
+ if (unverified)
81
+ findings.push({ check: 'unverified-dependencies', level: 'low', path, anchor: '(file)',
82
+ message: 'This lockfile has no supported dependency license data. Import an SBOM to check the dependencies.' });
83
+ for (const dependency of packages) {
84
+ const finding = dependencyFinding(path, dependency, policy);
85
+ if (finding === undefined)
86
+ continue;
87
+ const label = dependencyLabel(dependency);
88
+ const reason = policy.exceptions.get(label) ?? policy.exceptions.get(dependency.name);
89
+ if (reason !== undefined)
90
+ ignored.push({ index: findings.length, reason });
91
+ findings.push(finding);
92
+ }
93
+ }
94
+ return { findings, measurements, ...(ignored.length ? { ignored } : {}) };
95
+ }
96
+ function dependencyFinding(path, dependency, policy) {
97
+ const { name, license, line, endLine, groups } = dependency;
98
+ const decision = licenseDecision(license, policy);
99
+ if (decision === 'allowed')
100
+ return undefined;
101
+ const denied = decision === 'denied';
102
+ return { check: denied ? 'denied-license' : 'unknown-license', level: denied ? 'high' : 'medium',
103
+ path, line, endLine, anchor: name, groups, message: findingMessage(dependencyLabel(dependency), dependency, decision, policy.preset) };
104
+ }
105
+ function dependencyLabel({ name, version }) {
106
+ return version === undefined ? name : name + '@' + version;
107
+ }
81
108
  function boundLicense(dependency) {
82
109
  if ((dependency.license?.length ?? 0) <= maxLicenseLength)
83
110
  return dependency;
@@ -1,6 +1,7 @@
1
1
  import { isRecord } from '@repometry/core/internal/engine/plugin.js';
2
2
  import { locateJsonSpans, pointer } from '../json-spans.js';
3
3
  import { declaredLicense } from './policy.js';
4
+ const categories = ['dev', 'optional', 'peer', 'devOptional', 'inBundle'];
4
5
  /** Older and unknown versions lack the supported package metadata. */
5
6
  export function readNpmLock(text) {
6
7
  const lock = JSON.parse(text);
@@ -20,7 +21,7 @@ export function readNpmLock(text) {
20
21
  const version = typeof entry.version === 'string' ? entry.version : undefined;
21
22
  const license = declaredLicense(entry.license ?? entry.licenses);
22
23
  return [{ at: pointer(['packages', path]), name, ...(version === undefined ? {} : { version }),
23
- ...(license === undefined ? {} : { license }), dev: entry.dev === true }];
24
+ ...(license === undefined ? {} : { license }), groups: categories.filter(category => entry[category] === true) }];
24
25
  });
25
26
  if (!dependencies.length)
26
27
  return [];
@@ -1,7 +1,8 @@
1
- export declare const licenseListVersion: string;
2
- export declare const presets: readonly ['open-source', 'weak-copyleft', 'permissive'];
1
+ import type { LicenseKind } from '@repometry/core/internal/model/license-evidence.js';
2
+ import type { Decision } from './expression.js';
3
+ export { licenseListVersion } from './spdx-catalog.js';
4
+ export declare const presets: readonly ['open-source', 'weak-copyleft', 'permissive', 'osi-approved'];
3
5
  type Preset = typeof presets[number];
4
- type Decision = 'allowed' | 'denied' | 'unknown' | 'unclassified';
5
6
  interface Exception {
6
7
  readonly package: string;
7
8
  readonly reason: string;
@@ -11,21 +12,19 @@ export interface PolicyOptions {
11
12
  readonly allow?: readonly string[];
12
13
  readonly deny?: readonly string[];
13
14
  readonly exceptions?: readonly Exception[];
14
- readonly dev?: boolean;
15
15
  }
16
16
  interface Policy {
17
17
  readonly preset: Preset;
18
18
  readonly allow: ReadonlySet<string>;
19
19
  readonly deny: ReadonlySet<string>;
20
20
  readonly exceptions: ReadonlyMap<string, string>;
21
- readonly dev: boolean;
22
21
  }
23
22
  export declare const policySchema: {
24
23
  readonly type: 'object';
25
24
  readonly additionalProperties: false;
26
25
  readonly properties: {
27
26
  readonly policy: {
28
- readonly enum: readonly ["open-source", "weak-copyleft", "permissive"];
27
+ readonly enum: readonly ["open-source", "weak-copyleft", "permissive", "osi-approved"];
29
28
  };
30
29
  readonly allow: {
31
30
  readonly type: 'array';
@@ -61,12 +60,9 @@ export declare const policySchema: {
61
60
  };
62
61
  };
63
62
  };
64
- readonly dev: {
65
- readonly type: 'boolean';
66
- };
67
63
  };
68
64
  };
69
- export declare function licensePolicy({ policy: preset, allow, deny, exceptions, dev }?: PolicyOptions): Policy;
65
+ export declare function licensePolicy({ policy: preset, allow, deny, exceptions }?: PolicyOptions): Policy;
70
66
  export declare function declaredLicense(value: unknown): string | undefined;
67
+ export declare function licenseKind(expression: string | undefined): LicenseKind;
71
68
  export declare function licenseDecision(expression: string | undefined, policy: Policy): Decision;
72
- export {};