tslint-folders
Version:
Custom TSLint rules for checking imports between packages and their folders, and generating relevant diagrams.
55 lines (54 loc) • 2.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.TsConfigParser = void 0;
var path = require("path");
var ts = require("typescript");
var fs_1 = require("fs");
var cache = {};
var TsConfigParser;
(function (TsConfigParser) {
// Parse config in given folder, or some parent folder
function parseConfigNear(thisPath) {
var basePath = path.resolve(path.dirname(thisPath));
while ((0, fs_1.existsSync)(basePath)) {
var possibleTsConfigPath = path.join(basePath, "tsconfig.json");
if (cache[possibleTsConfigPath]) {
return cache[possibleTsConfigPath];
}
if ((0, fs_1.existsSync)(possibleTsConfigPath)) {
var config = loadTsConfig(possibleTsConfigPath, basePath);
cache[possibleTsConfigPath] = config;
return config;
}
basePath = path.join(basePath, "..");
}
throw new Error("Cannot find a tsconfig.json - it should be in some parent directory of all TypeScript files.");
}
TsConfigParser.parseConfigNear = parseConfigNear;
var loadTsConfig = function (tsconfigPath, tsconfigDir) {
var _a = parseTsConfig(tsconfigPath), baseUrl = _a.baseUrl, paths = _a.paths, include = _a.include;
var resolvedBaseUrl = path.resolve(path.join(tsconfigDir, baseUrl));
var includeOrDefault = include || ["."];
return { baseUrl: resolvedBaseUrl, paths: paths, include: includeOrDefault };
};
var parseTsConfig = function (tsconfigPath) {
var _a, _b, _c, _d, _e;
var basePath = path.resolve(path.dirname(tsconfigPath));
try {
var parseJsonResult = ts.parseConfigFileTextToJson(tsconfigPath, (0, fs_1.readFileSync)(tsconfigPath, { encoding: "utf8" }));
if (parseJsonResult.error)
throw parseJsonResult.error;
var result = ts.parseJsonConfigFileContent(parseJsonResult.config, ts.sys, basePath);
if (result.errors.length)
throw result.errors;
return {
baseUrl: ((_b = (_a = result.raw) === null || _a === void 0 ? void 0 : _a.compilerOptions) === null || _b === void 0 ? void 0 : _b.baseUrl) || ".",
paths: (_d = (_c = result.raw) === null || _c === void 0 ? void 0 : _c.compilerOptions) === null || _d === void 0 ? void 0 : _d.paths,
include: (_e = result.raw) === null || _e === void 0 ? void 0 : _e.include
};
}
catch (e) {
throw new Error("Cannot parse '".concat(tsconfigPath, "'. ").concat(JSON.stringify(e)));
}
};
})(TsConfigParser = exports.TsConfigParser || (exports.TsConfigParser = {}));
;