@trustify-da/trustify-da-javascript-client 0.3.0-ea.0e9ba23 → 0.3.0-ea.1d590b2

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 (66) hide show
  1. package/README.md +179 -11
  2. package/dist/package.json +12 -3
  3. package/dist/src/analysis.d.ts +16 -0
  4. package/dist/src/analysis.js +53 -4
  5. package/dist/src/batch_opts.d.ts +24 -0
  6. package/dist/src/batch_opts.js +35 -0
  7. package/dist/src/cli.js +171 -4
  8. package/dist/src/cyclone_dx_sbom.d.ts +14 -1
  9. package/dist/src/cyclone_dx_sbom.js +34 -6
  10. package/dist/src/index.d.ts +132 -2
  11. package/dist/src/index.js +334 -5
  12. package/dist/src/license/index.d.ts +2 -2
  13. package/dist/src/license/index.js +4 -4
  14. package/dist/src/license/license_utils.d.ts +40 -0
  15. package/dist/src/license/license_utils.js +134 -0
  16. package/dist/src/license/licenses_api.js +9 -2
  17. package/dist/src/license/project_license.d.ts +1 -6
  18. package/dist/src/license/project_license.js +4 -81
  19. package/dist/src/oci_image/utils.js +11 -2
  20. package/dist/src/provider.d.ts +6 -3
  21. package/dist/src/provider.js +14 -5
  22. package/dist/src/providers/base_java.d.ts +0 -9
  23. package/dist/src/providers/base_java.js +2 -38
  24. package/dist/src/providers/base_javascript.d.ts +19 -3
  25. package/dist/src/providers/base_javascript.js +106 -23
  26. package/dist/src/providers/base_pyproject.d.ts +153 -0
  27. package/dist/src/providers/base_pyproject.js +315 -0
  28. package/dist/src/providers/golang_gomodules.d.ts +12 -12
  29. package/dist/src/providers/golang_gomodules.js +102 -112
  30. package/dist/src/providers/gomod_parser.d.ts +4 -0
  31. package/dist/src/providers/gomod_parser.js +16 -0
  32. package/dist/src/providers/java_gradle.d.ts +19 -0
  33. package/dist/src/providers/java_gradle.js +116 -1
  34. package/dist/src/providers/java_maven.d.ts +9 -1
  35. package/dist/src/providers/java_maven.js +103 -10
  36. package/dist/src/providers/javascript_npm.d.ts +1 -0
  37. package/dist/src/providers/javascript_npm.js +21 -0
  38. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  39. package/dist/src/providers/javascript_pnpm.js +8 -4
  40. package/dist/src/providers/manifest.d.ts +2 -0
  41. package/dist/src/providers/manifest.js +22 -4
  42. package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
  43. package/dist/src/providers/python_controller.d.ts +5 -1
  44. package/dist/src/providers/python_controller.js +8 -4
  45. package/dist/src/providers/python_pip.d.ts +4 -0
  46. package/dist/src/providers/python_pip.js +7 -6
  47. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  48. package/dist/src/providers/python_pip_pyproject.js +144 -0
  49. package/dist/src/providers/python_poetry.d.ts +58 -0
  50. package/dist/src/providers/python_poetry.js +175 -0
  51. package/dist/src/providers/python_uv.d.ts +42 -0
  52. package/dist/src/providers/python_uv.js +149 -0
  53. package/dist/src/providers/requirements_parser.js +5 -8
  54. package/dist/src/providers/rust_cargo.d.ts +52 -0
  55. package/dist/src/providers/rust_cargo.js +614 -0
  56. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  57. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  58. package/dist/src/sbom.d.ts +14 -1
  59. package/dist/src/sbom.js +13 -2
  60. package/dist/src/tools.d.ts +26 -0
  61. package/dist/src/tools.js +58 -0
  62. package/dist/src/workspace.d.ts +61 -0
  63. package/dist/src/workspace.js +256 -0
  64. package/package.json +13 -4
  65. package/dist/src/license/compatibility.d.ts +0 -18
  66. package/dist/src/license/compatibility.js +0 -45
