|
1 | | -const ts = require('typescript'); |
2 | 1 | const { join, dirname, resolve } = require('path'); |
3 | 2 | const fs = require('fs'); |
4 | | -const { paramCase } = require('param-case'); |
| 3 | +const { parse } = require('tsconfck'); |
| 4 | + |
| 5 | +const DEFAULT_TSCONFIG_OPTIONS = { |
| 6 | + compilerOptions: {} |
| 7 | +}; |
5 | 8 |
|
6 | 9 | /** |
7 | 10 | * @typedef {object} LoadTsconfigInit |
@@ -31,107 +34,22 @@ function walkParentDirs({ base, start, filename }) { |
31 | 34 | return null; |
32 | 35 | } |
33 | 36 |
|
34 | | -/** |
35 | | - * @param {ts.CompilerOptions} options |
36 | | - * @param {string | undefined} key |
37 | | - * @param {(value: string) => string} [callback] |
38 | | - * @returns {string | undefined} |
39 | | - */ |
40 | | -function convertEnumCompilerOptions(enumCompilerOptions, key, callback) { |
41 | | - if (key == null) { |
42 | | - return undefined; |
43 | | - } |
44 | | - const value = enumCompilerOptions[key]; |
45 | | - return typeof callback === 'function' ? callback(value) : value; |
46 | | -} |
47 | | - |
48 | | -/** |
49 | | - * @param {string} value |
50 | | - * @returns {string} |
51 | | - */ |
52 | | -function toLowerCase(value) { |
53 | | - return value.toLowerCase(); |
54 | | -} |
55 | | - |
56 | | -/** |
57 | | - * @param {ts.NewLineKind} newLine |
58 | | - * @returns {string | undefined} |
59 | | - */ |
60 | | -function normalizeNewLineOption(newLine) { |
61 | | - switch (newLine) { |
62 | | - case ts.NewLineKind.CarriageReturnLineFeed: |
63 | | - return 'crlf'; |
64 | | - case ts.NewLineKind.LineFeed: |
65 | | - return 'lf'; |
66 | | - default: |
67 | | - return undefined; |
68 | | - } |
69 | | -} |
70 | | - |
71 | | -/** |
72 | | - * @param {ts.ModuleResolutionKind} moduleResolution |
73 | | - * @returns {string | undefined} |
74 | | - */ |
75 | | -function normalizeModuleResolutionOption(moduleResolution) { |
76 | | - switch (moduleResolution) { |
77 | | - case ts.ModuleResolutionKind.Classic: |
78 | | - return 'classic'; |
79 | | - case ts.ModuleResolutionKind.NodeJs: |
80 | | - return 'node'; |
81 | | - case ts.ModuleResolutionKind.Node12: |
82 | | - return 'node12'; |
83 | | - case ts.ModuleResolutionKind.NodeNext: |
84 | | - return 'nodenext'; |
85 | | - default: |
86 | | - return undefined; |
87 | | - } |
88 | | -} |
89 | | - |
90 | | -/** |
91 | | - * @param {ts.CompilerOptions} options |
92 | | - * @returns {ts.CompilerOptions} |
93 | | - */ |
94 | | -function normalizeCompilerOptions(options) { |
95 | | - if (options.importsNotUsedAsValues != null) { |
96 | | - options.importsNotUsedAsValues = convertEnumCompilerOptions( |
97 | | - ts.ImportsNotUsedAsValues, |
98 | | - options.importsNotUsedAsValues, |
99 | | - toLowerCase, |
100 | | - ); |
101 | | - } |
102 | | - if (options.jsx != null) { |
103 | | - options.jsx = convertEnumCompilerOptions(ts.JsxEmit, options.jsx, paramCase); |
104 | | - } |
105 | | - if (options.module != null) { |
106 | | - options.module = convertEnumCompilerOptions(ts.ModuleKind, options.module, toLowerCase); |
107 | | - } |
108 | | - if (options.moduleResolution != null) { |
109 | | - options.moduleResolution = normalizeModuleResolutionOption(options.moduleResolution); |
110 | | - } |
111 | | - if (options.newLine != null) { |
112 | | - options.newLine = normalizeNewLineOption(options.newLine); |
113 | | - } |
114 | | - if (options.target != null) { |
115 | | - options.target = convertEnumCompilerOptions(ts.ScriptTarget, options.target, toLowerCase); |
116 | | - } |
117 | | - return options; |
118 | | -} |
119 | | - |
120 | 37 | /** |
121 | 38 | * @param {string | undefined} configPath |
122 | 39 | * @param {LoadTsconfigInit} |
123 | | - * @returns {ts.CompilerOptions} |
| 40 | + * @returns {Promise<object>} |
124 | 41 | */ |
125 | | -exports.loadTsconfigOptions = function (configPath, { base, start, filename }) { |
| 42 | +exports.loadTsconfigOptions = async function (configPath, { base, start, filename }) { |
126 | 43 | // throw error if `configPath` does not exist |
127 | 44 | const tsconfig = configPath != null ? resolve(configPath) : walkParentDirs({ base, start, filename }); |
128 | 45 | if (tsconfig == null) { |
129 | | - return {}; |
| 46 | + return DEFAULT_TSCONFIG_OPTIONS; |
130 | 47 | } |
131 | | - const content = ts.readConfigFile(tsconfig, ts.sys.readFile); |
132 | | - if (content.error != null || content.config == null) { |
133 | | - return {}; |
| 48 | + try { |
| 49 | + const result = await parse(tsconfig); |
| 50 | + return result.tsconfig; |
| 51 | + } catch (error) { |
| 52 | + console.error(error); |
| 53 | + throw error; |
134 | 54 | } |
135 | | - const { options } = ts.parseJsonConfigFileContent(content.config, ts.sys, dirname(tsconfig)); |
136 | | - return normalizeCompilerOptions(options); |
137 | 55 | }; |
0 commit comments