@trustify-da/trustify-da-javascript-client 0.3.0-ea.6549d2a → 0.3.0-ea.68905d3
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.
- package/README.md +368 -14
- package/dist/package.json +39 -26
- package/dist/src/analysis.d.ts +28 -5
- package/dist/src/analysis.js +78 -13
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +287 -5
- package/dist/src/cyclone_dx_sbom.d.ts +27 -1
- package/dist/src/cyclone_dx_sbom.js +84 -10
- package/dist/src/index.d.ts +145 -8
- package/dist/src/index.js +362 -28
- package/dist/src/license/index.d.ts +5 -5
- package/dist/src/license/index.js +8 -5
- package/dist/src/license/license_utils.d.ts +40 -0
- package/dist/src/license/license_utils.js +137 -0
- package/dist/src/license/licenses_api.d.ts +2 -2
- package/dist/src/license/licenses_api.js +9 -2
- package/dist/src/license/project_license.d.ts +1 -6
- package/dist/src/license/project_license.js +4 -81
- package/dist/src/oci_image/images.d.ts +1 -1
- package/dist/src/oci_image/images.js +15 -1
- package/dist/src/oci_image/utils.d.ts +5 -5
- package/dist/src/oci_image/utils.js +11 -2
- package/dist/src/package_version.d.ts +8 -0
- package/dist/src/package_version.js +31 -0
- package/dist/src/provider.d.ts +8 -4
- package/dist/src/provider.js +18 -5
- package/dist/src/providers/base_java.d.ts +67 -12
- package/dist/src/providers/base_java.js +82 -61
- package/dist/src/providers/base_javascript.d.ts +91 -10
- package/dist/src/providers/base_javascript.js +220 -28
- package/dist/src/providers/base_pyproject.d.ts +158 -0
- package/dist/src/providers/base_pyproject.js +322 -0
- package/dist/src/providers/containerfile_parser.d.ts +5 -0
- package/dist/src/providers/containerfile_parser.js +20 -0
- package/dist/src/providers/golang_gomodules.d.ts +23 -13
- package/dist/src/providers/golang_gomodules.js +210 -121
- package/dist/src/providers/gomod_parser.d.ts +4 -0
- package/dist/src/providers/gomod_parser.js +16 -0
- package/dist/src/providers/java_gradle.d.ts +67 -0
- package/dist/src/providers/java_gradle.js +314 -21
- package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
- package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
- package/dist/src/providers/java_maven.d.ts +38 -10
- package/dist/src/providers/java_maven.js +279 -20
- package/dist/src/providers/javascript_bun.d.ts +22 -0
- package/dist/src/providers/javascript_bun.js +141 -0
- package/dist/src/providers/javascript_npm.d.ts +19 -0
- package/dist/src/providers/javascript_npm.js +61 -1
- package/dist/src/providers/javascript_pnpm.d.ts +14 -1
- package/dist/src/providers/javascript_pnpm.js +55 -5
- package/dist/src/providers/javascript_yarn.d.ts +12 -0
- package/dist/src/providers/javascript_yarn.js +75 -2
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +28 -7
- package/dist/src/providers/marker_evaluator.d.ts +14 -0
- package/dist/src/providers/marker_evaluator.js +191 -0
- package/dist/src/providers/oci_dockerfile.d.ts +51 -0
- package/dist/src/providers/oci_dockerfile.js +177 -0
- package/dist/src/providers/processors/yarn_berry_processor.d.ts +6 -2
- package/dist/src/providers/processors/yarn_berry_processor.js +91 -7
- package/dist/src/providers/processors/yarn_classic_processor.d.ts +6 -2
- package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
- package/dist/src/providers/python_controller.d.ts +5 -1
- package/dist/src/providers/python_controller.js +69 -8
- package/dist/src/providers/python_pip.d.ts +5 -0
- package/dist/src/providers/python_pip.js +8 -7
- package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
- package/dist/src/providers/python_pip_pyproject.js +146 -0
- package/dist/src/providers/python_poetry.d.ts +98 -0
- package/dist/src/providers/python_poetry.js +286 -0
- package/dist/src/providers/python_uv.d.ts +46 -0
- package/dist/src/providers/python_uv.js +269 -0
- package/dist/src/providers/requirements_parser.js +5 -4
- package/dist/src/providers/rust_cargo.d.ts +56 -0
- package/dist/src/providers/rust_cargo.js +682 -0
- package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
- package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
- package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
- package/dist/src/remediate.d.ts +117 -0
- package/dist/src/remediate.js +249 -0
- package/dist/src/remediation.d.ts +57 -0
- package/dist/src/remediation.js +436 -0
- package/dist/src/remediation_report.d.ts +40 -0
- package/dist/src/remediation_report.js +159 -0
- package/dist/src/sbom.d.ts +25 -1
- package/dist/src/sbom.js +23 -2
- package/dist/src/tools.d.ts +28 -2
- package/dist/src/tools.js +82 -1
- package/dist/src/updaters/maven_updater.d.ts +58 -0
- package/dist/src/updaters/maven_updater.js +345 -0
- package/dist/src/updaters/toml_updater.d.ts +62 -0
- package/dist/src/updaters/toml_updater.js +276 -0
- package/dist/src/workspace.d.ts +70 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +40 -27
- package/dist/src/license/compatibility.d.ts +0 -18
- package/dist/src/license/compatibility.js +0 -45
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { parse as parseToml } from 'smol-toml';
|
|
2
|
+
/**
|
|
3
|
+
* One entry in {@link updateTomlVersions}' `applied` list: a version that was changed and where.
|
|
4
|
+
* `type` discriminates the edit site — `'ref'` for a `[libraries]` entry pointing at a shared
|
|
5
|
+
* `[versions]` alias via `version.ref` (then `versionRef` names that alias), `'inline'` for a
|
|
6
|
+
* version declared directly on the library. `alias` is the library's catalog key in both cases.
|
|
7
|
+
* @typedef {{
|
|
8
|
+
* groupId: string,
|
|
9
|
+
* artifactId: string,
|
|
10
|
+
* newVersion: string,
|
|
11
|
+
* oldVersion: string,
|
|
12
|
+
* type: ('ref'|'inline'),
|
|
13
|
+
* alias: string,
|
|
14
|
+
* versionRef?: string
|
|
15
|
+
* }} TomlAppliedChange
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Stable edit-site key for a TOML (Gradle version catalog) change: same key => same commit/PR.
|
|
19
|
+
* Deps sharing one `[versions]` alias via `version.ref` share the ref key and are inseparable.
|
|
20
|
+
* @param {string} manifestPath
|
|
21
|
+
* @param {TomlAppliedChange} applied
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
export function tomlChangeKey(manifestPath, applied) {
|
|
25
|
+
return applied.type === 'ref'
|
|
26
|
+
? `toml:ref:${manifestPath}:${applied.versionRef}`
|
|
27
|
+
: `toml:inline:${manifestPath}:${applied.alias}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Updates dependency versions in a Gradle version catalog (libs.versions.toml) file.
|
|
31
|
+
*
|
|
32
|
+
* Supports two version declaration patterns:
|
|
33
|
+
* - Centralized: [versions] section defines version aliases, [libraries] references via version.ref
|
|
34
|
+
* - Inline: [libraries] entries include version = "x.y.z" directly
|
|
35
|
+
*
|
|
36
|
+
* Uses smol-toml parse() to locate keys, then performs position-based string replacement
|
|
37
|
+
* on the raw content to preserve formatting and comments.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} tomlContent - raw TOML file content
|
|
40
|
+
* @param {import('../remediate.js').VersionChangeRequest[]} versionChanges
|
|
41
|
+
* @returns {{content: string, applied: TomlAppliedChange[], skipped: Array<{groupId: string, artifactId: string, newVersion: string, reason: string}>}}
|
|
42
|
+
*/
|
|
43
|
+
export function updateTomlVersions(tomlContent, versionChanges) {
|
|
44
|
+
const applied = [];
|
|
45
|
+
const skipped = [];
|
|
46
|
+
if (!versionChanges || versionChanges.length === 0) {
|
|
47
|
+
return { content: tomlContent, applied, skipped };
|
|
48
|
+
}
|
|
49
|
+
let parsed;
|
|
50
|
+
try {
|
|
51
|
+
parsed = parseToml(tomlContent);
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
for (const change of versionChanges) {
|
|
55
|
+
skipped.push({
|
|
56
|
+
groupId: change.groupId,
|
|
57
|
+
artifactId: change.artifactId,
|
|
58
|
+
newVersion: change.newVersion,
|
|
59
|
+
reason: `Failed to parse TOML: ${err.message}`
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return { content: tomlContent, applied, skipped };
|
|
63
|
+
}
|
|
64
|
+
const libraries = parsed.libraries || {};
|
|
65
|
+
const versions = parsed.versions || {};
|
|
66
|
+
const libraryIndex = buildLibraryIndex(libraries);
|
|
67
|
+
let updatedContent = tomlContent;
|
|
68
|
+
for (const change of versionChanges) {
|
|
69
|
+
const moduleKey = `${change.groupId}:${change.artifactId}`;
|
|
70
|
+
const alias = libraryIndex.get(moduleKey);
|
|
71
|
+
if (!alias) {
|
|
72
|
+
skipped.push({
|
|
73
|
+
groupId: change.groupId,
|
|
74
|
+
artifactId: change.artifactId,
|
|
75
|
+
newVersion: change.newVersion,
|
|
76
|
+
reason: `No library entry found for module ${moduleKey}`
|
|
77
|
+
});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const libEntry = libraries[alias];
|
|
81
|
+
const versionRef = getVersionRef(libEntry);
|
|
82
|
+
if (versionRef) {
|
|
83
|
+
const oldVersion = versions[versionRef];
|
|
84
|
+
if (oldVersion === undefined) {
|
|
85
|
+
skipped.push({
|
|
86
|
+
groupId: change.groupId,
|
|
87
|
+
artifactId: change.artifactId,
|
|
88
|
+
newVersion: change.newVersion,
|
|
89
|
+
reason: `Version ref "${versionRef}" not found in [versions] section`
|
|
90
|
+
});
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (oldVersion === change.newVersion) {
|
|
94
|
+
skipped.push({
|
|
95
|
+
groupId: change.groupId,
|
|
96
|
+
artifactId: change.artifactId,
|
|
97
|
+
newVersion: change.newVersion,
|
|
98
|
+
reason: `Version already at ${change.newVersion}`
|
|
99
|
+
});
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
updatedContent = replaceVersionInSection(updatedContent, versionRef, oldVersion, change.newVersion);
|
|
103
|
+
applied.push({
|
|
104
|
+
groupId: change.groupId,
|
|
105
|
+
artifactId: change.artifactId,
|
|
106
|
+
newVersion: change.newVersion,
|
|
107
|
+
oldVersion,
|
|
108
|
+
type: 'ref',
|
|
109
|
+
versionRef,
|
|
110
|
+
alias
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
const inlineVersion = getInlineVersion(libEntry);
|
|
115
|
+
if (inlineVersion === undefined) {
|
|
116
|
+
skipped.push({
|
|
117
|
+
groupId: change.groupId,
|
|
118
|
+
artifactId: change.artifactId,
|
|
119
|
+
newVersion: change.newVersion,
|
|
120
|
+
reason: `No version.ref or inline version found for library "${alias}"`
|
|
121
|
+
});
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (inlineVersion === change.newVersion) {
|
|
125
|
+
skipped.push({
|
|
126
|
+
groupId: change.groupId,
|
|
127
|
+
artifactId: change.artifactId,
|
|
128
|
+
newVersion: change.newVersion,
|
|
129
|
+
reason: `Version already at ${change.newVersion}`
|
|
130
|
+
});
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const beforeInline = updatedContent;
|
|
134
|
+
updatedContent = replaceInlineVersion(updatedContent, alias, inlineVersion, change.newVersion);
|
|
135
|
+
if (updatedContent !== beforeInline) {
|
|
136
|
+
applied.push({
|
|
137
|
+
groupId: change.groupId,
|
|
138
|
+
artifactId: change.artifactId,
|
|
139
|
+
newVersion: change.newVersion,
|
|
140
|
+
oldVersion: inlineVersion,
|
|
141
|
+
type: 'inline',
|
|
142
|
+
alias
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
skipped.push({
|
|
147
|
+
groupId: change.groupId,
|
|
148
|
+
artifactId: change.artifactId,
|
|
149
|
+
newVersion: change.newVersion,
|
|
150
|
+
reason: `Inline version replacement did not match for library "${alias}"`
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return { content: updatedContent, applied, skipped };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Builds a map from "groupId:artifactId" to the TOML library alias.
|
|
159
|
+
* @param {object} libraries - parsed [libraries] section
|
|
160
|
+
* @returns {Map<string, string>}
|
|
161
|
+
*/
|
|
162
|
+
function buildLibraryIndex(libraries) {
|
|
163
|
+
const index = new Map();
|
|
164
|
+
for (const [alias, entry] of Object.entries(libraries)) {
|
|
165
|
+
const module = getModule(entry);
|
|
166
|
+
if (module) {
|
|
167
|
+
index.set(module, alias);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return index;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Extracts the module identifier from a library entry.
|
|
174
|
+
* Handles both string shorthand ("group:artifact:version") and object notation
|
|
175
|
+
* ({ module = "group:artifact" } or { group = "...", name = "..." }).
|
|
176
|
+
* @param {string|object} entry
|
|
177
|
+
* @returns {string|undefined} "groupId:artifactId"
|
|
178
|
+
*/
|
|
179
|
+
function getModule(entry) {
|
|
180
|
+
if (typeof entry === 'string') {
|
|
181
|
+
const parts = entry.split(':');
|
|
182
|
+
if (parts.length >= 2) {
|
|
183
|
+
return `${parts[0]}:${parts[1]}`;
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
if (entry.module) {
|
|
188
|
+
const parts = entry.module.split(':');
|
|
189
|
+
if (parts.length >= 2) {
|
|
190
|
+
return `${parts[0]}:${parts[1]}`;
|
|
191
|
+
}
|
|
192
|
+
return entry.module;
|
|
193
|
+
}
|
|
194
|
+
if (entry.group && entry.name) {
|
|
195
|
+
return `${entry.group}:${entry.name}`;
|
|
196
|
+
}
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Extracts the version.ref from a library entry, if present.
|
|
201
|
+
* @param {string|object} entry
|
|
202
|
+
* @returns {string|undefined}
|
|
203
|
+
*/
|
|
204
|
+
function getVersionRef(entry) {
|
|
205
|
+
if (typeof entry === 'object' && entry.version) {
|
|
206
|
+
if (typeof entry.version === 'object' && entry.version.ref) {
|
|
207
|
+
return entry.version.ref;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Extracts an inline version string from a library entry.
|
|
214
|
+
* Handles { version = "1.2.3" } and string shorthand "group:artifact:version".
|
|
215
|
+
* @param {string|object} entry
|
|
216
|
+
* @returns {string|undefined}
|
|
217
|
+
*/
|
|
218
|
+
function getInlineVersion(entry) {
|
|
219
|
+
if (typeof entry === 'string') {
|
|
220
|
+
const parts = entry.split(':');
|
|
221
|
+
if (parts.length >= 3) {
|
|
222
|
+
return parts[2];
|
|
223
|
+
}
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
if (typeof entry === 'object' && entry.version) {
|
|
227
|
+
if (typeof entry.version === 'string') {
|
|
228
|
+
return entry.version;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Replaces a version value using position-based string replacement.
|
|
235
|
+
* Targets lines matching: key = "oldVersion"
|
|
236
|
+
* @param {string} content - raw TOML content
|
|
237
|
+
* @param {string} key - version alias key
|
|
238
|
+
* @param {string} oldVersion - current version string
|
|
239
|
+
* @param {string} newVersion - replacement version string
|
|
240
|
+
* @returns {string} updated content
|
|
241
|
+
*/
|
|
242
|
+
function replaceVersionInSection(content, key, oldVersion, newVersion) {
|
|
243
|
+
const escapedKey = escapeRegExp(key);
|
|
244
|
+
const escapedOld = escapeRegExp(oldVersion);
|
|
245
|
+
const pattern = new RegExp(`^(\\s*${escapedKey}\\s*=\\s*")${escapedOld}("\\s*)$`, 'm');
|
|
246
|
+
return content.replace(pattern, (_, p1, p2) => `${p1}${newVersion}${p2}`);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Replaces an inline version in a library entry.
|
|
250
|
+
* Targets patterns like: version = "oldVersion" or version ="oldVersion"
|
|
251
|
+
* on the line containing the library alias.
|
|
252
|
+
* @param {string} content - raw TOML content
|
|
253
|
+
* @param {string} alias - library alias
|
|
254
|
+
* @param {string} oldVersion - current version string
|
|
255
|
+
* @param {string} newVersion - replacement version string
|
|
256
|
+
* @returns {string} updated content
|
|
257
|
+
*/
|
|
258
|
+
function replaceInlineVersion(content, alias, oldVersion, newVersion) {
|
|
259
|
+
const escapedAlias = escapeRegExp(alias);
|
|
260
|
+
const escapedOld = escapeRegExp(oldVersion);
|
|
261
|
+
const pattern = new RegExp(`^(\\s*${escapedAlias}\\s*=\\s*\\{[^}]*version\\s*=\\s*")${escapedOld}("[^}]*\\}\\s*)$`, 'm');
|
|
262
|
+
const result = content.replace(pattern, (_, p1, p2) => `${p1}${newVersion}${p2}`);
|
|
263
|
+
if (result !== content) {
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
const stringPattern = new RegExp(`^(\\s*${escapedAlias}\\s*=\\s*"[^:]+:[^:]+:)${escapedOld}("\\s*)$`, 'm');
|
|
267
|
+
return content.replace(stringPattern, (_, p1, p2) => `${p1}${newVersion}${p2}`);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Escapes special regex characters in a string.
|
|
271
|
+
* @param {string} str
|
|
272
|
+
* @returns {string}
|
|
273
|
+
*/
|
|
274
|
+
function escapeRegExp(str) {
|
|
275
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
276
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve ignore globs for workspace discovery: defaults + `TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE` + `opts.workspaceDiscoveryIgnore`.
|
|
3
|
+
* Patterns are fast-glob / micromatch style, relative to the workspace root (forward slashes).
|
|
4
|
+
*
|
|
5
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
|
|
6
|
+
* @returns {string[]}
|
|
7
|
+
*/
|
|
8
|
+
export function resolveWorkspaceDiscoveryIgnore(opts?: {
|
|
9
|
+
workspaceDiscoveryIgnore?: string[];
|
|
10
|
+
TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}): string[];
|
|
13
|
+
/**
|
|
14
|
+
* Drop manifest paths whose location matches an ignore pattern (e.g. root unshift, Cargo paths).
|
|
15
|
+
*
|
|
16
|
+
* @param {string[]} manifestPaths
|
|
17
|
+
* @param {string} root
|
|
18
|
+
* @param {string[]} ignorePatterns
|
|
19
|
+
* @returns {string[]}
|
|
20
|
+
*/
|
|
21
|
+
export function filterManifestPathsByDiscoveryIgnore(manifestPaths: string[], root: string, ignorePatterns: string[]): string[];
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {{ valid: true, name: string, version: string } | { valid: false, error: string }} ValidatePackageJsonResult
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Validate a package.json has non-empty `name` and `version` (required for stable SBOM root identity in batch).
|
|
27
|
+
*
|
|
28
|
+
* @param {string} packageJsonPath - Absolute or relative path to package.json
|
|
29
|
+
* @returns {ValidatePackageJsonResult}
|
|
30
|
+
*/
|
|
31
|
+
export function validatePackageJson(packageJsonPath: string): ValidatePackageJsonResult;
|
|
32
|
+
/**
|
|
33
|
+
* Discover all package.json paths in a JS/TS workspace.
|
|
34
|
+
* Reads pnpm-workspace.yaml or package.json workspaces.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root
|
|
37
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}] - optional `workspaceDiscoveryIgnore` globs (merged with defaults and env)
|
|
38
|
+
* @returns {Promise<string[]>} Paths to package.json files (absolute)
|
|
39
|
+
*/
|
|
40
|
+
export function discoverWorkspacePackages(workspaceRoot: string, opts?: {
|
|
41
|
+
workspaceDiscoveryIgnore?: string[];
|
|
42
|
+
TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string;
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}): Promise<string[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Convert workspace glob patterns to manifest-file glob patterns,
|
|
47
|
+
* correctly handling negation prefixes.
|
|
48
|
+
*
|
|
49
|
+
* @param {string[]} patterns - Workspace glob patterns (may include negations)
|
|
50
|
+
* @param {string} manifestFileName - e.g. 'package.json' or 'Cargo.toml'
|
|
51
|
+
* @returns {string[]}
|
|
52
|
+
*/
|
|
53
|
+
export function toManifestGlobPatterns(patterns: string[], manifestFileName: string): string[];
|
|
54
|
+
/**
|
|
55
|
+
* Discover all Cargo.toml manifest paths in a Cargo workspace.
|
|
56
|
+
* Uses `cargo metadata` to get workspace members.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain Cargo.toml and Cargo.lock)
|
|
59
|
+
* @param {import('./index.js').Options} [opts={}]
|
|
60
|
+
* @returns {Promise<string[]>} Paths to Cargo.toml files (absolute)
|
|
61
|
+
*/
|
|
62
|
+
export function discoverWorkspaceCrates(workspaceRoot: string, opts?: import("./index.js").Options): Promise<string[]>;
|
|
63
|
+
export type ValidatePackageJsonResult = {
|
|
64
|
+
valid: true;
|
|
65
|
+
name: string;
|
|
66
|
+
version: string;
|
|
67
|
+
} | {
|
|
68
|
+
valid: false;
|
|
69
|
+
error: string;
|
|
70
|
+
};
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import fg from 'fast-glob';
|
|
4
|
+
import { load as yamlLoad } from 'js-yaml';
|
|
5
|
+
import micromatch from 'micromatch';
|
|
6
|
+
import { getCustom, getCustomPath, invokeCommand } from './tools.js';
|
|
7
|
+
/** Default paths skipped during JS workspace discovery (merged with user patterns). */
|
|
8
|
+
const DEFAULT_WORKSPACE_DISCOVERY_IGNORE = [
|
|
9
|
+
'**/node_modules/**',
|
|
10
|
+
'**/.git/**',
|
|
11
|
+
];
|
|
12
|
+
/**
|
|
13
|
+
* Resolve ignore globs for workspace discovery: defaults + `TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE` + `opts.workspaceDiscoveryIgnore`.
|
|
14
|
+
* Patterns are fast-glob / micromatch style, relative to the workspace root (forward slashes).
|
|
15
|
+
*
|
|
16
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
|
|
17
|
+
* @returns {string[]}
|
|
18
|
+
*/
|
|
19
|
+
export function resolveWorkspaceDiscoveryIgnore(opts = {}) {
|
|
20
|
+
const merged = [...DEFAULT_WORKSPACE_DISCOVERY_IGNORE];
|
|
21
|
+
const fromEnv = getCustom('TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE', null, opts);
|
|
22
|
+
if (fromEnv && String(fromEnv).trim()) {
|
|
23
|
+
merged.push(...String(fromEnv).split(',').map(s => s.trim()).filter(Boolean));
|
|
24
|
+
}
|
|
25
|
+
const extra = opts.workspaceDiscoveryIgnore;
|
|
26
|
+
if (Array.isArray(extra)) {
|
|
27
|
+
for (const p of extra) {
|
|
28
|
+
if (typeof p === 'string' && p.trim()) {
|
|
29
|
+
merged.push(p.trim());
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return [...new Set(merged)];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} root - Workspace root (absolute)
|
|
37
|
+
* @param {string[]} ignorePatterns
|
|
38
|
+
*/
|
|
39
|
+
function buildWorkspaceDiscoveryGlobOptions(root, ignorePatterns) {
|
|
40
|
+
return {
|
|
41
|
+
cwd: root,
|
|
42
|
+
absolute: true,
|
|
43
|
+
onlyFiles: true,
|
|
44
|
+
ignore: ignorePatterns,
|
|
45
|
+
followSymbolicLinks: false,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @param {string} filePath - Absolute path
|
|
50
|
+
* @param {string} root - Workspace root (absolute)
|
|
51
|
+
* @returns {string} Relative path with forward slashes
|
|
52
|
+
*/
|
|
53
|
+
function relativePathForGlobMatch(filePath, root) {
|
|
54
|
+
const rel = path.relative(root, filePath);
|
|
55
|
+
return rel.split(path.sep).join('/');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Drop manifest paths whose location matches an ignore pattern (e.g. root unshift, Cargo paths).
|
|
59
|
+
*
|
|
60
|
+
* @param {string[]} manifestPaths
|
|
61
|
+
* @param {string} root
|
|
62
|
+
* @param {string[]} ignorePatterns
|
|
63
|
+
* @returns {string[]}
|
|
64
|
+
*/
|
|
65
|
+
export function filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns) {
|
|
66
|
+
if (!ignorePatterns.length) {
|
|
67
|
+
return manifestPaths;
|
|
68
|
+
}
|
|
69
|
+
const resolvedRoot = path.resolve(root);
|
|
70
|
+
return manifestPaths.filter(absPath => {
|
|
71
|
+
const rel = relativePathForGlobMatch(absPath, resolvedRoot);
|
|
72
|
+
if (rel === '') {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return !ignorePatterns.some(pattern => micromatch.isMatch(rel, pattern, { dot: true }));
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @typedef {{ valid: true, name: string, version: string } | { valid: false, error: string }} ValidatePackageJsonResult
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Validate a package.json has non-empty `name` and `version` (required for stable SBOM root identity in batch).
|
|
83
|
+
*
|
|
84
|
+
* @param {string} packageJsonPath - Absolute or relative path to package.json
|
|
85
|
+
* @returns {ValidatePackageJsonResult}
|
|
86
|
+
*/
|
|
87
|
+
export function validatePackageJson(packageJsonPath) {
|
|
88
|
+
let content;
|
|
89
|
+
try {
|
|
90
|
+
const raw = fs.readFileSync(packageJsonPath, 'utf-8');
|
|
91
|
+
content = JSON.parse(raw);
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
95
|
+
return { valid: false, error: `Invalid package.json: ${msg}` };
|
|
96
|
+
}
|
|
97
|
+
if (!content || typeof content !== 'object') {
|
|
98
|
+
return { valid: false, error: 'package.json must be a JSON object' };
|
|
99
|
+
}
|
|
100
|
+
const name = content.name;
|
|
101
|
+
const version = content.version;
|
|
102
|
+
if (typeof name !== 'string' || !name.trim()) {
|
|
103
|
+
return { valid: false, error: 'Missing or invalid name' };
|
|
104
|
+
}
|
|
105
|
+
if (typeof version !== 'string' || !version.trim()) {
|
|
106
|
+
return { valid: false, error: 'Missing or invalid version' };
|
|
107
|
+
}
|
|
108
|
+
return { valid: true, name: name.trim(), version: version.trim() };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Discover all package.json paths in a JS/TS workspace.
|
|
112
|
+
* Reads pnpm-workspace.yaml or package.json workspaces.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root
|
|
115
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}] - optional `workspaceDiscoveryIgnore` globs (merged with defaults and env)
|
|
116
|
+
* @returns {Promise<string[]>} Paths to package.json files (absolute)
|
|
117
|
+
*/
|
|
118
|
+
export async function discoverWorkspacePackages(workspaceRoot, opts = {}) {
|
|
119
|
+
const root = path.resolve(workspaceRoot);
|
|
120
|
+
const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts);
|
|
121
|
+
const globOpts = buildWorkspaceDiscoveryGlobOptions(root, ignorePatterns);
|
|
122
|
+
const pnpmWorkspace = path.join(root, 'pnpm-workspace.yaml');
|
|
123
|
+
const packageJson = path.join(root, 'package.json');
|
|
124
|
+
if (fs.existsSync(pnpmWorkspace)) {
|
|
125
|
+
return discoverFromPnpmWorkspace(root, pnpmWorkspace, globOpts, ignorePatterns);
|
|
126
|
+
}
|
|
127
|
+
if (fs.existsSync(packageJson)) {
|
|
128
|
+
return discoverFromPackageJsonWorkspaces(root, packageJson, globOpts, ignorePatterns);
|
|
129
|
+
}
|
|
130
|
+
return [];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* @param {string} root
|
|
134
|
+
* @param {string} pnpmWorkspacePath
|
|
135
|
+
* @param {object} globOpts - fast-glob options (cwd, ignore, followSymbolicLinks, …)
|
|
136
|
+
* @param {string[]} ignorePatterns - for post-filter
|
|
137
|
+
* @returns {Promise<string[]>}
|
|
138
|
+
*/
|
|
139
|
+
async function discoverFromPnpmWorkspace(root, pnpmWorkspacePath, globOpts, ignorePatterns) {
|
|
140
|
+
const content = fs.readFileSync(pnpmWorkspacePath, 'utf-8');
|
|
141
|
+
const packages = parsePnpmPackages(content);
|
|
142
|
+
if (packages.length === 0) {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
const patterns = toManifestGlobPatterns(packages, 'package.json');
|
|
146
|
+
const manifestPaths = await fg(patterns, globOpts);
|
|
147
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Parse the `packages` array from pnpm-workspace.yaml content.
|
|
151
|
+
* @param {string} content - Raw YAML content
|
|
152
|
+
* @returns {string[]}
|
|
153
|
+
*/
|
|
154
|
+
function parsePnpmPackages(content) {
|
|
155
|
+
let doc;
|
|
156
|
+
try {
|
|
157
|
+
doc = yamlLoad(content);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return [];
|
|
161
|
+
}
|
|
162
|
+
if (!doc || typeof doc !== 'object' || !Array.isArray(doc.packages)) {
|
|
163
|
+
return [];
|
|
164
|
+
}
|
|
165
|
+
return doc.packages.filter(p => typeof p === 'string' && p.trim()).map(p => p.trim());
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Convert workspace glob patterns to manifest-file glob patterns,
|
|
169
|
+
* correctly handling negation prefixes.
|
|
170
|
+
*
|
|
171
|
+
* @param {string[]} patterns - Workspace glob patterns (may include negations)
|
|
172
|
+
* @param {string} manifestFileName - e.g. 'package.json' or 'Cargo.toml'
|
|
173
|
+
* @returns {string[]}
|
|
174
|
+
*/
|
|
175
|
+
export function toManifestGlobPatterns(patterns, manifestFileName) {
|
|
176
|
+
return patterns.map(p => {
|
|
177
|
+
if (p.startsWith('!')) {
|
|
178
|
+
return `!${p.slice(1)}/${manifestFileName}`;
|
|
179
|
+
}
|
|
180
|
+
return `${p}/${manifestFileName}`;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @param {string} root
|
|
185
|
+
* @param {string} packageJsonPath
|
|
186
|
+
* @param {object} globOpts
|
|
187
|
+
* @param {string[]} ignorePatterns
|
|
188
|
+
* @returns {Promise<string[]>}
|
|
189
|
+
*/
|
|
190
|
+
async function discoverFromPackageJsonWorkspaces(root, packageJsonPath, globOpts, ignorePatterns) {
|
|
191
|
+
let pkg;
|
|
192
|
+
try {
|
|
193
|
+
pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return [];
|
|
197
|
+
}
|
|
198
|
+
const workspaces = pkg.workspaces;
|
|
199
|
+
if (!workspaces) {
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
const raw = Array.isArray(workspaces) ? workspaces : workspaces.packages || [];
|
|
203
|
+
const patterns = toManifestGlobPatterns(raw.filter(p => typeof p === 'string'), 'package.json');
|
|
204
|
+
if (patterns.length === 0) {
|
|
205
|
+
return [];
|
|
206
|
+
}
|
|
207
|
+
const manifestPaths = await fg(patterns, globOpts);
|
|
208
|
+
const rootPkg = path.join(root, 'package.json');
|
|
209
|
+
if (fs.existsSync(rootPkg) && !manifestPaths.includes(rootPkg)) {
|
|
210
|
+
manifestPaths.unshift(rootPkg);
|
|
211
|
+
}
|
|
212
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Discover all Cargo.toml manifest paths in a Cargo workspace.
|
|
216
|
+
* Uses `cargo metadata` to get workspace members.
|
|
217
|
+
*
|
|
218
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain Cargo.toml and Cargo.lock)
|
|
219
|
+
* @param {import('./index.js').Options} [opts={}]
|
|
220
|
+
* @returns {Promise<string[]>} Paths to Cargo.toml files (absolute)
|
|
221
|
+
*/
|
|
222
|
+
export async function discoverWorkspaceCrates(workspaceRoot, opts = {}) {
|
|
223
|
+
const root = path.resolve(workspaceRoot);
|
|
224
|
+
const cargoToml = path.join(root, 'Cargo.toml');
|
|
225
|
+
const cargoLock = path.join(root, 'Cargo.lock');
|
|
226
|
+
if (!fs.existsSync(cargoToml) || !fs.existsSync(cargoLock)) {
|
|
227
|
+
return [];
|
|
228
|
+
}
|
|
229
|
+
const cargoBin = getCustomPath('cargo', opts);
|
|
230
|
+
let output;
|
|
231
|
+
try {
|
|
232
|
+
output = invokeCommand(cargoBin, ['metadata', '--format-version', '1', '--no-deps'], { cwd: root });
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return [];
|
|
236
|
+
}
|
|
237
|
+
let metadata;
|
|
238
|
+
try {
|
|
239
|
+
metadata = JSON.parse(output.toString().trim());
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
return [];
|
|
243
|
+
}
|
|
244
|
+
const memberIds = new Set(metadata.workspace_members || []);
|
|
245
|
+
const manifestPaths = [];
|
|
246
|
+
for (const pkg of metadata.packages || []) {
|
|
247
|
+
if (memberIds.has(pkg.id) && pkg.manifest_path) {
|
|
248
|
+
const manifestPath = path.resolve(pkg.manifest_path);
|
|
249
|
+
if (fs.existsSync(manifestPath)) {
|
|
250
|
+
manifestPaths.push(manifestPath);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts);
|
|
255
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
256
|
+
}
|