UNPKG

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.

55 lines 1.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isPathExists = isPathExists; exports.loadConfigFile = loadConfigFile; exports.isFileExists = isFileExists; const path_1 = require("path"); const fs_1 = require("fs"); const load_tsconfig_1 = require("load-tsconfig"); /** * Helps to detect if a path exists or not * @param map - The object to check the path in * @param path - The path to check * @returns True if the path exists, false otherwise */ function isPathExists(map, path) { let pathToTarget = map; for (const step of path.split(".")) { pathToTarget = pathToTarget === null || pathToTarget === void 0 ? void 0 : pathToTarget[step]; if (pathToTarget === undefined) { return false; } } return true; } /** * Loads and parses JSON files * @param dir - The directory where the file is located * @param filename - The name of the file to load * @returns The parsed JSON file */ function loadConfigFile(dir, filename) { try { const path = (0, path_1.join)(dir, filename); if (filename === "jsconfig.json") { return { data: JSON.parse((0, fs_1.readFileSync)(path, "utf8")), }; } const file = (0, load_tsconfig_1.loadTsConfig)(dir, filename); return file; } catch (error) { throw new Error(`'${filename}' is invalid. Please, validate JSON file.`); } } /** * Helps to check if a file exists * @param dir - The directory where the file might be located * @param filename - The name of the file to check * @returns True if the file exists, false otherwise */ function isFileExists(dir, filename) { return (0, fs_1.existsSync)((0, path_1.join)(dir, filename)); } //# sourceMappingURL=helpers.js.map