Skip to content

Commit f7a2922

Browse files
author
Yui
committed
Merge pull request #711 from Microsoft/returnExitCode
Return exit code
2 parents debc653 + 8947757 commit f7a2922

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/compiler/tsc.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,41 +199,48 @@ module ts {
199199

200200
export function executeCommandLine(args: string[]): void {
201201
var commandLine = parseCommandLine(args);
202+
var compilerOptions = commandLine.options;
202203

203-
if (commandLine.options.locale) {
204+
if (compilerOptions.locale) {
204205
validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors);
205206
}
206207

207208
// If there are any errors due to command line parsing and/or
208209
// setting up localization, report them and quit.
209210
if (commandLine.errors.length > 0) {
210211
reportDiagnostics(commandLine.errors);
211-
return sys.exit(1);
212+
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
212213
}
213214

214-
if (commandLine.options.version) {
215+
if (compilerOptions.version) {
215216
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Version_0, version));
216-
return sys.exit(0);
217+
return sys.exit(EmitReturnStatus.Succeeded);
217218
}
218219

219-
if (commandLine.options.help || commandLine.filenames.length === 0) {
220+
if (compilerOptions.help) {
220221
printVersion();
221222
printHelp();
222-
return sys.exit(0);
223+
return sys.exit(EmitReturnStatus.Succeeded);
223224
}
224225

225-
var defaultCompilerHost = createCompilerHost(commandLine.options);
226+
if (commandLine.filenames.length === 0) {
227+
printVersion();
228+
printHelp();
229+
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
230+
}
231+
232+
var defaultCompilerHost = createCompilerHost(compilerOptions);
226233

227-
if (commandLine.options.watch) {
234+
if (compilerOptions.watch) {
228235
if (!sys.watchFile) {
229236
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"));
230-
return sys.exit(1);
237+
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
231238
}
232239

233240
watchProgram(commandLine, defaultCompilerHost);
234241
}
235242
else {
236-
var result = compile(commandLine, defaultCompilerHost).errors.length > 0 ? 1 : 0;
243+
var result = compile(commandLine, defaultCompilerHost).exitStatus
237244
return sys.exit(result);
238245
}
239246
}
@@ -328,21 +335,27 @@ module ts {
328335

329336
function compile(commandLine: ParsedCommandLine, compilerHost: CompilerHost) {
330337
var parseStart = new Date().getTime();
331-
var program = createProgram(commandLine.filenames, commandLine.options, compilerHost);
338+
var compilerOptions = commandLine.options;
339+
var program = createProgram(commandLine.filenames, compilerOptions, compilerHost);
332340

333341
var bindStart = new Date().getTime();
334-
var errors = program.getDiagnostics();
342+
var errors: Diagnostic[] = program.getDiagnostics();
343+
var exitStatus: EmitReturnStatus;
344+
335345
if (errors.length) {
336346
var checkStart = bindStart;
337347
var emitStart = bindStart;
338348
var reportStart = bindStart;
349+
exitStatus = EmitReturnStatus.AllOutputGenerationSkipped;
339350
}
340351
else {
341352
var checker = program.getTypeChecker(/*fullTypeCheckMode*/ true);
342353
var checkStart = new Date().getTime();
343354
var semanticErrors = checker.getDiagnostics();
344355
var emitStart = new Date().getTime();
345-
var emitErrors = checker.emitFiles().errors;
356+
var emitOutput = checker.emitFiles();
357+
var emitErrors = emitOutput.errors;
358+
exitStatus = emitOutput.emitResultStatus;
346359
var reportStart = new Date().getTime();
347360
errors = concatenate(semanticErrors, emitErrors);
348361
}
@@ -366,8 +379,7 @@ module ts {
366379
reportTimeStatistic("Total time", reportStart - parseStart);
367380
}
368381

369-
return { program: program, errors: errors };
370-
382+
return { program: program, exitStatus: exitStatus }
371383
}
372384

373385
function printVersion() {

src/compiler/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,11 @@ module ts {
604604
// Return code used by getEmitOutput function to indicate status of the function
605605
export enum EmitReturnStatus {
606606
Succeeded = 0, // All outputs generated as requested (.js, .map, .d.ts), no errors reported
607-
AllOutputGenerationSkipped = 1, // No .js generated because of syntax errors, or compiler options errors, nothing generated
607+
AllOutputGenerationSkipped = 1, // No .js generated because of syntax errors, nothing generated
608608
JSGeneratedWithSemanticErrors = 2, // .js and .map generated with semantic errors
609609
DeclarationGenerationSkipped = 3, // .d.ts generation skipped because of semantic errors or declaration emitter specific errors; Output .js with semantic errors
610-
EmitErrorsEncountered = 4 // Emitter errors occurred during emitting process
610+
EmitErrorsEncountered = 4, // Emitter errors occurred during emitting process
611+
CompilerOptionsErrors = 5, // Errors occurred in parsing compiler options, nothing generated
611612
}
612613

613614
export interface EmitResult {

0 commit comments

Comments
 (0)