@@ -1,9 +1,10 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import { EOL } from "os";
4
3
  import { PackageURL } from 'packageurl-js';
4
+ import { readLicenseFile } from '../license/license_utils.js';
5
5
  import Sbom from '../sbom.js';
6
6
  import { getCustom, getCustomPath, invokeCommand } from "../tools.js";
7
+ import { getParser, getRequireQuery } from './gomod_parser.js';
7
8
  export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
8
9
  /** @typedef {import('../provider').Provider} */
9
10
  /** @typedef {import('../provider').Provided} Provided */
@@ -16,46 +17,46 @@ export default { isSupported, validateLockFile, provideComponent, provideStack,
16
17
  const ecosystem = 'golang';
17
18
  const defaultMainModuleVersion = "v0.0.0";
18
19
  /**
19
- * @param {string} manifestName - the subject manifest name-type
20
- * @returns {boolean} - return true if `pom.xml` is the manifest name-type
20
+ * @param {string} manifestName the subject manifest name-type
21
+ * @returns {boolean} return true if `pom.xml` is the manifest name-type
21
22
  */
22
23
  function isSupported(manifestName) {
23
24
  return 'go.mod' === manifestName;
24
25
  }
25
26
  /**
26
27
  * Go modules have no standard license field in go.mod
27
- * @param {string} manifestPath - path to go.mod
28
+ * @param {string} manifestPath path to go.mod
28
29
  * @returns {string|null}
29
30
  */
30
31
  // eslint-disable-next-line no-unused-vars
31
- function readLicenseFromManifest(manifestPath) { return null; }
32
+ function readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
32
33
  /**
33
- * @param {string} manifestDir - the directory where the manifest lies
34
+ * @param {string} manifestDir the directory where the manifest lies
34
35
  */
35
36
  function validateLockFile() { return true; }
36
37
  /**
37
38
  * Provide content and content type for maven-maven stack analysis.
38
- * @param {string} manifest - the manifest path or name
39
- * @param {{}} [opts={}] - optional various options to pass along the application
40
- * @returns {Provided}
39
+ * @param {string} manifest the manifest path or name
40
+ * @param {{}} [opts={}] optional various options to pass along the application
41
+ * @returns {Promise<Provided>}
41
42
  */
42
- function provideStack(manifest, opts = {}) {
43
+ async function provideStack(manifest, opts = {}) {
43
44
  return {
44
45
  ecosystem,
45
- content: getSBOM(manifest, opts, true),
46
+ content: await getSBOM(manifest, opts, true),
46
47
  contentType: 'application/vnd.cyclonedx+json'
47
48
  };
48
49
  }
49
50
  /**
50
51
  * Provide content and content type for maven-maven component analysis.
51
- * @param {string} manifest - path to go.mod for component report
52
- * @param {{}} [opts={}] - optional various options to pass along the application
53
- * @returns {Provided}
52
+ * @param {string} manifest path to go.mod for component report
53
+ * @param {{}} [opts={}] optional various options to pass along the application
54
+ * @returns {Promise<Provided>}
54
55
  */
55
- function provideComponent(manifest, opts = {}) {
56
+ async function provideComponent(manifest, opts = {}) {
56
57
  return {
57
58
  ecosystem,
58
- content: getSBOM(manifest, opts, false),
59
+ content: await getSBOM(manifest, opts, false),
59
60
  contentType: 'application/vnd.cyclonedx+json'
60
61
  };
61
62
  }
@@ -76,50 +77,52 @@ function getChildVertexFromEdge(edge) {
76
77
  return edge.split(" ")[1];
77
78
  }
78
79
  /**
79
- *
80
- * @param line one row from go.mod file
81
- * @return {boolean} whether line from go.mod should be considered as ignored or not
80
+ * Check whether a require_spec has a valid exhortignore marker.
81
+ * For direct dependencies: `//exhortignore` or `// exhortignore`
82
+ * For indirect dependencies: `// indirect; exhortignore` (semicolon-separated)
83
+ * @param {import('web-tree-sitter').SyntaxNode} specNode
84
+ * @return {boolean}
82
85
  */
83
- function ignoredLine(line) {
84
- let result = false;
85
- if (line.match(".*exhortignore.*")) {
86
- if (line.match(".+//\\s*exhortignore") || line.match(".+//\\sindirect (//)?\\s*exhortignore")) {
87
- let trimmedRow = line.trim();
88
- if (!trimmedRow.startsWith("module ") && !trimmedRow.startsWith("go ") && !trimmedRow.startsWith("require (") && !trimmedRow.startsWith("require(")
89
- && !trimmedRow.startsWith("exclude ") && !trimmedRow.startsWith("replace ") && !trimmedRow.startsWith("retract ") && !trimmedRow.startsWith("use ")
90
- && !trimmedRow.includes("=>")) {
91
- if (trimmedRow.startsWith("require ") || trimmedRow.match("^[a-z.0-9/-]+\\s{1,2}[vV][0-9]\\.[0-9](\\.[0-9]){0,2}.*")) {
92
- result = true;
93
- }
94
- }
86
+ function hasExhortIgnore(specNode) {
87
+ // Ideally this would be the following tree-sitter query instead, but for some
88
+ // reason it throws an error here but not in the playground.
89
+ // (require_spec) ((module_path) @path (version) (comment) @comment (#match? @comment "^//.*exhortignore"))
90
+ // QueryError: Bad pattern structure at offset 53: '(comment) @comment (#match? @comment "^//.*exhortignore")) @spec'...
91
+ let comments = specNode.children.filter(c => c.type === 'comment');
92
+ for (let comment of comments) {
93
+ let text = comment.text;
94
+ if (/^\/\/\s*indirect;\s*exhortignore/.test(text)) {
95
+ return true;
96
+ }
97
+ if (/^\/\/\s*exhortignore/.test(text)) {
98
+ return true;
95
99
  }
96
100
  }
97
- return result;
98
- }
99
- /**
100
- * extract package name from go.mod line that contains exhortignore comment.
101
- * @param line a row contains exhortignore as part of a comment
102
- * @return {string} the full package name + group/namespace + version
103
- * @private
104
- */
105
- function extractPackageName(line) {
106
- let trimmedRow = line.trim();
107
- let firstRemarkNotationOccurrence = trimmedRow.indexOf("//");
108
- return trimmedRow.substring(0, firstRemarkNotationOccurrence).trim();
101
+ return false;
109
102
  }
110
103
  /**
111
104
  *
112
- * @param {string } manifest - path to manifest
113
- * @return {[PackageURL]} list of ignored dependencies d
105
+ * @param {string} manifestContent go.mod file contents
106
+ * @param {import('web-tree-sitter').Parser} parser
107
+ * @param {import('web-tree-sitter').Query} requireQuery
108
+ * @return {PackageURL[]} list of ignored dependencies
114
109
  */
115
- function getIgnoredDeps(manifest) {
116
- let goMod = fs.readFileSync(manifest).toString().trim();
117
- let lines = goMod.split(getLineSeparatorGolang());
118
- return lines.filter(line => ignoredLine(line)).map(line => extractPackageName(line)).map(dep => toPurl(dep, /[ ]{1,3}/));
110
+ function getIgnoredDeps(manifestContent, parser, requireQuery) {
111
+ let tree = parser.parse(manifestContent);
112
+ return requireQuery.matches(tree.rootNode)
113
+ .filter(match => {
114
+ let specNode = match.captures.find(c => c.name === 'spec').node;
115
+ return hasExhortIgnore(specNode);
116
+ })
117
+ .map(match => {
118
+ let name = match.captures.find(c => c.name === 'name').node.text;
119
+ let version = match.captures.find(c => c.name === 'version').node.text;
120
+ return toPurl(`${name} ${version}`, /[ ]{1,3}/);
121
+ });
119
122
  }
120
123
  /**
121
124
  *
122
- * @param {[PackageURL]}allIgnoredDeps - list of purls of all dependencies that should be ignored
125
+ * @param {PackageURL[]} allIgnoredDeps list of purls of all dependencies that should be ignored
123
126
  * @param {PackageURL} purl object to be checked if needed to be ignored
124
127
  * @return {boolean}
125
128
  */
@@ -138,59 +141,29 @@ function enforceRemovingIgnoredDepsInCaseOfAutomaticVersionUpdate(ignoredDeps, s
138
141
  }
139
142
  /**
140
143
  *
141
- * @param {[string]} lines - array of lines of go.mod manifest
142
- * @param {string} goMod - content of go.mod manifest
143
- * @return {[string]} all dependencies from go.mod file as array
144
+ * @param {string} manifestContent go.mod file contents
145
+ * @param {import('web-tree-sitter').Parser} parser
146
+ * @param {import('web-tree-sitter').Query} requireQuery
147
+ * @return {string[]} all dependencies from go.mod file as "name version" strings
144
148
  */
145
- function collectAllDepsFromManifest(lines, goMod) {
146
- let result;
147
- // collect all deps that starts with require keyword
148
- result = lines.filter((line) => line.trim().startsWith("require") && !line.includes("(")).map((dep) => dep.substring("require".length).trim());
149
- // collect all deps that are inside `require` blocks
150
- let currentSegmentOfGoMod = goMod;
151
- let requirePositionObject = decideRequireBlockIndex(currentSegmentOfGoMod);
152
- while (requirePositionObject.index > -1) {
153
- let depsInsideRequirementsBlock = currentSegmentOfGoMod.substring(requirePositionObject.index + requirePositionObject.startingOffeset).trim();
154
- let endOfBlockIndex = depsInsideRequirementsBlock.indexOf(")");
155
- let currentIndex = 0;
156
- while (currentIndex < endOfBlockIndex) {
157
- let endOfLinePosition = depsInsideRequirementsBlock.indexOf(EOL, currentIndex);
158
- let dependency = depsInsideRequirementsBlock.substring(currentIndex, endOfLinePosition);
159
- result.push(dependency.trim());
160
- currentIndex = endOfLinePosition + 1;
161
- }
162
- currentSegmentOfGoMod = currentSegmentOfGoMod.substring(endOfBlockIndex + 1).trim();
163
- requirePositionObject = decideRequireBlockIndex(currentSegmentOfGoMod);
164
- }
165
- function decideRequireBlockIndex(goMod) {
166
- let object = {};
167
- let index = goMod.indexOf("require(");
168
- object.startingOffeset = "require(".length;
169
- if (index === -1) {
170
- index = goMod.indexOf("require (");
171
- object.startingOffeset = "require (".length;
172
- if (index === -1) {
173
- index = goMod.indexOf("require (");
174
- object.startingOffeset = "require (".length;
175
- }
176
- }
177
- object.index = index;
178
- return object;
179
- }
180
- return result;
149
+ function collectAllDepsFromManifest(manifestContent, parser, requireQuery) {
150
+ let tree = parser.parse(manifestContent);
151
+ return requireQuery.matches(tree.rootNode).map(match => {
152
+ let name = match.captures.find(c => c.name === 'name').node.text;
153
+ let version = match.captures.find(c => c.name === 'version').node.text;
154
+ return `${name} ${version}`;
155
+ });
181
156
  }
182
157
  /**
183
158
  *
184
159
  * @param {string} rootElementName the rootElementName element of go mod graph, to compare only direct deps from go mod graph against go.mod manifest
185
- * @param{[string]} goModGraphOutputRows the goModGraphOutputRows from go mod graph' output
186
- * @param {string }manifest path to go.mod manifest on file system
160
+ * @param {string[]} goModGraphOutputRows the goModGraphOutputRows from go mod graph' output
161
+ * @param {string} manifestContent go.mod file contents
187
162
  * @private
188
163
  */
189
- function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, manifest) {
190
- let goMod = fs.readFileSync(manifest).toString().trim();
191
- let lines = goMod.split(getLineSeparatorGolang());
164
+ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, manifestContent, parser, requireQuery) {
192
165
  let comparisonLines = goModGraphOutputRows.filter((line) => line.startsWith(rootElementName)).map((line) => getChildVertexFromEdge(line));
193
- let manifestDeps = collectAllDepsFromManifest(lines, goMod);
166
+ let manifestDeps = collectAllDepsFromManifest(manifestContent, parser, requireQuery);
194
167
  try {
195
168
  comparisonLines.forEach((dependency) => {
196
169
  let parts = dependency.split("@");
@@ -202,7 +175,7 @@ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, man
202
175
  let currentVersion = components[1];
203
176
  if (currentDepName === depName) {
204
177
  if (currentVersion !== version) {
205
- throw new Error(`versions mismatch for dependency name ${depName}, manifest version=${currentVersion}, installed Version=${version}, if you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting - MATCH_MANIFEST_VERSIONS=false`);
178
+ throw new Error(`version mismatch for dependency "${depName}", manifest version=${currentVersion}, installed version=${version}, if you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting MATCH_MANIFEST_VERSIONS=false`);
206
179
  }
207
180
  }
208
181
  });
@@ -218,10 +191,10 @@ function performManifestVersionsCheck(rootElementName, goModGraphOutputRows, man
218
191
  * @param {string} manifest - path for go.mod
219
192
  * @param {{}} [opts={}] - optional various options to pass along the application
220
193
  * @param {boolean} includeTransitive - whether the sbom should contain transitive dependencies of the main module or not.
221
- * @returns {string} the SBOM json content
194
+ * @returns {Promise<string>} the SBOM json content
222
195
  * @private
223
196
  */
224
- function getSBOM(manifest, opts = {}, includeTransitive) {
197
+ async function getSBOM(manifest, opts = {}, includeTransitive) {
225
198
  // get custom goBin path
226
199
  let goBin = getCustomPath('go', opts);
227
200
  // verify goBin is accessible
@@ -247,14 +220,25 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
247
220
  catch (error) {
248
221
  throw new Error('failed to determine root module name', { cause: error });
249
222
  }
250
- let ignoredDeps = getIgnoredDeps(manifest);
223
+ let manifestContent = fs.readFileSync(manifest).toString();
224
+ let [parser, requireQuery] = await Promise.all([getParser(), getRequireQuery()]);
225
+ let ignoredDeps = getIgnoredDeps(manifestContent, parser, requireQuery);
251
226
  let allIgnoredDeps = ignoredDeps.map((dep) => dep.toString());
252
227
  let sbom = new Sbom();
253
228
  let rows = goGraphOutput.split(getLineSeparatorGolang()).filter(line => !line.includes(' go@'));
254
- let root = getParentVertexFromEdge(goModEditOutput['Module']['Path']);
229
+ let root = goModEditOutput['Module']['Path'];
230
+ // Build set of direct dependency paths from go mod edit -json
231
+ let directDepPaths = new Set();
232
+ if (goModEditOutput['Require']) {
233
+ goModEditOutput['Require'].forEach(req => {
234
+ if (!req['Indirect']) {
235
+ directDepPaths.add(req['Path']);
236
+ }
237
+ });
238
+ }
255
239
  let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "false", opts);
256
240
  if (matchManifestVersions === "true") {
257
- performManifestVersionsCheck(root, rows, manifest);
241
+ performManifestVersionsCheck(root, rows, manifestContent, parser, requireQuery);
258
242
  }
259
243
  const mainModule = toPurl(root, "@");
260
244
  const license = readLicenseFromManifest(manifest);
@@ -272,7 +256,11 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
272
256
  currentParent = getParentVertexFromEdge(row);
273
257
  source = toPurl(currentParent, "@");
274
258
  }
275
- let target = toPurl(getChildVertexFromEdge(row), "@");
259
+ let child = getChildVertexFromEdge(row);
260
+ let target = toPurl(child, "@");
261
+ if (getParentVertexFromEdge(row) === root && !directDepPaths.has(getPackageName(child))) {
262
+ return;
263
+ }
276
264
  sbom.addDependency(source, target);
277
265
  });
278
266
  // at the end, filter out all ignored dependencies including versions.
@@ -282,10 +270,12 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
282
270
  else {
283
271
  let directDependencies = rows.filter(row => row.startsWith(root));
284
272
  directDependencies.forEach(pair => {
285
- let dependency = getChildVertexFromEdge(pair);
286
- let depPurl = toPurl(dependency, "@");
287
- if (dependencyNotIgnored(ignoredDeps, depPurl)) {
288
- sbom.addDependency(mainModule, depPurl);
273
+ let child = getChildVertexFromEdge(pair);
274
+ let target = toPurl(child, "@");
275
+ if (dependencyNotIgnored(ignoredDeps, target)) {
276
+ if (directDepPaths.has(getPackageName(child))) {
277
+ sbom.addDependency(mainModule, target);
278
+ }
289
279
  }
290
280
  });
291
281
  enforceRemovingIgnoredDepsInCaseOfAutomaticVersionUpdate(ignoredDeps, sbom);
@@ -295,7 +285,7 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
295
285
  /**
296
286
  * Utility function for creating Purl String
297
287
 
298
- * @param {string }dependency the name of the artifact, can include a namespace(group) or not - namespace/artifactName.
288
+ * @param {string} dependency the name of the artifact, can include a namespace(group) or not - namespace/artifactName.
299
289
  * @param {RegExp} delimiter delimiter between name of dependency and version
300
290
  * @private
301
291
  * @returns {PackageURL|null} PackageUrl Object ready to be used in SBOM
@@ -320,12 +310,12 @@ function toPurl(dependency, delimiter) {
320
310
  }
321
311
  return pkg;
322
312
  }
323
- /** This function gets rows from go mod graph , and go.mod graph, and selecting for all
313
+ /** This function gets rows from go mod graph, and go.mod graph, and selecting for all
324
314
  * packages the has more than one minor the final versions as selected by golang MVS algorithm.
325
- * @param {[string]}rows all the rows from go modules dependency tree
315
+ * @param {string[]} rows all the rows from go modules dependency tree
326
316
  * @param {string} manifestPath the path of the go.mod file
327
317
  * @param {string} path to go binary
328
- * @return {[string]} rows that contains final versions.
318
+ * @return {string[]} rows that contains final versions.
329
319
  */
330
320
  function getFinalPackagesVersionsForModule(rows, manifestPath, goBin) {
331
321
  let manifestDir = path.dirname(manifestPath);
@@ -380,7 +370,7 @@ function getFinalPackagesVersionsForModule(rows, manifestPath, goBin) {
380
370
  /**
381
371
  *
382
372
  * @param {string} fullPackage - full package with its name and version
383
- * @return -{string} package name only
373
+ * @return {string} package name only
384
374
  * @private
385
375
  */
386
376
  function getPackageName(fullPackage) {
@@ -398,7 +388,7 @@ function isSpecialGoModule(moduleName) {
398
388
  /**
399
389
  *
400
390
  * @param {string} fullPackage - full package with its name and version
401
- * @return -{string} package version only
391
+ * @return {string|undefined} package version only
402
392
  * @private
403
393
  */
404
394
  function getVersionOfPackage(fullPackage) {
@@ -0,0 +1,4 @@
1
+ export function getParser(): Promise<Parser>;
2
+ export function getRequireQuery(): Promise<Query>;
3
+ import { Parser } from 'web-tree-sitter';
4
+ import { Query } from 'web-tree-sitter';
@@ -0,0 +1,16 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { Language, Parser, Query } from 'web-tree-sitter';
3
+ const wasmUrl = new URL('./tree-sitter-gomod.wasm', import.meta.url);
4
+ async function init() {
5
+ await Parser.init();
6
+ const wasmBytes = new Uint8Array(await readFile(wasmUrl));
7
+ return await Language.load(wasmBytes);
8
+ }
9
+ export async function getParser() {
10
+ const language = await init();
11
+ return new Parser().setLanguage(language);
12
+ }
13
+ export async function getRequireQuery() {
14
+ const language = await init();
15
+ return new Query(language, '(require_spec (module_path) @name (version) @version) @spec');
16
+ }
@@ -1,3 +1,22 @@
1
+ /**
2
+ * Discover all build.gradle[.kts] manifest paths in a Gradle multi-project build.
3
+ * Uses a custom init script to get structured project listing.
4
+ *
5
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain settings.gradle[.kts])
6
+ * @param {import('../index.js').Options} [opts={}]
7
+ * @returns {Promise<string[]>} Paths to build.gradle[.kts] files (absolute)
8
+ */
9
+ export function discoverGradleSubprojects(workspaceRoot: string, opts?: import("../index.js").Options): Promise<string[]>;
10
+ /**
11
+ * Parse the structured output from the Gradle init script.
12
+ *
13
+ * @param {string} raw - Raw stdout from gradle
14
+ * @returns {{ path: string, dir: string }[]}
15
+ */
16
+ export function parseGradleInitScriptOutput(raw: string): {
17
+ path: string;
18
+ dir: string;
19
+ }[];
1
20
  /**
2
21
  * This class provides common functionality for Groovy and Kotlin DSL files.
3
22
  */
@@ -1,8 +1,13 @@
1
+ import crypto from 'node:crypto';
1
2
  import fs from 'node:fs';
3
+ import os from 'node:os';
2
4
  import path from 'node:path';
3
5
  import { EOL } from 'os';
4
6
  import TOML from 'fast-toml';
7
+ import { readLicenseFile } from '../license/license_utils.js';
5
8
  import Sbom from '../sbom.js';
9
+ import { invokeCommand } from '../tools.js';
10
+ import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore } from '../workspace.js';
6
11
  import Base_java, { ecosystem_gradle } from "./base_java.js";
7
12
  /** @typedef {import('../provider.js').Provider} */
8
13
  /** @typedef {import('../provider.js').Provided} Provided */
@@ -55,7 +60,7 @@ export default class Java_gradle extends Base_java {
55
60
  * @returns {null}
56
61
  */
57
62
  // eslint-disable-next-line no-unused-vars
58
- readLicenseFromManifest(manifestPath) { return null; }
63
+ readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
59
64
  /**
60
65
  * Provide content and content type for stack analysis.
61
66
  * @param {string} manifest - the manifest path or name
@@ -406,3 +411,113 @@ export default class Java_gradle extends Base_java {
406
411
  return undefined;
407
412
  }
408
413
  }
414
+ const DEFAULT_GRADLE_DISCOVERY_IGNORE = [
415
+ '**/build/**',
416
+ '**/.gradle/**',
417
+ ];
418
+ /** Gradle init script that emits structured project listing. */
419
+ const GRADLE_INIT_SCRIPT = `allprojects {
420
+ task daListProjects {
421
+ doLast {
422
+ println "::DA_PROJECT::\${project.path}::\${project.projectDir}"
423
+ }
424
+ }
425
+ }
426
+ `;
427
+ /**
428
+ * Discover all build.gradle[.kts] manifest paths in a Gradle multi-project build.
429
+ * Uses a custom init script to get structured project listing.
430
+ *
431
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain settings.gradle[.kts])
432
+ * @param {import('../index.js').Options} [opts={}]
433
+ * @returns {Promise<string[]>} Paths to build.gradle[.kts] files (absolute)
434
+ */
435
+ export async function discoverGradleSubprojects(workspaceRoot, opts = {}) {
436
+ const root = path.resolve(workspaceRoot);
437
+ const hasSettings = fs.existsSync(path.join(root, 'settings.gradle'))
438
+ || fs.existsSync(path.join(root, 'settings.gradle.kts'));
439
+ if (!hasSettings) {
440
+ return [];
441
+ }
442
+ const manifestPaths = [];
443
+ const rootBuildKts = path.join(root, 'build.gradle.kts');
444
+ const rootBuild = path.join(root, 'build.gradle');
445
+ const rootManifest = fs.existsSync(rootBuildKts) ? rootBuildKts : fs.existsSync(rootBuild) ? rootBuild : null;
446
+ if (rootManifest) {
447
+ manifestPaths.push(rootManifest);
448
+ }
449
+ let gradleBin;
450
+ try {
451
+ gradleBin = new Java_gradle().selectToolBinary(rootManifest || rootBuild, opts);
452
+ }
453
+ catch {
454
+ const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_GRADLE_DISCOVERY_IGNORE];
455
+ return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
456
+ }
457
+ const initScriptPath = path.join(os.tmpdir(), `da-list-projects-${crypto.randomUUID()}.gradle`);
458
+ try {
459
+ fs.writeFileSync(initScriptPath, GRADLE_INIT_SCRIPT);
460
+ let output;
461
+ try {
462
+ output = invokeCommand(gradleBin, [
463
+ '-q', '--no-daemon',
464
+ '--init-script', initScriptPath,
465
+ 'daListProjects',
466
+ ], { cwd: root });
467
+ }
468
+ catch {
469
+ const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_GRADLE_DISCOVERY_IGNORE];
470
+ return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
471
+ }
472
+ const projects = parseGradleInitScriptOutput(output.toString());
473
+ for (const proj of projects) {
474
+ if (proj.path === ':') {
475
+ continue;
476
+ }
477
+ const projDir = path.resolve(proj.dir);
478
+ const buildKts = path.join(projDir, 'build.gradle.kts');
479
+ const buildGroovy = path.join(projDir, 'build.gradle');
480
+ if (fs.existsSync(buildKts)) {
481
+ manifestPaths.push(buildKts);
482
+ }
483
+ else if (fs.existsSync(buildGroovy)) {
484
+ manifestPaths.push(buildGroovy);
485
+ }
486
+ }
487
+ }
488
+ finally {
489
+ try {
490
+ fs.unlinkSync(initScriptPath);
491
+ }
492
+ catch { /* ignore */ }
493
+ }
494
+ const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_GRADLE_DISCOVERY_IGNORE];
495
+ return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
496
+ }
497
+ /**
498
+ * Parse the structured output from the Gradle init script.
499
+ *
500
+ * @param {string} raw - Raw stdout from gradle
501
+ * @returns {{ path: string, dir: string }[]}
502
+ */
503
+ export function parseGradleInitScriptOutput(raw) {
504
+ const projects = [];
505
+ for (const rawLine of raw.split('\n')) {
506
+ const line = rawLine.trimEnd();
507
+ if (!line.startsWith('::DA_PROJECT::')) {
508
+ continue;
509
+ }
510
+ const prefix = '::DA_PROJECT::';
511
+ const remainder = line.substring(prefix.length);
512
+ const lastSep = remainder.lastIndexOf('::');
513
+ if (lastSep < 0) {
514
+ continue;
515
+ }
516
+ const projPath = remainder.substring(0, lastSep);
517
+ const dir = remainder.substring(lastSep + 2);
518
+ if (projPath && dir) {
519
+ projects.push({ path: projPath, dir });
520
+ }
521
+ }
522
+ return projects;
523
+ }
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Discover all pom.xml manifest paths in a Maven multi-module project.
3
+ *
4
+ * @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pom.xml)
5
+ * @param {object} [opts={}]
6
+ * @returns {Promise<string[]>} Paths to pom.xml files (absolute)
7
+ */
8
+ export function discoverMavenModules(workspaceRoot: string, opts?: object): Promise<string[]>;
1
9
  /** @typedef {import('../provider').Provider} */
2
10
  /** @typedef {import('../provider').Provided} Provided */
3
11
  /** @typedef {{name: string, version: string}} Package */
@@ -28,7 +36,7 @@ export default class Java_maven extends Base_java {
28
36
  */
29
37
  provideComponent(manifest: string, opts?: {}): Provided;
30
38
  /**
31
- * Read license from pom.xml manifest
39
+ * Read license from pom.xml manifest, with fallback to LICENSE file
32
40
  * @param {string} manifestPath - path to pom.xml
33
41
  * @returns {string|null}
34
42
  */