Files
meteor/lib/util/getProjectRootPaths.js
Dominik Ferber 85bb02e317 refactor(): support matching Meteor projects downwards
Until now the path was only checked upwards for Meteor projects. Now it checks all children recursively. This enables having one common eslint configuration above multiple child directories containing Meteor applications.
2015-10-01 23:01:18 +02:00

49 lines
1.1 KiB
JavaScript

import path from 'path'
var walk = require('walkdir')
function findOneUpwards (currentDirectory, matcher, attempts = 0) {
// No folder with '.meteor/release' in it found
if (attempts > 50 || currentDirectory === path.sep) {
return false
}
if (matcher(currentDirectory)) {
return currentDirectory
}
return findOneUpwards(path.join(currentDirectory, '..'), matcher, attempts + 1)
}
function findAllDownwards (startPath, matcher) {
const matchedPaths = []
const options = {follow_symlinks: false, no_recurse: false, max_depth: 20}
walk.sync(
startPath,
options,
function (currentPath) {
if (matcher(currentPath)) {
matchedPaths.push(currentPath)
this.ignore(currentPath)
}
}
)
return matchedPaths
}
export default function getProjectRootPaths (currentDirectory, matcher) {
if (matcher(currentDirectory)) {
return [currentDirectory]
}
const upwards = findOneUpwards(currentDirectory, matcher)
if (upwards) {
return [upwards]
}
return findAllDownwards(currentDirectory, matcher)
}