refactor(maven): externalize update and extract

This commit is contained in:
Rhys Arkins 2019-04-06 10:05:52 +02:00
parent ec7ad01faa
commit 431588a27a
3 changed files with 46 additions and 41 deletions

View file

@ -135,8 +135,31 @@ function extractDependencies(raw) {
return result; return result;
} }
async function extractAllPackageFiles(config, packageFiles) {
const mavenFiles = [];
for (const packageFile of packageFiles) {
const content = await platform.getFile(packageFile);
if (content) {
const deps = extractDependencies(content);
if (deps) {
mavenFiles.push({
packageFile,
manager: 'maven',
...deps,
});
} else {
logger.info({ packageFile }, 'can not read dependencies');
}
} else {
logger.info({ packageFile }, 'packageFile has no content');
}
}
return mavenFiles;
}
module.exports = { module.exports = {
containsPlaceholder, containsPlaceholder,
parsePom, parsePom,
extractDependencies, extractDependencies,
extractAllPackageFiles,
}; };

View file

@ -1,44 +1,5 @@
const { extractDependencies } = require('./extract'); const { extractAllPackageFiles } = require('./extract');
const { updateDependency } = require('./update');
async function extractAllPackageFiles(config, packageFiles) {
const mavenFiles = [];
for (const packageFile of packageFiles) {
const content = await platform.getFile(packageFile);
if (content) {
const deps = extractDependencies(content);
if (deps) {
mavenFiles.push({
packageFile,
manager: 'maven',
...deps,
});
} else {
logger.info({ packageFile }, 'can not read dependencies');
}
} else {
logger.info({ packageFile }, 'packageFile has no content');
}
}
return mavenFiles;
}
function updateDependency(fileContent, upgrade) {
const { currentValue, newValue, fileReplacePosition } = upgrade;
const leftPart = fileContent.slice(0, fileReplacePosition);
const rightPart = fileContent.slice(fileReplacePosition);
const versionClosePosition = rightPart.indexOf('</');
const restPart = rightPart.slice(versionClosePosition);
const versionPart = rightPart.slice(0, versionClosePosition);
const version = versionPart.trim();
if (version === newValue) {
return fileContent;
}
if (version === currentValue) {
const replacedPart = versionPart.replace(currentValue, newValue);
return leftPart + replacedPart + restPart;
}
return null;
}
module.exports = { module.exports = {
extractAllPackageFiles, extractAllPackageFiles,

View file

@ -0,0 +1,21 @@
module.exports = {
updateDependency,
};
function updateDependency(fileContent, upgrade) {
const { currentValue, newValue, fileReplacePosition } = upgrade;
const leftPart = fileContent.slice(0, fileReplacePosition);
const rightPart = fileContent.slice(fileReplacePosition);
const versionClosePosition = rightPart.indexOf('</');
const restPart = rightPart.slice(versionClosePosition);
const versionPart = rightPart.slice(0, versionClosePosition);
const version = versionPart.trim();
if (version === newValue) {
return fileContent;
}
if (version === currentValue) {
const replacedPart = versionPart.replace(currentValue, newValue);
return leftPart + replacedPart + restPart;
}
return null;
}