Skip to content

Commit c18a5fd

Browse files
committed
refactor(node): Split isApplicable() out of filterApplicable()
Make the `filterApplicable()` function more readable. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent bca9510 commit c18a5fd

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

plugins/package-managers/node/src/main/kotlin/NodePackageManagerDetection.kt

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,43 +67,55 @@ internal class NodePackageManagerDetection(private val definitionFiles: Collecti
6767
patterns.any { it.matches(projectDir.toPath()) }
6868
}.keys
6969

70+
/**
71+
* Return whether the given [manager] is applicable for then given [definitionFile], or return null if unknown.
72+
*/
73+
fun isApplicable(manager: NodePackageManagerType, definitionFile: File): Boolean? {
74+
val projectDir = definitionFile.parentFile
75+
76+
// Try to clearly determine the package manager from files specific to it.
77+
val managersFromFiles = projectDirManagers[projectDir].orEmpty()
78+
when {
79+
manager !in managersFromFiles -> return false
80+
managersFromFiles.size == 1 -> {
81+
logger.info { "Detected '$definitionFile' to be the root of a(n) $manager project." }
82+
return true
83+
}
84+
}
85+
86+
// There is ambiguity when only looking at the files, so also look at any workspaces to clearly determine
87+
// the package manager.
88+
val managersFromWorkspaces = getWorkspaceRoots(projectDir).mapNotNull {
89+
projectDirManagers[it]
90+
}.flatten()
91+
92+
if (managersFromWorkspaces.isNotEmpty()) {
93+
logger.info {
94+
"Skipping '$definitionFile' as it is part of a workspace implicitly handled by $managersFromWorkspaces."
95+
}
96+
97+
return false
98+
}
99+
100+
return null
101+
}
102+
70103
/**
71104
* Return those [definitionFiles] that define root projects for the given [manager].
72105
*/
73106
fun filterApplicable(manager: NodePackageManagerType): List<File> =
74107
definitionFiles.filter { file ->
75108
val projectDir = file.parentFile
76-
77-
// Try to clearly determine the package manager from files specific to it.
78109
val managersFromFiles = projectDirManagers[projectDir].orEmpty()
79-
when {
80-
manager !in managersFromFiles -> return@filter false
81-
managersFromFiles.size == 1 -> {
82-
logger.info { "Detected '$file' to be the root of a(n) $manager project." }
83-
return@filter true
84-
}
85-
}
86110

87-
// There is ambiguity when only looking at the files, so also look at any workspaces to clearly determine
88-
// the package manager.
89-
val managersFromWorkspaces = getWorkspaceRoots(projectDir).mapNotNull {
90-
projectDirManagers[it]
91-
}.flatten()
92-
93-
if (managersFromWorkspaces.isNotEmpty()) {
94-
logger.info {
95-
"Skipping '$file' as it is part of a workspace implicitly handled by $managersFromWorkspaces."
111+
isApplicable(manager, file) ?: run {
112+
// Looking at the workspaces did not bring any clarity, so assume the package manager is NPM.
113+
logger.warn {
114+
"Any of $managersFromFiles could be the package manager for '$file'. Assuming it is an NPM project."
96115
}
97116

98-
return@filter false
99-
}
100-
101-
// Looking at the workspaces did not bring any clarity, so assume the package manager is NPM.
102-
logger.warn {
103-
"Any of $managersFromFiles could be the package manager for '$file'. Assuming it is an NPM project."
117+
manager == NodePackageManagerType.NPM
104118
}
105-
106-
manager == NodePackageManagerType.NPM
107119
}
108120
}
109121

0 commit comments

Comments
 (0)