Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions lib/updaters/types/maven.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
const jsdom = require('jsdom');
const serialize = require('w3c-xmlserializer');
const detectNewline = require('detect-newline');
const { XMLParser, XMLBuilder } = require('fast-xml-parser');

const CRLF = '\r\n';
const LF = '\n';

function pomDocument(contents) {
const dom = new jsdom.JSDOM('');
const parser = new dom.window.DOMParser();
return parser.parseFromString(contents, 'application/xml');
const parser = new XMLParser();
return parser.parse(contents);
}

function pomVersionElement(document) {
const versionElement = document.querySelector('project > version');
function pomVersion(document) {
const version = document?.project?.version;

if (!versionElement) {
if (!version) {
throw new Error(
'Failed to read the version field in your pom file - is it present?',
);
}

return versionElement;
return version;
}

module.exports.readVersion = function (contents) {
const document = pomDocument(contents);
return pomVersionElement(document).textContent;
return pomVersion(document);
};

module.exports.writeVersion = function (contents, version) {
const newline = detectNewline(contents);
const document = pomDocument(contents);
const versionElement = pomVersionElement(document);

versionElement.textContent = version;
document.project.version = version;

const xml = serialize(document);
const builder = new XMLBuilder({format: true});
const xml = builder.build(document);

if (newline === CRLF) {
return xml.replace(/\n/g, CRLF) + CRLF;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@
"detect-indent": "^6.1.0",
"detect-newline": "^3.1.0",
"dotgitignore": "^2.1.0",
"fast-xml-parser": "^5.2.5",
"figures": "^3.2.0",
"find-up": "^5.0.0",
"git-semver-tags": "^5.0.1",
"jsdom": "^25.0.1",
"semver": "^7.6.3",
"w3c-xmlserializer": "^5.0.0",
"yaml": "^2.6.0",
"yargs": "^17.7.2"
},
Expand Down