Skip to content

Commit

Permalink
If multiple candidates, sort closer directories first
Browse files Browse the repository at this point in the history
  • Loading branch information
bitspittle committed Oct 16, 2024
1 parent ea89ff5 commit 93080ba
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,21 @@ fun Session.findKobwebApplication(basePath: Path): KobwebApplication? {


val foundPath: Path? = if (!KobwebFolder.isFoundIn(basePath)) {
// Frustratingly, both walkTopDown and walkButtomUp seem to visit directories in the same order, whereas we want
// folders closer to us (e.g. "site") to be recommended over folders further away (e.g. "subdir/site").
// So we'll sort by depth ourselves.
fun Sequence<Path>.sortedByDepth(): Sequence<Path> {
return sortedBy { p ->
p.relativeToCurrentDirectoryOrBasePath().toString().count { it == '/' || it == '\\' }
}
}

val candidates = try {
basePath.toFile().walkTopDown().maxDepth(2)
.filter { it.isDirectory && KobwebFolder.isFoundIn(it.toPath()) }
basePath.toFile().walk().maxDepth(2)
.filter { it.isDirectory }
.map { it.toPath() }
.filter { KobwebFolder.isFoundIn(it) }
.sortedByDepth()
.toList()
} catch(ex: Exception) {
// If this happens, we definitely don't have access to projects to recommend to users.
Expand Down

0 comments on commit 93080ba

Please sign in to comment.