diff --git a/gradle.js b/gradle.js index 9d2fd11..bcf47da 100644 --- a/gradle.js +++ b/gradle.js @@ -48,35 +48,23 @@ export const getGradleDependenciesWithVersions = async (parsedGradle) => { let allDependencies = []; // if it's not an array, a single dependency has been declared and it doesn't apply if (Array.isArray(parsedGradle.dependencies)) { - allDependencies = allDependencies.concat( - parsedGradle.dependencies.filter((dep) => dep.version), - ); + allDependencies = allDependencies.concat(parsedGradle.dependencies.filter((dep) => dep.version)); } if (Array.isArray(parsedGradle?.subprojects?.[0]?.dependencies)) { - allDependencies = allDependencies.concat( - parsedGradle.subprojects[0].dependencies.filter((dep) => dep.version), - ); + allDependencies = allDependencies.concat(parsedGradle.subprojects[0].dependencies.filter((dep) => dep.version)); } return allDependencies; }; export const getGradleSpringBootVersion = async (parsedGradle) => { - const springBootPlugin = parsedGradle?.plugins?.filter( - (plugin) => plugin.id === 'org.springframework.boot', - ); - if ( - Array.isArray(springBootPlugin) && - springBootPlugin.length && - springBootPlugin[0].version - ) { + const springBootPlugin = parsedGradle?.plugins?.filter((plugin) => plugin.id === 'org.springframework.boot'); + if (Array.isArray(springBootPlugin) && springBootPlugin.length && springBootPlugin[0].version) { // Handle build.gradle, which allows 3.2.+ format return springBootPlugin[0].version.replace('+', 'x'); } // there are likely more places we could search for this variable, but we'll start here if (parsedGradle?.buildscript?.[0]?.ext?.springBootVersion) { - return String( - parsedGradle?.buildscript?.[0]?.ext?.springBootVersion, - ).replace('+', 'x'); + return String(parsedGradle?.buildscript?.[0]?.ext?.springBootVersion).replace('+', 'x'); } if (parsedGradle.buildscript?.[0]?.["ext['springBootVersion']"]) { return parsedGradle.buildscript[0]["ext['springBootVersion']"]; @@ -85,28 +73,19 @@ export const getGradleSpringBootVersion = async (parsedGradle) => { return ''; }; -export const retrieveSimilarGradlePackages = async ( - parsedGradle, - springBootVersion, -) => { - const gradleDependenciesWithVersions = - await getGradleDependenciesWithVersions(parsedGradle); +export const retrieveSimilarGradlePackages = async (parsedGradle, springBootVersion) => { + const gradleDependenciesWithVersions = await getGradleDependenciesWithVersions(parsedGradle); if (springBootVersion) { - const defaultVersions = - await getDefaultSpringBootVersions(springBootVersion); + const defaultVersions = await getDefaultSpringBootVersions(springBootVersion); if (defaultVersions.length) { const declaredPackages = []; gradleDependenciesWithVersions.forEach((gradleDependency) => defaultVersions.forEach((bootPackage) => { - if ( - gradleDependency.group === bootPackage.group && - gradleDependency.name === bootPackage.name - ) { + if (gradleDependency.group === bootPackage.group && gradleDependency.name === bootPackage.name) { const existingMatches = declaredPackages.find( (declaredPackage) => - declaredPackage.group === gradleDependency.group && - declaredPackage.name === gradleDependency.name, + declaredPackage.group === gradleDependency.group && declaredPackage.name === gradleDependency.name, ); if (!existingMatches) { declaredPackages.push( diff --git a/package.json b/package.json index 162101d..dcfa343 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ }, "prettier": { "trailingComma": "all", + "printWidth": 120, "semi": true, "singleQuote": true }, diff --git a/pom.js b/pom.js index 938bd35..631cdd6 100644 --- a/pom.js +++ b/pom.js @@ -1,13 +1,7 @@ import { XMLParser } from 'fast-xml-parser'; import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs'; import { parse } from 'node-html-parser'; -import { - cachePath, - ensureDirExists, - getDefaultSpringBootVersions, - getJsonFromFile, - Package, -} from './shared.js'; +import { cachePath, ensureDirExists, getDefaultSpringBootVersions, getJsonFromFile, Package } from './shared.js'; export const getXMLFromFile = async (filename) => { try { @@ -32,9 +26,7 @@ export const getXMLFromFile = async (filename) => { parsedPomFiles.forEach((pom) => { properties = properties.concat(pom.project.properties ?? []); dependencies = dependencies.concat(pom.project.dependencies ?? []); - dependencyManagement = dependencyManagement.concat( - pom.project.dependencyManagement ?? [], - ); + dependencyManagement = dependencyManagement.concat(pom.project.dependencyManagement ?? []); parent = parent.concat(pom.project.parent ?? []); }); @@ -83,45 +75,32 @@ export const getPomDependenciesWithVersions = async (parsedPom) => { export const getPomSpringBootVersion = async (parsedPom) => { let bootVersion = parsedPom.project.parent.filter( - (pom) => - pom.groupId === 'org.springframework.boot' && - pom.artifactId === 'spring-boot-starter-parent', + (pom) => pom.groupId === 'org.springframework.boot' && pom.artifactId === 'spring-boot-starter-parent', ); if (bootVersion.length) { return bootVersion[0].version; } - bootVersion = - parsedPom.project.dependencyManagement?.dependencies?.dependency.filter( - (pom) => - pom.groupId === 'org.springframework.boot' && - pom.artifactId === 'spring-boot-starter-parent', - ); + bootVersion = parsedPom.project.dependencyManagement?.dependencies?.dependency.filter( + (pom) => pom.groupId === 'org.springframework.boot' && pom.artifactId === 'spring-boot-starter-parent', + ); if (bootVersion) { const tempBootVersion = bootVersion.version; return replaceVariable(parsedPom.project.properties, tempBootVersion); } - if ( - Array.isArray( - parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency, - ) - ) { - const bootVersion = - parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency.find( - (dependency) => - dependency.groupId === 'org.springframework.boot' && - dependency.artifactId === 'spring-boot-dependencies', - )?.version; + if (Array.isArray(parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency)) { + const bootVersion = parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency.find( + (dependency) => + dependency.groupId === 'org.springframework.boot' && dependency.artifactId === 'spring-boot-dependencies', + )?.version; if (bootVersion) { return replaceVariable(parsedPom.project.properties, bootVersion); } } if ( - parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency - .groupId === 'org.springframework.boot' && - parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency - .artifactId === 'spring-boot-dependencies' + parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency.groupId === 'org.springframework.boot' && + parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency.artifactId === 'spring-boot-dependencies' ) { return replaceVariable( parsedPom.project.properties, @@ -138,41 +117,24 @@ export const getPomSpringBootVersion = async (parsedPom) => { return ''; }; -export const retrieveSimilarPomPackages = async ( - parsedPom, - springBootVersion, -) => { - const pomDependenciesWithVersions = - await getPomDependenciesWithVersions(parsedPom); +export const retrieveSimilarPomPackages = async (parsedPom, springBootVersion) => { + const pomDependenciesWithVersions = await getPomDependenciesWithVersions(parsedPom); if (springBootVersion) { - const defaultVersions = - await getDefaultSpringBootVersions(springBootVersion); + const defaultVersions = await getDefaultSpringBootVersions(springBootVersion); if (defaultVersions.length) { const declaredPackages = []; pomDependenciesWithVersions.forEach((pomDependency) => defaultVersions.forEach((bootPackage) => { - if ( - pomDependency.groupId === bootPackage.group && - pomDependency.artifactId === bootPackage.name - ) { - const pomVersion = replaceVariable( - parsedPom.project.properties, - pomDependency.version, - ); + if (pomDependency.groupId === bootPackage.group && pomDependency.artifactId === bootPackage.name) { + const pomVersion = replaceVariable(parsedPom.project.properties, pomDependency.version); const existingMatches = declaredPackages.find( (declaredPackage) => - declaredPackage.group === pomDependency.groupId && - declaredPackage.name === pomDependency.artifactId, + declaredPackage.group === pomDependency.groupId && declaredPackage.name === pomDependency.artifactId, ); if (!existingMatches) { declaredPackages.push( - new Package( - pomDependency.groupId, - pomDependency.artifactId, - pomVersion, - bootPackage.version, - ), + new Package(pomDependency.groupId, pomDependency.artifactId, pomVersion, bootPackage.version), ); } } @@ -192,10 +154,7 @@ export const retrieveSimilarPomPackages = async ( return []; }; -export const retrieveSimilarPomProperties = async ( - parsedPom, - springBootVersion, -) => { +export const retrieveSimilarPomProperties = async (parsedPom, springBootVersion) => { const pomProperties = await getPomProperties(parsedPom); if (springBootVersion) { const defaultProperties = await getSpringBootProperties(springBootVersion); @@ -274,17 +233,9 @@ const downloadSpringVersionProperties = async (springBootVersion) => { }), ); } - await writeFileSync( - `${cachePath}/properties_${springBootVersion}.json`, - JSON.stringify(versions, null, 2), - ); + await writeFileSync(`${cachePath}/properties_${springBootVersion}.json`, JSON.stringify(versions, null, 2)); } else { - await writeFileSync( - `${cachePath}/properties_${springBootVersion}.json`, - JSON.stringify(versions, null, 2), - ); - console.log( - 'URL not found - Spring Boot default versions URL no longer exists.', - ); + await writeFileSync(`${cachePath}/properties_${springBootVersion}.json`, JSON.stringify(versions, null, 2)); + console.log('URL not found - Spring Boot default versions URL no longer exists.'); } }; diff --git a/shared.js b/shared.js index 0074b8a..cc4ea3b 100644 --- a/shared.js +++ b/shared.js @@ -58,18 +58,10 @@ const downloadSpringDefaultVersions = async (springBootVersion) => { version: child.childNodes[5].rawText, }), ); - await writeFileSync( - `${cachePath}/dependencies_${springBootVersion}.json`, - JSON.stringify(versions, null, 2), - ); + await writeFileSync(`${cachePath}/dependencies_${springBootVersion}.json`, JSON.stringify(versions, null, 2)); } else { - await writeFileSync( - `${cachePath}/dependencies_${springBootVersion}.json`, - JSON.stringify(versions, null, 2), - ); - console.log( - 'URL not found - Spring Boot default versions URL no longer exists.', - ); + await writeFileSync(`${cachePath}/dependencies_${springBootVersion}.json`, JSON.stringify(versions, null, 2)); + console.log('URL not found - Spring Boot default versions URL no longer exists.'); } }; diff --git a/test/test-pom.spec.js b/test/test-pom.spec.js index ebc087c..8fb13d6 100644 --- a/test/test-pom.spec.js +++ b/test/test-pom.spec.js @@ -39,10 +39,7 @@ describe('test pom parsing', () => { const xmlData = await getXMLFromFile(filename); - strictEqual( - xmlData.project.parent[0].artifactId, - 'spring-boot-starter-parent', - ); + strictEqual(xmlData.project.parent[0].artifactId, 'spring-boot-starter-parent'); strictEqual(xmlData.project.parent[0].groupId, 'org.springframework.boot'); strictEqual(xmlData.project.parent[0].version, '3.1.0'); @@ -50,22 +47,10 @@ describe('test pom parsing', () => { strictEqual(xmlData.project.properties[0]['jackson.version'], '2.10.2'); strictEqual(xmlData.project.dependencies[0].dependency.length, 2); - strictEqual( - xmlData.project.dependencies[0].dependency[0].groupId, - 'org.apache.httpcomponents', - ); - strictEqual( - xmlData.project.dependencies[0].dependency[0].artifactId, - 'httpclient', - ); - strictEqual( - xmlData.project.dependencies[0].dependency[1].groupId, - 'org.java-websocket', - ); - strictEqual( - xmlData.project.dependencies[0].dependency[1].artifactId, - 'Java-WebSocket', - ); + strictEqual(xmlData.project.dependencies[0].dependency[0].groupId, 'org.apache.httpcomponents'); + strictEqual(xmlData.project.dependencies[0].dependency[0].artifactId, 'httpclient'); + strictEqual(xmlData.project.dependencies[0].dependency[1].groupId, 'org.java-websocket'); + strictEqual(xmlData.project.dependencies[0].dependency[1].artifactId, 'Java-WebSocket'); strictEqual(xmlData.project.dependencies[0].dependency[1].version, '2.3.1'); }); @@ -119,8 +104,7 @@ describe('test pom parsing', () => { }, }; - const pomDependenciesWithVersions = - await getPomDependenciesWithVersions(parsedPom); + const pomDependenciesWithVersions = await getPomDependenciesWithVersions(parsedPom); strictEqual(pomDependenciesWithVersions.length, 1); strictEqual(pomDependenciesWithVersions[0].artifactId, 'Java-WebSocket'); @@ -133,8 +117,7 @@ describe('test pom parsing', () => { project: [], }; - const pomDependenciesWithVersions = - await getPomDependenciesWithVersions(parsedPom); + const pomDependenciesWithVersions = await getPomDependenciesWithVersions(parsedPom); strictEqual(pomDependenciesWithVersions.length, 0); });