eslint-plugin-canonical
Version:
Canonical linting rules for ESLint.
35 lines (34 loc) • 1.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findDirectory = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = __importDefault(require("node:path"));
const createDirectoryFinder = () => {
const cache = {};
return (startPath, needleFileName, rootPath, allowList = null) => {
const key = JSON.stringify({
allowList,
needleFileName,
rootPath,
startPath,
});
if (cache[key] !== undefined) {
return cache[key];
}
let currentDirectory = node_path_1.default.resolve(startPath, './');
while (currentDirectory.startsWith(rootPath)) {
if ((0, node_fs_1.existsSync)(node_path_1.default.resolve(currentDirectory, needleFileName)) &&
(allowList === null || allowList.includes(currentDirectory))) {
cache[key] = currentDirectory;
return currentDirectory;
}
currentDirectory = node_path_1.default.resolve(currentDirectory, '..');
}
cache[key] = null;
return null;
};
};
exports.findDirectory = createDirectoryFinder();