tsconfig-loader.js
4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadTsconfig = exports.walkForTsConfig = exports.tsConfigLoader = void 0;
var path = require("path");
var fs = require("fs");
// tslint:disable:no-require-imports
var JSON5 = require("json5");
var StripBom = require("strip-bom");
function tsConfigLoader(_a) {
var getEnv = _a.getEnv, cwd = _a.cwd, _b = _a.loadSync, loadSync = _b === void 0 ? loadSyncDefault : _b;
var TS_NODE_PROJECT = getEnv("TS_NODE_PROJECT");
var TS_NODE_BASEURL = getEnv("TS_NODE_BASEURL");
// tsconfig.loadSync handles if TS_NODE_PROJECT is a file or directory
// and also overrides baseURL if TS_NODE_BASEURL is available.
var loadResult = loadSync(cwd, TS_NODE_PROJECT, TS_NODE_BASEURL);
return loadResult;
}
exports.tsConfigLoader = tsConfigLoader;
function loadSyncDefault(cwd, filename, baseUrl) {
// Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename
var configPath = resolveConfigPath(cwd, filename);
if (!configPath) {
return {
tsConfigPath: undefined,
baseUrl: undefined,
paths: undefined,
};
}
var config = loadTsconfig(configPath);
return {
tsConfigPath: configPath,
baseUrl: baseUrl ||
(config && config.compilerOptions && config.compilerOptions.baseUrl),
paths: config && config.compilerOptions && config.compilerOptions.paths,
};
}
function resolveConfigPath(cwd, filename) {
if (filename) {
var absolutePath = fs.lstatSync(filename).isDirectory()
? path.resolve(filename, "./tsconfig.json")
: path.resolve(cwd, filename);
return absolutePath;
}
if (fs.statSync(cwd).isFile()) {
return path.resolve(cwd);
}
var configAbsolutePath = walkForTsConfig(cwd);
return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
}
function walkForTsConfig(directory, existsSync) {
if (existsSync === void 0) { existsSync = fs.existsSync; }
var configPath = path.join(directory, "./tsconfig.json");
if (existsSync(configPath)) {
return configPath;
}
var parentDirectory = path.join(directory, "../");
// If we reached the top
if (directory === parentDirectory) {
return undefined;
}
return walkForTsConfig(parentDirectory, existsSync);
}
exports.walkForTsConfig = walkForTsConfig;
function loadTsconfig(configFilePath, existsSync, readFileSync) {
if (existsSync === void 0) { existsSync = fs.existsSync; }
if (readFileSync === void 0) { readFileSync = function (filename) {
return fs.readFileSync(filename, "utf8");
}; }
if (!existsSync(configFilePath)) {
return undefined;
}
var configString = readFileSync(configFilePath);
var cleanedJson = StripBom(configString);
var config = JSON5.parse(cleanedJson);
var extendedConfig = config.extends;
if (extendedConfig) {
if (typeof extendedConfig === "string" &&
extendedConfig.indexOf(".json") === -1) {
extendedConfig += ".json";
}
var currentDir = path.dirname(configFilePath);
var extendedConfigPath = path.join(currentDir, extendedConfig);
if (extendedConfig.indexOf("/") !== -1 &&
extendedConfig.indexOf(".") !== -1 &&
!existsSync(extendedConfigPath)) {
extendedConfigPath = path.join(currentDir, "node_modules", extendedConfig);
}
var base = loadTsconfig(extendedConfigPath, existsSync, readFileSync) || {};
// baseUrl should be interpreted as relative to the base tsconfig,
// but we need to update it so it is relative to the original tsconfig being loaded
if (base.compilerOptions && base.compilerOptions.baseUrl) {
var extendsDir = path.dirname(extendedConfig);
base.compilerOptions.baseUrl = path.join(extendsDir, base.compilerOptions.baseUrl);
}
return __assign(__assign(__assign({}, base), config), { compilerOptions: __assign(__assign({}, base.compilerOptions), config.compilerOptions) });
}
return config;
}
exports.loadTsconfig = loadTsconfig;
//# sourceMappingURL=tsconfig-loader.js.map