-
Notifications
You must be signed in to change notification settings - Fork 251
/
extract-ts-config.js
73 lines (65 loc) · 1.95 KB
/
extract-ts-config.js
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
const path = require("path");
const tryRequire = require("semver-try-require");
const { supportedTranspilers } = require("../../src/meta.js");
const typescript = tryRequire("typescript", supportedTranspilers.typescript);
const FORMAT_DIAGNOSTICS_HOST = {
getCanonicalFileName(pFileName) {
let lReturnValue = pFileName.toLowerCase();
// depends on the platform which branch is taken, hence the c8 ignore
/* c8 ignore start */
if (typescript?.sys?.useCaseSensitiveFileNames ?? false) {
lReturnValue = pFileName;
}
/* c8 ignore stop */
return lReturnValue;
},
getCurrentDirectory() {
return process.cwd();
},
getNewLine() {
return "\n";
},
};
/**
* Reads the file with name `pTSConfigFileName` and returns its parsed
* contents as an object
*
* Silently fails if a supported version of the typescript compiler isn't available
*
* @param {string} pTSConfigFileName
* @return {any} tsconfig as an object
* @throws {Error} when the tsconfig is invalid/ jas errors
* @throws {TypeError} when the tsconfig is unreadable
*/
module.exports = function extractTSConfig(pTSConfigFileName) {
let lReturnValue = {};
if (typescript) {
const lConfig = typescript.readConfigFile(
pTSConfigFileName,
typescript.sys.readFile
);
if (typeof lConfig.error !== "undefined") {
throw new TypeError(
typescript.formatDiagnostics([lConfig.error], FORMAT_DIAGNOSTICS_HOST)
);
}
lReturnValue = typescript.parseJsonConfigFileContent(
lConfig.config,
typescript.sys,
path.dirname(path.resolve(pTSConfigFileName)),
{},
pTSConfigFileName
);
if (lReturnValue.errors.length > 0) {
throw new Error(
typescript.formatDiagnostics(
lReturnValue.errors,
FORMAT_DIAGNOSTICS_HOST
)
);
}
// lRetval.fileNames; // all files included in the project
// lRetval.options; // CompilerOptions
}
return lReturnValue;
};