eslint-plugin-path
Version:
An ESLint plugin for enforcing consistent imports across project. In other words, it helps to replace all relatives import with absolutes dependinng on settings.
70 lines • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearMatcher = clearMatcher;
exports.getAliasItemCreator = getAliasItemCreator;
exports.getConfigSettings = getConfigSettings;
const path_1 = require("path");
const constants_1 = require("./constants");
const helpers_1 = require("./helpers");
/**
* Helps to clear matchers' key or value
* @param value - The value to clear
* @returns The cleared tsconfig path matcher value
*/
function clearMatcher(value) {
return value.replace("*", "");
}
/**
* Helps to create alias item creator based on package dir
* @param packageDir - The package directory
* @returns Returns alias item creator
*/
function getAliasItemCreator(packageDir) {
return function createAliasItem(path, alias = null) {
return {
path: (0, path_1.isAbsolute)(path) ? clearMatcher(path) : (0, path_1.join)(packageDir, clearMatcher(path)),
alias: alias ? clearMatcher(alias) : null,
};
};
}
const CONFIG_CACHE = new Map();
/**
* Creates alias items that were described in tsconfig.json
*
* @param {string} packagePath - The package path
* @param {ConfigSettings} settings - The config settings
* @returns Returns array of alias items
*/
function getConfigSettings(packagePath, settings) {
let fileName = null;
if ((0, helpers_1.isFileExists)(packagePath, constants_1.FILES.tsconfig)) {
fileName = constants_1.FILES.tsconfig;
}
if ((0, helpers_1.isFileExists)(packagePath, constants_1.FILES.jsconfig)) {
fileName = constants_1.FILES.jsconfig;
}
if ((settings === null || settings === void 0 ? void 0 : settings.config) && (0, helpers_1.isFileExists)(packagePath, settings === null || settings === void 0 ? void 0 : settings.config)) {
fileName = settings === null || settings === void 0 ? void 0 : settings.config;
}
if (fileName === null) {
return [];
}
const urls = [];
const key = `${packagePath}/${fileName}`;
const cached = CONFIG_CACHE.get(key);
if (cached) {
return cached;
}
const config = (0, helpers_1.loadConfigFile)(packagePath, fileName);
const createAliasItem = getAliasItemCreator(packagePath);
if ((0, helpers_1.isPathExists)(config, "data.compilerOptions.paths")) {
const entires = Object.entries(config.data.compilerOptions.paths);
entires.forEach(([key, paths]) => paths.forEach((path) => urls.push(createAliasItem(path, key))));
}
if ((0, helpers_1.isPathExists)(config, "data.compilerOptions.baseUrl")) {
urls.push(createAliasItem(config.data.compilerOptions.baseUrl));
}
CONFIG_CACHE.set(key, urls);
return urls;
}
//# sourceMappingURL=config.js.map