| 1 | "use strict";
|
|---|
| 2 | var __assign = (this && this.__assign) || function () {
|
|---|
| 3 | __assign = Object.assign || function(t) {
|
|---|
| 4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|---|
| 5 | s = arguments[i];
|
|---|
| 6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|---|
| 7 | t[p] = s[p];
|
|---|
| 8 | }
|
|---|
| 9 | return t;
|
|---|
| 10 | };
|
|---|
| 11 | return __assign.apply(this, arguments);
|
|---|
| 12 | };
|
|---|
| 13 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 14 | exports.loadTsconfig = exports.walkForTsConfig = exports.tsConfigLoader = void 0;
|
|---|
| 15 | var path = require("path");
|
|---|
| 16 | var fs = require("fs");
|
|---|
| 17 | // tslint:disable:no-require-imports
|
|---|
| 18 | var JSON5 = require("json5");
|
|---|
| 19 | var StripBom = require("strip-bom");
|
|---|
| 20 | function tsConfigLoader(_a) {
|
|---|
| 21 | var getEnv = _a.getEnv, cwd = _a.cwd, _b = _a.loadSync, loadSync = _b === void 0 ? loadSyncDefault : _b;
|
|---|
| 22 | var TS_NODE_PROJECT = getEnv("TS_NODE_PROJECT");
|
|---|
| 23 | var TS_NODE_BASEURL = getEnv("TS_NODE_BASEURL");
|
|---|
| 24 | // tsconfig.loadSync handles if TS_NODE_PROJECT is a file or directory
|
|---|
| 25 | // and also overrides baseURL if TS_NODE_BASEURL is available.
|
|---|
| 26 | var loadResult = loadSync(cwd, TS_NODE_PROJECT, TS_NODE_BASEURL);
|
|---|
| 27 | return loadResult;
|
|---|
| 28 | }
|
|---|
| 29 | exports.tsConfigLoader = tsConfigLoader;
|
|---|
| 30 | function loadSyncDefault(cwd, filename, baseUrl) {
|
|---|
| 31 | // Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename
|
|---|
| 32 | var configPath = resolveConfigPath(cwd, filename);
|
|---|
| 33 | if (!configPath) {
|
|---|
| 34 | return {
|
|---|
| 35 | tsConfigPath: undefined,
|
|---|
| 36 | baseUrl: undefined,
|
|---|
| 37 | paths: undefined,
|
|---|
| 38 | };
|
|---|
| 39 | }
|
|---|
| 40 | var config = loadTsconfig(configPath);
|
|---|
| 41 | return {
|
|---|
| 42 | tsConfigPath: configPath,
|
|---|
| 43 | baseUrl: baseUrl ||
|
|---|
| 44 | (config && config.compilerOptions && config.compilerOptions.baseUrl),
|
|---|
| 45 | paths: config && config.compilerOptions && config.compilerOptions.paths,
|
|---|
| 46 | };
|
|---|
| 47 | }
|
|---|
| 48 | function resolveConfigPath(cwd, filename) {
|
|---|
| 49 | if (filename) {
|
|---|
| 50 | var absolutePath = fs.lstatSync(filename).isDirectory()
|
|---|
| 51 | ? path.resolve(filename, "./tsconfig.json")
|
|---|
| 52 | : path.resolve(cwd, filename);
|
|---|
| 53 | return absolutePath;
|
|---|
| 54 | }
|
|---|
| 55 | if (fs.statSync(cwd).isFile()) {
|
|---|
| 56 | return path.resolve(cwd);
|
|---|
| 57 | }
|
|---|
| 58 | var configAbsolutePath = walkForTsConfig(cwd);
|
|---|
| 59 | return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
|
|---|
| 60 | }
|
|---|
| 61 | function walkForTsConfig(directory, existsSync) {
|
|---|
| 62 | if (existsSync === void 0) { existsSync = fs.existsSync; }
|
|---|
| 63 | var configPath = path.join(directory, "./tsconfig.json");
|
|---|
| 64 | if (existsSync(configPath)) {
|
|---|
| 65 | return configPath;
|
|---|
| 66 | }
|
|---|
| 67 | var parentDirectory = path.join(directory, "../");
|
|---|
| 68 | // If we reached the top
|
|---|
| 69 | if (directory === parentDirectory) {
|
|---|
| 70 | return undefined;
|
|---|
| 71 | }
|
|---|
| 72 | return walkForTsConfig(parentDirectory, existsSync);
|
|---|
| 73 | }
|
|---|
| 74 | exports.walkForTsConfig = walkForTsConfig;
|
|---|
| 75 | function loadTsconfig(configFilePath, existsSync, readFileSync) {
|
|---|
| 76 | if (existsSync === void 0) { existsSync = fs.existsSync; }
|
|---|
| 77 | if (readFileSync === void 0) { readFileSync = function (filename) {
|
|---|
| 78 | return fs.readFileSync(filename, "utf8");
|
|---|
| 79 | }; }
|
|---|
| 80 | if (!existsSync(configFilePath)) {
|
|---|
| 81 | return undefined;
|
|---|
| 82 | }
|
|---|
| 83 | var configString = readFileSync(configFilePath);
|
|---|
| 84 | var cleanedJson = StripBom(configString);
|
|---|
| 85 | var config;
|
|---|
| 86 | try {
|
|---|
| 87 | config = JSON5.parse(cleanedJson);
|
|---|
| 88 | }
|
|---|
| 89 | catch (e) {
|
|---|
| 90 | throw new Error("".concat(configFilePath, " is malformed ").concat(e.message));
|
|---|
| 91 | }
|
|---|
| 92 | var extendedConfig = config.extends;
|
|---|
| 93 | if (extendedConfig) {
|
|---|
| 94 | var base = void 0;
|
|---|
| 95 | if (Array.isArray(extendedConfig)) {
|
|---|
| 96 | base = extendedConfig.reduce(function (currBase, extendedConfigElement) {
|
|---|
| 97 | return mergeTsconfigs(currBase, loadTsconfigFromExtends(configFilePath, extendedConfigElement, existsSync, readFileSync));
|
|---|
| 98 | }, {});
|
|---|
| 99 | }
|
|---|
| 100 | else {
|
|---|
| 101 | base = loadTsconfigFromExtends(configFilePath, extendedConfig, existsSync, readFileSync);
|
|---|
| 102 | }
|
|---|
| 103 | return mergeTsconfigs(base, config);
|
|---|
| 104 | }
|
|---|
| 105 | return config;
|
|---|
| 106 | }
|
|---|
| 107 | exports.loadTsconfig = loadTsconfig;
|
|---|
| 108 | /**
|
|---|
| 109 | * Intended to be called only from loadTsconfig.
|
|---|
| 110 | * Parameters don't have defaults because they should use the same as loadTsconfig.
|
|---|
| 111 | */
|
|---|
| 112 | function loadTsconfigFromExtends(configFilePath, extendedConfigValue,
|
|---|
| 113 | // eslint-disable-next-line no-shadow
|
|---|
| 114 | existsSync, readFileSync) {
|
|---|
| 115 | var _a;
|
|---|
| 116 | if (typeof extendedConfigValue === "string" &&
|
|---|
| 117 | extendedConfigValue.indexOf(".json") === -1) {
|
|---|
| 118 | extendedConfigValue += ".json";
|
|---|
| 119 | }
|
|---|
| 120 | var currentDir = path.dirname(configFilePath);
|
|---|
| 121 | var extendedConfigPath = path.join(currentDir, extendedConfigValue);
|
|---|
| 122 | if (extendedConfigValue.indexOf("/") !== -1 &&
|
|---|
| 123 | extendedConfigValue.indexOf(".") !== -1 &&
|
|---|
| 124 | !existsSync(extendedConfigPath)) {
|
|---|
| 125 | extendedConfigPath = path.join(currentDir, "node_modules", extendedConfigValue);
|
|---|
| 126 | }
|
|---|
| 127 | var config = loadTsconfig(extendedConfigPath, existsSync, readFileSync) || {};
|
|---|
| 128 | // baseUrl should be interpreted as relative to extendedConfigPath,
|
|---|
| 129 | // but we need to update it so it is relative to the original tsconfig being loaded
|
|---|
| 130 | if ((_a = config.compilerOptions) === null || _a === void 0 ? void 0 : _a.baseUrl) {
|
|---|
| 131 | var extendsDir = path.dirname(extendedConfigValue);
|
|---|
| 132 | config.compilerOptions.baseUrl = path.join(extendsDir, config.compilerOptions.baseUrl);
|
|---|
| 133 | }
|
|---|
| 134 | return config;
|
|---|
| 135 | }
|
|---|
| 136 | function mergeTsconfigs(base, config) {
|
|---|
| 137 | base = base || {};
|
|---|
| 138 | config = config || {};
|
|---|
| 139 | return __assign(__assign(__assign({}, base), config), { compilerOptions: __assign(__assign({}, base.compilerOptions), config.compilerOptions) });
|
|---|
| 140 | }
|
|---|
| 141 | //# sourceMappingURL=tsconfig-loader.js.map |
|---|