Skip to content

Commit f9f95ba

Browse files
committed
Support for tsconfig.json files in command-line compiler
1 parent cbecae3 commit f9f95ba

8 files changed

+258
-27
lines changed

Diff for: src/compiler/commandLineParser.ts

+97-11
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ module ts {
3333
type: "boolean",
3434
description: Diagnostics.Print_this_message,
3535
},
36+
{
37+
name: "listFiles",
38+
type: "boolean",
39+
},
3640
{
3741
name: "locale",
3842
type: "string",
3943
},
4044
{
4145
name: "mapRoot",
4246
type: "string",
47+
isFilePath: true,
4348
description: Diagnostics.Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations,
4449
paramType: Diagnostics.LOCATION,
4550
},
@@ -90,6 +95,7 @@ module ts {
9095
{
9196
name: "outDir",
9297
type: "string",
98+
isFilePath: true,
9399
description: Diagnostics.Redirect_output_structure_to_the_directory,
94100
paramType: Diagnostics.DIRECTORY,
95101
},
@@ -98,6 +104,14 @@ module ts {
98104
type: "boolean",
99105
description: Diagnostics.Do_not_erase_const_enum_declarations_in_generated_code
100106
},
107+
{
108+
name: "project",
109+
shortName: "p",
110+
type: "string",
111+
isFilePath: true,
112+
description: Diagnostics.Compile_the_project_in_the_given_directory,
113+
paramType: Diagnostics.DIRECTORY
114+
},
101115
{
102116
name: "removeComments",
103117
type: "boolean",
@@ -111,6 +125,7 @@ module ts {
111125
{
112126
name: "sourceRoot",
113127
type: "string",
128+
isFilePath: true,
114129
description: Diagnostics.Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations,
115130
paramType: Diagnostics.LOCATION,
116131
},
@@ -141,17 +156,6 @@ module ts {
141156
}
142157
];
143158

144-
var shortOptionNames: Map<string> = {};
145-
var optionNameMap: Map<CommandLineOption> = {};
146-
147-
forEach(optionDeclarations, option => {
148-
optionNameMap[option.name.toLowerCase()] = option;
149-
150-
if (option.shortName) {
151-
shortOptionNames[option.shortName] = option.name;
152-
}
153-
});
154-
155159
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
156160
// Set default compiler option values
157161
var options: CompilerOptions = {
@@ -160,7 +164,15 @@ module ts {
160164
};
161165
var filenames: string[] = [];
162166
var errors: Diagnostic[] = [];
167+
var shortOptionNames: Map<string> = {};
168+
var optionNameMap: Map<CommandLineOption> = {};
163169

170+
forEach(optionDeclarations, option => {
171+
optionNameMap[option.name.toLowerCase()] = option;
172+
if (option.shortName) {
173+
shortOptionNames[option.shortName] = option.name;
174+
}
175+
});
164176
parseStrings(commandLine);
165177
return {
166178
options,
@@ -256,4 +268,78 @@ module ts {
256268
parseStrings(args);
257269
}
258270
}
271+
272+
export function readConfigFile(filename: string): any {
273+
try {
274+
var text = sys.readFile(filename);
275+
return /\S/.test(text) ? JSON.parse(text) : {};
276+
}
277+
catch (e) {
278+
}
279+
}
280+
281+
export function parseConfigFile(json: any, basePath?: string): ParsedCommandLine {
282+
var errors: Diagnostic[] = [];
283+
284+
return {
285+
options: getCompilerOptions(),
286+
filenames: getFiles(),
287+
errors
288+
};
289+
290+
function getCompilerOptions(): CompilerOptions {
291+
var options: CompilerOptions = {};
292+
var optionNameMap: Map<CommandLineOption> = {};
293+
forEach(optionDeclarations, option => {
294+
optionNameMap[option.name] = option;
295+
});
296+
var jsonOptions = json["compilerOptions"];
297+
if (jsonOptions) {
298+
for (var id in jsonOptions) {
299+
if (hasProperty(optionNameMap, id)) {
300+
var opt = optionNameMap[id];
301+
var optType = opt.type;
302+
var value = jsonOptions[id];
303+
var expectedType = typeof optType === "string" ? optType : "string";
304+
if (typeof value === expectedType) {
305+
if (typeof optType !== "string") {
306+
var key = value.toLowerCase();
307+
if (hasProperty(optType, key)) {
308+
value = optType[key];
309+
}
310+
else {
311+
errors.push(createCompilerDiagnostic(opt.error));
312+
value = 0;
313+
}
314+
}
315+
if (opt.isFilePath) {
316+
value = normalizePath(combinePaths(basePath, value));
317+
}
318+
options[opt.name] = value;
319+
}
320+
else {
321+
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, id, expectedType));
322+
}
323+
}
324+
else {
325+
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, id));
326+
}
327+
}
328+
}
329+
return options;
330+
}
331+
332+
function getFiles(): string[] {
333+
var references: string[] = json["references"] instanceof Array ? json["references"] : []
334+
var files = map(references, s => combinePaths(basePath, s));
335+
var sysFiles = sys.readDirectory(basePath, ".ts");
336+
for (var i = 0; i < sysFiles.length; i++) {
337+
var name = sysFiles[i];
338+
if (!fileExtensionIs(name, ".d.ts") || !contains(sysFiles, name.substr(0, name.length - 5) + ".ts")) {
339+
files.push(name);
340+
}
341+
}
342+
return files;
343+
}
344+
}
259345
}

