Skip to content

Commit

Permalink
refactor(npm): Use a simpler return type for two functions
Browse files Browse the repository at this point in the history
Returning a `Pair` or `Map` respectively is not needed.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Sep 12, 2024
1 parent fcfab20 commit 8b45010
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions plugins/package-managers/node/src/main/kotlin/Npm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ open class Npm(
// Create packages for all modules found in the workspace and add them to the graph builder. They are
// reused when they are referenced by scope dependencies.
val packages = parseInstalledModules(workingDir)
graphBuilder.addPackages(packages.values)
graphBuilder.addPackages(packages)

val scopeNames = setOfNotNull(
// Optional dependencies are just like regular dependencies except that NPM ignores failures when
Expand Down Expand Up @@ -232,7 +232,7 @@ open class Npm(
)
}

private fun parseInstalledModules(rootDirectory: File): Map<String, Package> {
private fun parseInstalledModules(rootDirectory: File): List<Package> {
val nodeModulesDir = rootDirectory.resolve("node_modules")

logger.info { "Searching for 'package.json' files in '$nodeModulesDir'..." }
Expand All @@ -250,19 +250,19 @@ open class Npm(
nodeModulesFiles.mapTo(mutableListOf()) { file ->
logger.debug { "Starting to parse '$file'..." }
async {
parsePackage(rootDirectory, file).also { (id, _) ->
logger.debug { "Finished parsing '$file' to '$id'." }
parsePackage(rootDirectory, file).also { pkg ->
logger.debug { "Finished parsing '$file' to '${pkg.id}'." }
}
}
}.awaitAll().toMap()
}.awaitAll().distinctBy { it.id }
}
}

/**
* Construct a [Package] by parsing its _package.json_ file and - if applicable - querying additional
* content via the `npm view` command. The result is a [Pair] with the raw identifier and the new package.
*/
internal suspend fun parsePackage(workingDir: File, packageJsonFile: File): Pair<String, Package> {
internal suspend fun parsePackage(workingDir: File, packageJsonFile: File): Package {
val packageDir = packageJsonFile.parentFile

logger.debug { "Found a 'package.json' file in '$packageDir'." }
Expand Down Expand Up @@ -365,7 +365,7 @@ open class Npm(
"Generated package info for '${id.toCoordinates()}' has no version."
}

return Pair(id.toCoordinates(), module)
return module
}

private suspend fun getRemotePackageDetailsAsync(workingDir: File, packageName: String): Deferred<PackageJson> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ internal class NpmDependencyHandler(private val npm: Npm) : DependencyHandler<Np

override fun createPackage(dependency: NpmModuleInfo, issues: MutableCollection<Issue>): Package =
runBlocking {
npm.parsePackage(dependency.workingDir, dependency.packageFile).second
npm.parsePackage(dependency.workingDir, dependency.packageFile)
}
}

0 comments on commit 8b45010

Please sign in to comment.