|
3 | 3 |
|
4 | 4 | /* @internal */ |
5 | 5 | namespace ts { |
6 | | - export const defaultInitCompilerOptions: CompilerOptions = { |
7 | | - module: ModuleKind.CommonJS, |
8 | | - target: ScriptTarget.ES3, |
9 | | - noImplicitAny: true, |
10 | | - outDir: "built", |
11 | | - rootDir: ".", |
12 | | - sourceMap: false, |
13 | | - } |
14 | | - |
15 | | - export function buildConfigFile(writer: EmitTextWriter, compilerOptions: CompilerOptions, fileNames?: string[], excludes?: string[]) { |
16 | | - compilerOptions = extend(compilerOptions, defaultInitCompilerOptions); |
17 | | - let { write, writeLine, increaseIndent, decreaseIndent } = writer; |
18 | | - let { optionNameMap } = getOptionNameMap(); |
19 | | - writeTsConfigDotJsonFile(); |
20 | | - |
21 | | - function writeTsConfigDotJsonFile() { |
22 | | - write("{"); |
23 | | - writeLine(); |
24 | | - increaseIndent(); |
25 | | - writeCompilerOptions(); |
26 | | - if (fileNames && fileNames.length > 0) { |
27 | | - write(","); |
28 | | - writeLine(); |
29 | | - writeFileNames(); |
30 | | - } |
31 | | - if (excludes) { |
32 | | - write(","); |
33 | | - writeLine(); |
34 | | - writeExcludeOptions(); |
35 | | - } |
36 | | - writeLine(); |
37 | | - decreaseIndent(); |
38 | | - write("}"); |
39 | | - } |
40 | | - |
41 | | - function writeCompilerOptions() { |
42 | | - write(`"compilerOptions": {`); |
43 | | - writeLine(); |
44 | | - increaseIndent(); |
45 | | - |
46 | | - for (var option in compilerOptions) { |
47 | | - switch (option) { |
48 | | - case "init": |
49 | | - case "watch": |
50 | | - case "help": |
51 | | - case "version": |
52 | | - continue; |
53 | | - |
54 | | - case "module": |
55 | | - case "target": |
56 | | - case "newLine": |
57 | | - writeComplexCompilerOption(option); |
58 | | - break; |
59 | | - |
60 | | - default: |
61 | | - writeSimpleCompilerOption(option); |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - decreaseIndent(); |
66 | | - write("}"); |
67 | | - } |
68 | | - |
69 | | - function writeOptionalOptionDescription(option: string) { |
70 | | - option = option.toLowerCase(); |
71 | | - if (optionNameMap[option].description && |
72 | | - optionNameMap[option].description.key) { |
73 | | - |
74 | | - write(`// ${optionNameMap[option].description.key}`); |
75 | | - writeLine(); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - /** |
80 | | - * Write simple compiler option. A simple compiler option is an option |
81 | | - * with boolean or non string set value. |
82 | | - */ |
83 | | - function writeSimpleCompilerOption(option: string) { |
84 | | - writeOptionalOptionDescription(option); |
85 | | - |
86 | | - write(`"${option}": `); |
87 | | - if (typeof compilerOptions[option] === "string") { |
88 | | - write(`"${compilerOptions[option]}",`); |
89 | | - writeLine(); |
90 | | - } |
91 | | - else { |
92 | | - if (compilerOptions[option]) { |
93 | | - write("true,"); |
94 | | - } |
95 | | - else { |
96 | | - write("false,"); |
97 | | - } |
98 | | - writeLine(); |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - function writeComplexCompilerOption(option: string) { |
103 | | - writeOptionalOptionDescription(option); |
104 | | - |
105 | | - outer: switch (option) { |
106 | | - case "module": |
107 | | - var moduleValue: string; |
108 | | - switch (compilerOptions.module) { |
109 | | - case ModuleKind.None: |
110 | | - break outer; |
111 | | - case ModuleKind.CommonJS: |
112 | | - moduleValue = "commonjs"; |
113 | | - break; |
114 | | - case ModuleKind.System: |
115 | | - moduleValue = "system"; |
116 | | - break; |
117 | | - case ModuleKind.UMD: |
118 | | - moduleValue = "umd"; |
119 | | - break; |
120 | | - default: |
121 | | - moduleValue = "amd"; |
122 | | - break; |
123 | | - } |
124 | | - write(`"module": "${moduleValue}",`); |
125 | | - writeLine(); |
126 | | - break; |
127 | | - |
128 | | - case "target": |
129 | | - var targetValue: string; |
130 | | - switch (compilerOptions.target) { |
131 | | - case ScriptTarget.ES5: |
132 | | - targetValue = "es5"; |
133 | | - break; |
134 | | - case ScriptTarget.ES6: |
135 | | - targetValue = "es6"; |
136 | | - break; |
137 | | - default: |
138 | | - targetValue = "es3"; |
139 | | - break; |
140 | | - } |
141 | | - write(`"target": "${targetValue}",`); |
142 | | - writeLine(); |
143 | | - break; |
144 | | - |
145 | | - case "newLine": |
146 | | - var newlineValue: string; |
147 | | - switch (compilerOptions.newLine) { |
148 | | - case NewLineKind.CarriageReturnLineFeed: |
149 | | - newlineValue = "CRLF"; |
150 | | - break; |
151 | | - default: |
152 | | - newlineValue = "LF"; |
153 | | - break; |
154 | | - } |
155 | | - write(`"newLine": "${newlineValue}",`); |
156 | | - writeLine(); |
157 | | - break; |
158 | | - } |
159 | | - } |
160 | | - |
161 | | - function writeFileNames() { |
162 | | - write(`"files": [`); |
163 | | - writeLine(); |
164 | | - increaseIndent(); |
165 | | - |
166 | | - for (let fileName of fileNames) { |
167 | | - write(`"${fileName}",`); |
168 | | - writeLine(); |
169 | | - } |
170 | | - |
171 | | - decreaseIndent(); |
172 | | - write("]"); |
173 | | - } |
174 | | - |
175 | | - function writeExcludeOptions() { |
176 | | - write(`"exclude": [`); |
177 | | - writeLine(); |
178 | | - increaseIndent(); |
179 | | - |
180 | | - for (let exclude of excludes) { |
181 | | - write(`"${exclude}",`); |
182 | | - writeLine(); |
183 | | - } |
184 | | - |
185 | | - decreaseIndent(); |
186 | | - write("]"); |
187 | | - } |
188 | | - } |
189 | | - |
190 | 6 | export function isExternalModuleOrDeclarationFile(sourceFile: SourceFile) { |
191 | 7 | return isExternalModule(sourceFile) || isDeclarationFile(sourceFile); |
192 | 8 | } |
|
0 commit comments