Diff for: src/compiler/core.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ module ts {
178178
return <T>result;
179179
}
180180

181+
export function extend<T>(first: Map<T>, second: Map<T>): Map<T> {
182+
var result: Map<T> = {};
183+
for (var id in first) {
184+
result[id] = first[id];
185+
}
186+
for (var id in second) {
187+
if (!hasProperty(result, id)) {
188+
result[id] = second[id];
189+
}
190+
}
191+
return result;
192+
}
193+
181194
export function forEachValue<T, U>(map: Map<T>, callback: (value: T) => U): U {
182195
var result: U;
183196
for (var id in map) {
@@ -568,7 +581,7 @@ module ts {
568581
export function combinePaths(path1: string, path2: string) {
569582
if (!(path1 && path1.length)) return path2;
570583
if (!(path2 && path2.length)) return path1;
571-
if (path2.charAt(0) === directorySeparator) return path2;
584+
if (getRootLength(path2) !== 0) return path2;
572585
if (path1.charAt(path1.length - 1) === directorySeparator) return path1 + path2;
573586
return path1 + directorySeparator + path2;
574587
}

Diff for: src/compiler/diagnosticInformationMap.generated.ts

+2
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ module ts {
378378
Cannot_read_file_0_Colon_1: { code: 5012, category: DiagnosticCategory.Error, key: "Cannot read file '{0}': {1}" },
379379
Unsupported_file_encoding: { code: 5013, category: DiagnosticCategory.Error, key: "Unsupported file encoding." },
380380
Unknown_compiler_option_0: { code: 5023, category: DiagnosticCategory.Error, key: "Unknown compiler option '{0}'." },
381+
Compiler_option_0_requires_a_value_of_type_1: { code: 5024, category: DiagnosticCategory.Error, key: "Compiler option '{0}' requires a value of type {1}." },
381382
Could_not_write_file_0_Colon_1: { code: 5033, category: DiagnosticCategory.Error, key: "Could not write file '{0}': {1}" },
382383
Option_mapRoot_cannot_be_specified_without_specifying_sourcemap_option: { code: 5038, category: DiagnosticCategory.Error, key: "Option mapRoot cannot be specified without specifying sourcemap option." },
383384
Option_sourceRoot_cannot_be_specified_without_specifying_sourcemap_option: { code: 5039, category: DiagnosticCategory.Error, key: "Option sourceRoot cannot be specified without specifying sourcemap option." },
@@ -397,6 +398,7 @@ module ts {
397398
Specify_module_code_generation_Colon_commonjs_or_amd: { code: 6016, category: DiagnosticCategory.Message, key: "Specify module code generation: 'commonjs' or 'amd'" },
398399
Print_this_message: { code: 6017, category: DiagnosticCategory.Message, key: "Print this message." },
399400
Print_the_compiler_s_version: { code: 6019, category: DiagnosticCategory.Message, key: "Print the compiler's version." },
401+
Compile_the_project_in_the_given_directory: { code: 6020, category: DiagnosticCategory.Message, key: "Compile the project in the given directory." },
400402
Syntax_Colon_0: { code: 6023, category: DiagnosticCategory.Message, key: "Syntax: {0}" },
401403
options: { code: 6024, category: DiagnosticCategory.Message, key: "options" },
402404
file: { code: 6025, category: DiagnosticCategory.Message, key: "file" },

Diff for: src/compiler/diagnosticMessages.json

+8
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,10 @@
16101610
"category": "Error",
16111611
"code": 5023
16121612
},
1613+
"Compiler option '{0}' requires a value of type {1}.": {
1614+
"category": "Error",
1615+
"code": 5024
1616+
},
16131617
"Could not write file '{0}': {1}": {
16141618
"category": "Error",
16151619
"code": 5033
@@ -1686,6 +1690,10 @@
16861690
"category": "Message",
16871691
"code": 6019
16881692
},
1693+
"Compile the project in the given directory.": {
1694+
"category": "Message",
1695+
"code": 6020
1696+
},
16891697
"Syntax: {0}": {
16901698
"category": "Message",
16911699
"code": 6023

Diff for: src/compiler/program.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ module ts {
8181
var seenNoDefaultLib = options.noLib;
8282
var commonSourceDirectory: string;
8383

84+
//options = extend(options, {
85+
// module: ModuleKind.None,
86+
// target: ScriptTarget.ES3
87+
//});
88+
8489
forEach(rootNames, name => processRootFile(name, false));
8590
if (!seenNoDefaultLib) {
8691
processRootFile(host.getDefaultLibFilename(options), true);
@@ -146,7 +151,9 @@ module ts {
146151
function invokeEmitter(targetSourceFile?: SourceFile) {
147152
var resolver = getDiagnosticsProducingTypeChecker().getEmitResolver();
148153
return emitFiles(resolver, getEmitHost(), targetSourceFile);
149-
} function getSourceFile(filename: string) {
154+
}
155+
156+
function getSourceFile(filename: string) {
150157
filename = host.getCanonicalFileName(filename);
151158
return hasProperty(filesByName, filename) ? filesByName[filename] : undefined;
152159
}

Diff for: src/compiler/sys.ts

+63
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module ts {
1414
createDirectory(directoryName: string): void;
1515
getExecutingFilePath(): string;
1616
getCurrentDirectory(): string;
17+
readDirectory(path: string, extension?: string): string[];
1718
getMemoryUsage? (): number;
1819
exit(exitCode?: number): void;
1920
}
@@ -28,6 +29,13 @@ module ts {
2829
declare var global: any;
2930
declare var __filename: string;
3031

32+
declare class Enumerator {
33+
public atEnd(): boolean;
34+
public moveNext(): boolean;
35+
public item(): any;
36+
constructor(o: any);
37+
}
38+
3139
export var sys: System = (function () {
3240

3341
function getWScriptSystem(): System {
@@ -100,6 +108,34 @@ module ts {
100108
}
101109
}
102110

111+
function getNames(collection: any): string[] {
112+
var result: string[] = [];
113+
for (var e = new Enumerator(collection); !e.atEnd(); e.moveNext()) {
114+
result.push(e.item().Name);
115+
}
116+
return result.sort();
117+
}
118+
119+
function readDirectory(path: string, extension?: string): string[] {
120+
var result: string[] = [];
121+
visitDirectory(path);
122+
return result;
123+
function visitDirectory(path: string) {
124+
var folder = fso.GetFolder(path || ".");
125+
var files = getNames(folder.files);
126+
for (var i = 0; i < files.length; i++) {
127+
var name = files[i];
128+
if (!extension || fileExtensionIs(name, extension)) {
129+
result.push(combinePaths(path, name));
130+
}
131+
}
132+
var subfolders = getNames(folder.subfolders);
133+
for (var i = 0; i < subfolders.length; i++) {
134+
visitDirectory(combinePaths(path, subfolders[i]));
135+
}
136+
}
137+
}
138+
103139
return {
104140
args,
105141
newLine: "\r\n",
@@ -129,6 +165,7 @@ module ts {
129165
getCurrentDirectory() {
130166
return new ActiveXObject("WScript.Shell").CurrentDirectory;
131167
},
168+
readDirectory,
132169
exit(exitCode?: number): void {
133170
try {
134171
WScript.Quit(exitCode);
@@ -185,6 +222,31 @@ module ts {
185222
_fs.writeFileSync(fileName, data, "utf8");
186223
}
187224

225+
function readDirectory(path: string, extension?: string): string[] {
226+
var result: string[] = [];
227+
visitDirectory(path);
228+
return result;
229+
function visitDirectory(path: string) {
230+
var files = _fs.readdirSync(path || ".").sort();
231+
var directories: string[] = [];
232+
for (var i = 0; i < files.length; i++) {
233+
var name = combinePaths(path, files[i]);
234+
var stat = _fs.statSync(name);
235+
if (stat.isFile()) {
236+
if (!extension || fileExtensionIs(name, extension)) {
237+
result.push(name);
238+
}
239+
}
240+
else if (stat.isDirectory()) {
241+
directories.push(name);
242+
}
243+
}
244+
for (var i = 0; i < directories.length; i++) {
245+
visitDirectory(directories[i]);
246+
}
247+
}
248+
}
249+
188250
return {
189251
args: process.argv.slice(2),
190252
newLine: _os.EOL,
@@ -231,6 +293,7 @@ module ts {
231293
getCurrentDirectory() {
232294
return process.cwd();
233295
},
296+
readDirectory,
234297
getMemoryUsage() {
235298
if (global.gc) {
236299
global.gc();

0 commit comments

Comments
 (0)