@trustify-da/trustify-da-javascript-client 0.3.0-ea.dcb2120 → 0.3.0-ea.de62188
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 +1 -1
- package/dist/package.json +25 -22
- package/dist/src/analysis.d.ts +14 -7
- package/dist/src/analysis.js +29 -13
- package/dist/src/cli.js +58 -1
- package/dist/src/cyclone_dx_sbom.d.ts +7 -1
- package/dist/src/cyclone_dx_sbom.js +29 -8
- package/dist/src/index.d.ts +72 -11
- package/dist/src/index.js +76 -7
- package/dist/src/license/index.d.ts +3 -3
- package/dist/src/license/index.js +4 -1
- package/dist/src/license/license_utils.js +4 -1
- package/dist/src/license/licenses_api.d.ts +2 -2
- 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/provider.d.ts +4 -3
- package/dist/src/provider.js +8 -2
- package/dist/src/providers/base_java.d.ts +14 -12
- package/dist/src/providers/base_java.js +18 -44
- package/dist/src/providers/base_javascript.d.ts +20 -9
- package/dist/src/providers/base_javascript.js +10 -3
- package/dist/src/providers/base_pyproject.d.ts +22 -32
- package/dist/src/providers/base_pyproject.js +60 -74
- 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 +11 -1
- package/dist/src/providers/golang_gomodules.js +65 -8
- package/dist/src/providers/java_gradle.d.ts +19 -0
- package/dist/src/providers/java_gradle.js +116 -2
- 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 +10 -2
- package/dist/src/providers/java_maven.js +165 -1
- package/dist/src/providers/javascript_bun.d.ts +10 -0
- package/dist/src/providers/javascript_bun.js +100 -0
- package/dist/src/providers/javascript_npm.d.ts +6 -0
- package/dist/src/providers/javascript_npm.js +21 -0
- package/dist/src/providers/javascript_pnpm.js +6 -2
- 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 +2 -2
- package/dist/src/providers/processors/yarn_berry_processor.js +6 -2
- package/dist/src/providers/processors/yarn_classic_processor.d.ts +2 -2
- package/dist/src/providers/python_controller.d.ts +5 -1
- package/dist/src/providers/python_controller.js +65 -7
- package/dist/src/providers/python_pip.d.ts +5 -0
- package/dist/src/providers/python_pip.js +5 -5
- 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 +64 -8
- package/dist/src/providers/python_poetry.js +139 -22
- package/dist/src/providers/python_uv.d.ts +20 -1
- package/dist/src/providers/python_uv.js +126 -3
- package/dist/src/providers/rust_cargo.d.ts +6 -2
- package/dist/src/providers/rust_cargo.js +31 -4
- package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
- package/dist/src/sbom.d.ts +7 -1
- package/dist/src/sbom.js +4 -2
- package/dist/src/tools.d.ts +28 -2
- package/dist/src/tools.js +58 -0
- package/dist/src/updaters/maven_updater.d.ts +26 -0
- package/dist/src/updaters/maven_updater.js +322 -0
- package/dist/src/updaters/toml_updater.d.ts +33 -0
- package/dist/src/updaters/toml_updater.js +244 -0
- package/dist/src/workspace.d.ts +9 -0
- package/dist/src/workspace.js +1 -1
- package/package.json +26 -23
package/dist/src/sbom.js
CHANGED
|
@@ -43,10 +43,12 @@ export default class Sbom {
|
|
|
43
43
|
/**
|
|
44
44
|
* @param {component} sourceRef current source Component ( Starting from root component by clients)
|
|
45
45
|
* @param {PackageURL} targetRef current dependency to add to Dependencies list of component sourceRef
|
|
46
|
+
* @param {string} [scope] - Scope of the dependency
|
|
47
|
+
* @param {Array<{alg: string, content: string}>} [targetHashes] - Optional hashes for the target component
|
|
46
48
|
* @return Sbom
|
|
47
49
|
*/
|
|
48
|
-
addDependency(sourceRef, targetRef, scope) {
|
|
49
|
-
return this.sbomModel.addDependency(sourceRef, targetRef, scope);
|
|
50
|
+
addDependency(sourceRef, targetRef, scope, targetHashes) {
|
|
51
|
+
return this.sbomModel.addDependency(sourceRef, targetRef, scope, targetHashes);
|
|
50
52
|
}
|
|
51
53
|
/**
|
|
52
54
|
* @return String sbom json in a string format
|
package/dist/src/tools.d.ts
CHANGED
|
@@ -61,6 +61,32 @@ export function toPurlFromString(strPurl: any): PackageURL | null;
|
|
|
61
61
|
* @param {string} cwd - directory for which to find the root of the git repository.
|
|
62
62
|
*/
|
|
63
63
|
export function getGitRootDir(cwd: string): string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Normalize a filesystem path, lowercasing on Windows for case-insensitive comparison.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} thePath
|
|
68
|
+
* @returns {string}
|
|
69
|
+
*/
|
|
70
|
+
export function normalizePath(thePath: string): string;
|
|
71
|
+
/**
|
|
72
|
+
* Walk up from `startDir` to `repoRoot` looking for an executable wrapper script.
|
|
73
|
+
*
|
|
74
|
+
* @param {string} startDir - Absolute directory to start from
|
|
75
|
+
* @param {string} wrapperName - Wrapper filename (e.g. `mvnw`, `gradlew`)
|
|
76
|
+
* @param {string} [repoRoot] - Stop boundary (defaults to git root or filesystem root)
|
|
77
|
+
* @returns {string | undefined}
|
|
78
|
+
*/
|
|
79
|
+
export function traverseForWrapper(startDir: string, wrapperName: string, repoRoot?: string): string | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Resolve a build-tool binary, preferring a wrapper when configured.
|
|
82
|
+
*
|
|
83
|
+
* @param {string} globalBinary - Global binary name (e.g. `mvn`, `gradle`)
|
|
84
|
+
* @param {string} localWrapper - Wrapper filename (e.g. `mvnw`, `gradlew.bat`)
|
|
85
|
+
* @param {string} startDir - Directory from which to start the wrapper search
|
|
86
|
+
* @param {import('./index.js').Options} [opts={}]
|
|
87
|
+
* @returns {string} Path to the resolved binary
|
|
88
|
+
*/
|
|
89
|
+
export function resolveBinary(globalBinary: string, localWrapper: string, startDir: string, opts?: import("./index.js").Options): string;
|
|
64
90
|
/** this method invokes command string in a process in a synchronous way.
|
|
65
91
|
* @param {string} bin - the command to be invoked
|
|
66
92
|
* @param {Array<string>} args - the args to pass to the binary
|
|
@@ -74,13 +100,13 @@ export function invokeCommand(bin: string, args: Array<string>, opts?: import("c
|
|
|
74
100
|
* @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
|
|
75
101
|
* @returns {RequestInit} The fetch options with proxy agent if applicable
|
|
76
102
|
*/
|
|
77
|
-
export function addProxyAgent(options: RequestInit, opts:
|
|
103
|
+
export function addProxyAgent(options: RequestInit, opts: any): RequestInit;
|
|
78
104
|
/**
|
|
79
105
|
* Utility function for fetching vendor tokens
|
|
80
106
|
* @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
|
|
81
107
|
* @returns {{}}
|
|
82
108
|
*/
|
|
83
|
-
export function getTokenHeaders(opts?:
|
|
109
|
+
export function getTokenHeaders(opts?: any): {};
|
|
84
110
|
export const RegexNotToBeLogged: RegExp;
|
|
85
111
|
export const TRUSTIFY_DA_TOKEN_HEADER: "trust-da-token";
|
|
86
112
|
export const TRUSTIFY_DA_TELEMETRY_ID_HEADER: "telemetry-anonymous-id";
|
package/dist/src/tools.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { execFileSync } from "child_process";
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
2
4
|
import { EOL } from "os";
|
|
3
5
|
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
4
6
|
import { PackageURL } from "packageurl-js";
|
|
@@ -128,6 +130,62 @@ export function getGitRootDir(cwd) {
|
|
|
128
130
|
return undefined;
|
|
129
131
|
}
|
|
130
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Normalize a filesystem path, lowercasing on Windows for case-insensitive comparison.
|
|
135
|
+
*
|
|
136
|
+
* @param {string} thePath
|
|
137
|
+
* @returns {string}
|
|
138
|
+
*/
|
|
139
|
+
export function normalizePath(thePath) {
|
|
140
|
+
const normalized = path.resolve(thePath).normalize();
|
|
141
|
+
return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Walk up from `startDir` to `repoRoot` looking for an executable wrapper script.
|
|
145
|
+
*
|
|
146
|
+
* @param {string} startDir - Absolute directory to start from
|
|
147
|
+
* @param {string} wrapperName - Wrapper filename (e.g. `mvnw`, `gradlew`)
|
|
148
|
+
* @param {string} [repoRoot] - Stop boundary (defaults to git root or filesystem root)
|
|
149
|
+
* @returns {string | undefined}
|
|
150
|
+
*/
|
|
151
|
+
export function traverseForWrapper(startDir, wrapperName, repoRoot = undefined) {
|
|
152
|
+
const currentDir = normalizePath(startDir);
|
|
153
|
+
repoRoot = repoRoot || getGitRootDir(currentDir) || path.parse(currentDir).root;
|
|
154
|
+
const wrapperPath = path.join(currentDir, wrapperName);
|
|
155
|
+
try {
|
|
156
|
+
fs.accessSync(wrapperPath, fs.constants.X_OK);
|
|
157
|
+
return wrapperPath;
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
const rootDir = path.parse(currentDir).root;
|
|
161
|
+
if (currentDir === repoRoot || currentDir === rootDir) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const parentDir = path.dirname(currentDir);
|
|
165
|
+
if (parentDir === currentDir || parentDir === rootDir) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
return traverseForWrapper(parentDir, wrapperName, repoRoot);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Resolve a build-tool binary, preferring a wrapper when configured.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} globalBinary - Global binary name (e.g. `mvn`, `gradle`)
|
|
175
|
+
* @param {string} localWrapper - Wrapper filename (e.g. `mvnw`, `gradlew.bat`)
|
|
176
|
+
* @param {string} startDir - Directory from which to start the wrapper search
|
|
177
|
+
* @param {import('./index.js').Options} [opts={}]
|
|
178
|
+
* @returns {string} Path to the resolved binary
|
|
179
|
+
*/
|
|
180
|
+
export function resolveBinary(globalBinary, localWrapper, startDir, opts = {}) {
|
|
181
|
+
if (getWrapperPreference(globalBinary, opts)) {
|
|
182
|
+
const wrapper = traverseForWrapper(startDir, localWrapper);
|
|
183
|
+
if (wrapper !== undefined) {
|
|
184
|
+
return wrapper;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return getCustomPath(globalBinary, opts);
|
|
188
|
+
}
|
|
131
189
|
/** this method invokes command string in a process in a synchronous way.
|
|
132
190
|
* @param {string} bin - the command to be invoked
|
|
133
191
|
* @param {Array<string>} args - the args to pass to the binary
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Updates dependency versions in a Maven pom.xml while preserving file formatting.
|
|
3
|
+
*
|
|
4
|
+
* Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
|
|
5
|
+
* then performs position-based string splice to replace version strings without altering
|
|
6
|
+
* any other formatting, comments, whitespace, or indentation.
|
|
7
|
+
*
|
|
8
|
+
* Supports `${property}` indirection: when a dependency's `<version>` is a property
|
|
9
|
+
* reference, the updater resolves the property chain (including recursive references)
|
|
10
|
+
* and updates the terminal property value in `<properties>` instead.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} pomContent - the raw pom.xml file content
|
|
13
|
+
* @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges -
|
|
14
|
+
* list of version changes to apply
|
|
15
|
+
* @returns {{content: string, applied: Array, skipped: Array}} the updated content and
|
|
16
|
+
* lists of applied and skipped changes
|
|
17
|
+
*/
|
|
18
|
+
export function updateMavenVersions(pomContent: string, versionChanges: Array<{
|
|
19
|
+
groupId: string;
|
|
20
|
+
artifactId: string;
|
|
21
|
+
newVersion: string;
|
|
22
|
+
}>): {
|
|
23
|
+
content: string;
|
|
24
|
+
applied: any[];
|
|
25
|
+
skipped: any[];
|
|
26
|
+
};
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { XMLParser } from 'fast-xml-parser';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts the trimmed text content of a simple XML element from a string block.
|
|
4
|
+
* @param {string} block - the XML text to search within
|
|
5
|
+
* @param {string} tagName - the element name whose content to extract
|
|
6
|
+
* @returns {string|null} the trimmed text content, or null if not found
|
|
7
|
+
*/
|
|
8
|
+
function extractTagContent(block, tagName) {
|
|
9
|
+
const openTag = `<${tagName}>`;
|
|
10
|
+
const closeTag = `</${tagName}>`;
|
|
11
|
+
const start = block.indexOf(openTag);
|
|
12
|
+
if (start === -1) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const contentStart = start + openTag.length;
|
|
16
|
+
const end = block.indexOf(closeTag, contentStart);
|
|
17
|
+
if (end === -1) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return block.substring(contentStart, end).trim();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Finds the character positions of an XML element's text content within the raw file.
|
|
24
|
+
* Returns the start (inclusive) and end (exclusive) offsets of the text between the
|
|
25
|
+
* opening and closing tags.
|
|
26
|
+
* @param {string} content - full raw XML content
|
|
27
|
+
* @param {number} searchStart - start offset to search from
|
|
28
|
+
* @param {number} searchEnd - end offset to search within
|
|
29
|
+
* @param {string} tagName - the element name whose content position to locate
|
|
30
|
+
* @returns {{start: number, end: number}|null} character offsets, or null if not found
|
|
31
|
+
*/
|
|
32
|
+
function findTagContentPosition(content, searchStart, searchEnd, tagName) {
|
|
33
|
+
const openTag = `<${tagName}>`;
|
|
34
|
+
const closeTag = `</${tagName}>`;
|
|
35
|
+
const tagStart = content.indexOf(openTag, searchStart);
|
|
36
|
+
if (tagStart === -1 || tagStart >= searchEnd) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const contentStart = tagStart + openTag.length;
|
|
40
|
+
const tagEnd = content.indexOf(closeTag, contentStart);
|
|
41
|
+
if (tagEnd === -1 || tagEnd > searchEnd) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return { start: contentStart, end: tagEnd };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Checks whether the character immediately following a tag prefix indicates
|
|
48
|
+
* that the prefix is the complete tag name (e.g. `<dependency>` vs `<dependencyManagement>`).
|
|
49
|
+
* @param {string} char - the character after the tag prefix
|
|
50
|
+
* @returns {boolean} true if the character terminates the tag name
|
|
51
|
+
*/
|
|
52
|
+
function isTagBoundary(char) {
|
|
53
|
+
return char === '>' || char === ' ' || char === '\t'
|
|
54
|
+
|| char === '\n' || char === '\r' || char === '/';
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Finds the text content position of a `<version>` element within the first
|
|
58
|
+
* `<dependency>` block matching the given groupId and artifactId.
|
|
59
|
+
* Searches through all `<dependency>` elements in the file, covering both
|
|
60
|
+
* `<dependencies>` and `<dependencyManagement>` sections.
|
|
61
|
+
* @param {string} content - full raw pom.xml content
|
|
62
|
+
* @param {string} groupId - the dependency's groupId to match
|
|
63
|
+
* @param {string} artifactId - the dependency's artifactId to match
|
|
64
|
+
* @returns {{start: number, end: number}|null} version content offsets, or null
|
|
65
|
+
*/
|
|
66
|
+
function findDependencyVersionPosition(content, groupId, artifactId) {
|
|
67
|
+
let searchStart = 0;
|
|
68
|
+
const TAG_PREFIX = '<dependency';
|
|
69
|
+
const CLOSE_TAG = '</dependency>';
|
|
70
|
+
while (searchStart < content.length) {
|
|
71
|
+
const start = content.indexOf(TAG_PREFIX, searchStart);
|
|
72
|
+
if (start === -1) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
// Verify this is exactly <dependency> and not <dependencyManagement> etc.
|
|
76
|
+
const charAfterTag = content.charAt(start + TAG_PREFIX.length);
|
|
77
|
+
if (!isTagBoundary(charAfterTag)) {
|
|
78
|
+
searchStart = start + 1;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
// Find closing </dependency>
|
|
82
|
+
const end = content.indexOf(CLOSE_TAG, start + TAG_PREFIX.length);
|
|
83
|
+
if (end === -1) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
const blockEnd = end + CLOSE_TAG.length;
|
|
87
|
+
const block = content.substring(start, blockEnd);
|
|
88
|
+
const gid = extractTagContent(block, 'groupId');
|
|
89
|
+
const aid = extractTagContent(block, 'artifactId');
|
|
90
|
+
if (gid === groupId && aid === artifactId) {
|
|
91
|
+
const pos = findTagContentPosition(content, start, blockEnd, 'version');
|
|
92
|
+
if (pos) {
|
|
93
|
+
return pos;
|
|
94
|
+
}
|
|
95
|
+
// Matching dependency but no <version> element; continue searching
|
|
96
|
+
// (the version may be in <dependencyManagement>)
|
|
97
|
+
}
|
|
98
|
+
searchStart = blockEnd;
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Finds the text content position of a property element inside a `<properties>` block.
|
|
104
|
+
* Searches through all `<properties>` blocks in the file.
|
|
105
|
+
* @param {string} content - full raw pom.xml content
|
|
106
|
+
* @param {string} propName - the property element name to locate
|
|
107
|
+
* @returns {{start: number, end: number}|null} property value offsets, or null
|
|
108
|
+
*/
|
|
109
|
+
function findPropertyPosition(content, propName) {
|
|
110
|
+
let searchStart = 0;
|
|
111
|
+
const TAG_PREFIX = '<properties';
|
|
112
|
+
const CLOSE_TAG = '</properties>';
|
|
113
|
+
while (searchStart < content.length) {
|
|
114
|
+
const propsStart = content.indexOf(TAG_PREFIX, searchStart);
|
|
115
|
+
if (propsStart === -1) {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
// Verify this is exactly <properties> and not some other tag
|
|
119
|
+
const charAfter = content.charAt(propsStart + TAG_PREFIX.length);
|
|
120
|
+
if (!isTagBoundary(charAfter)) {
|
|
121
|
+
searchStart = propsStart + 1;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const propsCloseStart = content.indexOf(CLOSE_TAG, propsStart);
|
|
125
|
+
if (propsCloseStart === -1) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
const propsBlockEnd = propsCloseStart + CLOSE_TAG.length;
|
|
129
|
+
const result = findTagContentPosition(content, propsStart, propsBlockEnd, propName);
|
|
130
|
+
if (result) {
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
searchStart = propsBlockEnd;
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Resolves a Maven property chain, following `${propName}` references through
|
|
139
|
+
* the properties map until a concrete (non-reference) value is reached.
|
|
140
|
+
* Detects circular references via a visited set.
|
|
141
|
+
* @param {string} propName - the initial property name to resolve
|
|
142
|
+
* @param {Object} properties - properties map from the parsed XML
|
|
143
|
+
* @param {Set<string>} [visited] - visited property names (for cycle detection)
|
|
144
|
+
* @returns {{terminalPropName: string, resolvedValue: string}|{error: string}}
|
|
145
|
+
*/
|
|
146
|
+
function resolvePropertyChain(propName, properties, visited = new Set()) {
|
|
147
|
+
if (visited.has(propName)) {
|
|
148
|
+
const chain = [...visited, propName].join(' -> ');
|
|
149
|
+
return { error: `Circular property reference detected: ${chain}` };
|
|
150
|
+
}
|
|
151
|
+
visited.add(propName);
|
|
152
|
+
const value = properties[propName];
|
|
153
|
+
if (value == null) {
|
|
154
|
+
return { error: `Property '${propName}' not found in <properties>` };
|
|
155
|
+
}
|
|
156
|
+
const valueStr = String(value);
|
|
157
|
+
const propMatch = valueStr.match(/^\$\{(.+)\}$/);
|
|
158
|
+
if (propMatch) {
|
|
159
|
+
return resolvePropertyChain(propMatch[1], properties, visited);
|
|
160
|
+
}
|
|
161
|
+
return { terminalPropName: propName, resolvedValue: valueStr };
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Updates dependency versions in a Maven pom.xml while preserving file formatting.
|
|
165
|
+
*
|
|
166
|
+
* Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
|
|
167
|
+
* then performs position-based string splice to replace version strings without altering
|
|
168
|
+
* any other formatting, comments, whitespace, or indentation.
|
|
169
|
+
*
|
|
170
|
+
* Supports `${property}` indirection: when a dependency's `<version>` is a property
|
|
171
|
+
* reference, the updater resolves the property chain (including recursive references)
|
|
172
|
+
* and updates the terminal property value in `<properties>` instead.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} pomContent - the raw pom.xml file content
|
|
175
|
+
* @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges -
|
|
176
|
+
* list of version changes to apply
|
|
177
|
+
* @returns {{content: string, applied: Array, skipped: Array}} the updated content and
|
|
178
|
+
* lists of applied and skipped changes
|
|
179
|
+
*/
|
|
180
|
+
export function updateMavenVersions(pomContent, versionChanges) {
|
|
181
|
+
if (!versionChanges || versionChanges.length === 0) {
|
|
182
|
+
return { content: pomContent, applied: [], skipped: [] };
|
|
183
|
+
}
|
|
184
|
+
const parser = new XMLParser({
|
|
185
|
+
parseTagValue: false,
|
|
186
|
+
commentPropName: '#comment',
|
|
187
|
+
});
|
|
188
|
+
let parsed;
|
|
189
|
+
try {
|
|
190
|
+
parsed = parser.parse(pomContent);
|
|
191
|
+
}
|
|
192
|
+
catch (e) {
|
|
193
|
+
return {
|
|
194
|
+
content: pomContent,
|
|
195
|
+
applied: [],
|
|
196
|
+
skipped: versionChanges.map(vc => ({
|
|
197
|
+
groupId: vc.groupId,
|
|
198
|
+
artifactId: vc.artifactId,
|
|
199
|
+
newVersion: vc.newVersion,
|
|
200
|
+
reason: `Failed to parse pom.xml: ${e.message}`,
|
|
201
|
+
})),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
const project = parsed?.project;
|
|
205
|
+
if (!project) {
|
|
206
|
+
return {
|
|
207
|
+
content: pomContent,
|
|
208
|
+
applied: [],
|
|
209
|
+
skipped: versionChanges.map(vc => ({
|
|
210
|
+
groupId: vc.groupId,
|
|
211
|
+
artifactId: vc.artifactId,
|
|
212
|
+
newVersion: vc.newVersion,
|
|
213
|
+
reason: 'No <project> element found in pom.xml',
|
|
214
|
+
})),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const properties = project.properties || {};
|
|
218
|
+
const applied = [];
|
|
219
|
+
const skipped = [];
|
|
220
|
+
/** @type {Map<number, {start: number, end: number, newValue: string}>} */
|
|
221
|
+
const replacementsByPosition = new Map();
|
|
222
|
+
for (const change of versionChanges) {
|
|
223
|
+
const versionPos = findDependencyVersionPosition(pomContent, change.groupId, change.artifactId);
|
|
224
|
+
if (!versionPos) {
|
|
225
|
+
skipped.push({
|
|
226
|
+
groupId: change.groupId,
|
|
227
|
+
artifactId: change.artifactId,
|
|
228
|
+
newVersion: change.newVersion,
|
|
229
|
+
reason: 'Dependency not found in pom.xml or has no <version> element',
|
|
230
|
+
});
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
const currentVersion = pomContent.substring(versionPos.start, versionPos.end);
|
|
234
|
+
const propMatch = currentVersion.match(/^\$\{(.+)\}$/);
|
|
235
|
+
if (propMatch) {
|
|
236
|
+
const resolved = resolvePropertyChain(propMatch[1], properties);
|
|
237
|
+
if (resolved.error) {
|
|
238
|
+
skipped.push({
|
|
239
|
+
groupId: change.groupId,
|
|
240
|
+
artifactId: change.artifactId,
|
|
241
|
+
newVersion: change.newVersion,
|
|
242
|
+
reason: resolved.error,
|
|
243
|
+
});
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const propPos = findPropertyPosition(pomContent, resolved.terminalPropName);
|
|
247
|
+
if (!propPos) {
|
|
248
|
+
skipped.push({
|
|
249
|
+
groupId: change.groupId,
|
|
250
|
+
artifactId: change.artifactId,
|
|
251
|
+
newVersion: change.newVersion,
|
|
252
|
+
reason: `Could not locate property '${resolved.terminalPropName}' in pom.xml`,
|
|
253
|
+
});
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
// Deduplicate: multiple deps may share the same property
|
|
257
|
+
const existing = replacementsByPosition.get(propPos.start);
|
|
258
|
+
if (existing) {
|
|
259
|
+
if (existing.newValue !== change.newVersion) {
|
|
260
|
+
skipped.push({
|
|
261
|
+
groupId: change.groupId,
|
|
262
|
+
artifactId: change.artifactId,
|
|
263
|
+
newVersion: change.newVersion,
|
|
264
|
+
reason: `Property '${resolved.terminalPropName}' already targeted `
|
|
265
|
+
+ `with a different version '${existing.newValue}'`,
|
|
266
|
+
});
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
// Same replacement already queued; record as applied without duplicating
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
replacementsByPosition.set(propPos.start, {
|
|
273
|
+
start: propPos.start,
|
|
274
|
+
end: propPos.end,
|
|
275
|
+
newValue: change.newVersion,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
applied.push({
|
|
279
|
+
groupId: change.groupId,
|
|
280
|
+
artifactId: change.artifactId,
|
|
281
|
+
newVersion: change.newVersion,
|
|
282
|
+
type: 'property',
|
|
283
|
+
property: resolved.terminalPropName,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
const existing = replacementsByPosition.get(versionPos.start);
|
|
288
|
+
if (existing) {
|
|
289
|
+
if (existing.newValue !== change.newVersion) {
|
|
290
|
+
skipped.push({
|
|
291
|
+
groupId: change.groupId,
|
|
292
|
+
artifactId: change.artifactId,
|
|
293
|
+
newVersion: change.newVersion,
|
|
294
|
+
reason: `Version already targeted with a different version '${existing.newValue}'`,
|
|
295
|
+
});
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
replacementsByPosition.set(versionPos.start, {
|
|
301
|
+
start: versionPos.start,
|
|
302
|
+
end: versionPos.end,
|
|
303
|
+
newValue: change.newVersion,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
applied.push({
|
|
307
|
+
groupId: change.groupId,
|
|
308
|
+
artifactId: change.artifactId,
|
|
309
|
+
newVersion: change.newVersion,
|
|
310
|
+
type: 'direct',
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
// Sort replacements by position descending to preserve offsets during splice
|
|
315
|
+
const sortedReplacements = [...replacementsByPosition.values()]
|
|
316
|
+
.sort((a, b) => b.start - a.start);
|
|
317
|
+
let result = pomContent;
|
|
318
|
+
for (const r of sortedReplacements) {
|
|
319
|
+
result = result.substring(0, r.start) + r.newValue + result.substring(r.end);
|
|
320
|
+
}
|
|
321
|
+
return { content: result, applied, skipped };
|
|
322
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Updates dependency versions in a Gradle version catalog (libs.versions.toml) file.
|
|
3
|
+
*
|
|
4
|
+
* Supports two version declaration patterns:
|
|
5
|
+
* - Centralized: [versions] section defines version aliases, [libraries] references via version.ref
|
|
6
|
+
* - Inline: [libraries] entries include version = "x.y.z" directly
|
|
7
|
+
*
|
|
8
|
+
* Uses smol-toml parse() to locate keys, then performs position-based string replacement
|
|
9
|
+
* on the raw content to preserve formatting and comments.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} tomlContent - raw TOML file content
|
|
12
|
+
* @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges
|
|
13
|
+
* @returns {{content: string, applied: Array<{groupId: string, artifactId: string, newVersion: string, oldVersion: string}>, skipped: Array<{groupId: string, artifactId: string, newVersion: string, reason: string}>}}
|
|
14
|
+
*/
|
|
15
|
+
export function updateTomlVersions(tomlContent: string, versionChanges: Array<{
|
|
16
|
+
groupId: string;
|
|
17
|
+
artifactId: string;
|
|
18
|
+
newVersion: string;
|
|
19
|
+
}>): {
|
|
20
|
+
content: string;
|
|
21
|
+
applied: Array<{
|
|
22
|
+
groupId: string;
|
|
23
|
+
artifactId: string;
|
|
24
|
+
newVersion: string;
|
|
25
|
+
oldVersion: string;
|
|
26
|
+
}>;
|
|
27
|
+
skipped: Array<{
|
|
28
|
+
groupId: string;
|
|
29
|
+
artifactId: string;
|
|
30
|
+
newVersion: string;
|
|
31
|
+
reason: string;
|
|
32
|
+
}>;
|
|
33
|
+
};
|