eslint-plugin-export-scope
Version:
Don't leak LOCAL utils, states, components into the global scope
79 lines • 3.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSubPath = exports.getFullScopePath = exports.getRootDir = exports.getPathOfTheNearestConfig = exports.getFileTree = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const checkIsImportable_1 = require("./checkIsImportable");
const getFileTree = (dir, extensions = [".ts", ".tsx", ".mts", ".js", ".jsx", "mjs"]) => {
const extSet = new Set(extensions);
const filePaths = [];
const dirPaths = [];
const traverse = (dir) => {
const entries = (0, fs_1.readdirSync)(dir, { withFileTypes: true });
entries.map((x) => {
if ([checkIsImportable_1.SCOPE_TS_FILE_NAME, checkIsImportable_1.SCOPE_JS_FILE_NAME].includes(x.name))
return;
if (x.name === "node_modules" || x.name.startsWith("."))
return;
const path = (0, path_1.resolve)(dir, x.name);
if (x.isDirectory()) {
dirPaths.push(path);
return traverse(path);
}
else {
if (extSet.has((0, path_1.extname)(x.name))) {
filePaths.push(path);
}
}
});
};
traverse(dir);
return { filePaths, dirPaths };
};
exports.getFileTree = getFileTree;
const nearestConfigMap = new Map();
const getPathOfTheNearestConfig = (originPath, configFileName) => {
const configFileNames = Array.isArray(configFileName) ? configFileName : [configFileName];
const key = [originPath, configFileNames.join("_")].join("_");
if (nearestConfigMap.has(key)) {
return nearestConfigMap.get(key);
}
const cacheResult = (result) => {
nearestConfigMap.set(key, result);
// clear cache after 1 second
setTimeout(() => nearestConfigMap.delete(key), 1000);
return result;
};
let currentDir = originPath;
while (currentDir !== "/") {
const fileNames = (0, fs_1.readdirSync)(currentDir);
const fileName = fileNames.find((x) => configFileNames.includes(x));
if (fileName) {
return cacheResult((0, path_1.resolve)(currentDir, fileName));
}
if (fileNames.includes("package.json")) {
return cacheResult(null);
}
currentDir = (0, path_1.dirname)(currentDir);
}
return cacheResult(null);
};
exports.getPathOfTheNearestConfig = getPathOfTheNearestConfig;
const getRootDir = (originPath) => {
const configPath = (0, exports.getPathOfTheNearestConfig)(originPath, "package.json");
return configPath ? (0, path_1.dirname)(configPath) : null;
};
exports.getRootDir = getRootDir;
const getFullScopePath = (exportDir, scope) => {
if (scope.startsWith(".")) {
return (0, path_1.resolve)(exportDir, scope);
}
const rootDir = (0, exports.getRootDir)(exportDir);
if (!rootDir)
return null;
return (0, path_1.resolve)(rootDir, scope);
};
exports.getFullScopePath = getFullScopePath;
const isSubPath = (path1, path2) => !(0, path_1.relative)(path1.toLowerCase(), path2.toLowerCase()).startsWith(".");
exports.isSubPath = isSubPath;
//# sourceMappingURL=utils.js.map
;