@@ -15,10 +15,14 @@ type cbType = func(p any) any
15
15
16
16
func CommandLine (sys System , cb cbType , commandLineArgs []string ) ExitStatus {
17
17
parsedCommandLine := tsoptions .ParseCommandLine (commandLineArgs , sys )
18
- return executeCommandLineWorker (sys , cb , parsedCommandLine )
18
+ e , watcher := executeCommandLineWorker (sys , cb , parsedCommandLine )
19
+ if watcher != nil {
20
+ return e
21
+ }
22
+ return start (watcher )
19
23
}
20
24
21
- func executeCommandLineWorker (sys System , cb cbType , commandLine * tsoptions.ParsedCommandLine ) ExitStatus {
25
+ func executeCommandLineWorker (sys System , cb cbType , commandLine * tsoptions.ParsedCommandLine ) ( ExitStatus , * watcher ) {
22
26
configFileName := ""
23
27
reportDiagnostic := createDiagnosticReporter (sys , commandLine .CompilerOptions ().Pretty )
24
28
// if commandLine.Options().Locale != nil
@@ -27,35 +31,35 @@ func executeCommandLineWorker(sys System, cb cbType, commandLine *tsoptions.Pars
27
31
for _ , e := range commandLine .Errors {
28
32
reportDiagnostic (e )
29
33
}
30
- return ExitStatusDiagnosticsPresent_OutputsSkipped
34
+ return ExitStatusDiagnosticsPresent_OutputsSkipped , nil
31
35
}
32
36
33
37
if commandLine .CompilerOptions ().Init .IsTrue () ||
34
38
commandLine .CompilerOptions ().Version .IsTrue () ||
35
39
// commandLine.CompilerOptions().Help != nil ||
36
40
// commandLine.CompilerOptions().All != nil ||
37
41
commandLine .CompilerOptions ().Watch .IsTrue () && commandLine .CompilerOptions ().ListFilesOnly .IsTrue () {
38
- return ExitStatusNotImplemented
42
+ return ExitStatusNotImplemented , nil
39
43
}
40
44
41
45
if commandLine .CompilerOptions ().Project != "" {
42
46
if len (commandLine .FileNames ()) != 0 {
43
47
reportDiagnostic (ast .NewCompilerDiagnostic (diagnostics .Option_project_cannot_be_mixed_with_source_files_on_a_command_line ))
44
- return ExitStatusDiagnosticsPresent_OutputsSkipped
48
+ return ExitStatusDiagnosticsPresent_OutputsSkipped , nil
45
49
}
46
50
47
51
fileOrDirectory := tspath .NormalizePath (commandLine .CompilerOptions ().Project )
48
52
if fileOrDirectory != "" || sys .FS ().DirectoryExists (fileOrDirectory ) {
49
53
configFileName = tspath .CombinePaths (fileOrDirectory , "tsconfig.json" )
50
54
if ! sys .FS ().FileExists (configFileName ) {
51
55
reportDiagnostic (ast .NewCompilerDiagnostic (diagnostics .Cannot_find_a_tsconfig_json_file_at_the_current_directory_Colon_0 , configFileName ))
52
- return ExitStatusDiagnosticsPresent_OutputsSkipped
56
+ return ExitStatusDiagnosticsPresent_OutputsSkipped , nil
53
57
}
54
58
} else {
55
59
configFileName = fileOrDirectory
56
60
if ! sys .FS ().FileExists (configFileName ) {
57
61
reportDiagnostic (ast .NewCompilerDiagnostic (diagnostics .The_specified_path_does_not_exist_Colon_0 , fileOrDirectory ))
58
- return ExitStatusDiagnosticsPresent_OutputsSkipped
62
+ return ExitStatusDiagnosticsPresent_OutputsSkipped , nil
59
63
}
60
64
}
61
65
} else if len (commandLine .FileNames ()) == 0 {
@@ -70,10 +74,10 @@ func executeCommandLineWorker(sys System, cb cbType, commandLine *tsoptions.Pars
70
74
// print version
71
75
// print help
72
76
}
73
- return ExitStatusDiagnosticsPresent_OutputsSkipped
77
+ return ExitStatusDiagnosticsPresent_OutputsSkipped , nil
74
78
}
75
79
76
- // !!! convert to options with absolute paths is usualy done here, but for ease of implementation, it's done in `tsoptions.ParseCommandLine`
80
+ // !!! convert to options with absolute paths is usualy done here, but for ease of implementation, it's done in `tsoptions.ParseCommandLine() `
77
81
compilerOptionsFromCommandLine := commandLine .CompilerOptions ()
78
82
79
83
if configFileName != "" {
@@ -84,48 +88,41 @@ func executeCommandLineWorker(sys System, cb cbType, commandLine *tsoptions.Pars
84
88
for _ , e := range errors {
85
89
reportDiagnostic (e )
86
90
}
87
- return ExitStatusDiagnosticsPresent_OutputsGenerated
91
+ return ExitStatusDiagnosticsPresent_OutputsGenerated , nil
88
92
}
89
93
if compilerOptionsFromCommandLine .ShowConfig .IsTrue () {
90
- // write show config
91
- return ExitStatusNotImplemented
94
+ return ExitStatusNotImplemented , nil
92
95
}
93
96
// updateReportDiagnostic
94
97
if isWatchSet (configParseResult .CompilerOptions ()) {
95
- // todo watch
96
- return ExitStatusNotImplementedWatch
98
+ return ExitStatusSuccess , createWatcher (sys , configParseResult , reportDiagnostic )
97
99
} else if isIncrementalCompilation (configParseResult .CompilerOptions ()) {
98
- // todo performIncrementalCompilation
99
- return ExitStatusNotImplementedIncremental
100
- } else {
101
- return performCompilation (
102
- sys ,
103
- cb ,
104
- configParseResult ,
105
- reportDiagnostic ,
106
- )
100
+ return ExitStatusNotImplementedIncremental , nil
107
101
}
102
+ return performCompilation (
103
+ sys ,
104
+ cb ,
105
+ configParseResult ,
106
+ reportDiagnostic ,
107
+ ), nil
108
108
} else {
109
109
if compilerOptionsFromCommandLine .ShowConfig .IsTrue () {
110
- // write show config
111
- return ExitStatusNotImplemented
110
+ return ExitStatusNotImplemented , nil
112
111
}
113
112
// todo update reportDiagnostic
114
113
if isWatchSet (compilerOptionsFromCommandLine ) {
115
- // todo watch
116
- // return ExitStatusDiagnosticsPresent_OutputsSkipped
117
- return ExitStatusNotImplementedWatch
114
+ // !!! reportWatchModeWithoutSysSupport
115
+ return ExitStatusSuccess , createWatcher (sys , commandLine , reportDiagnostic )
118
116
} else if isIncrementalCompilation (compilerOptionsFromCommandLine ) {
119
- // todo incremental
120
- return ExitStatusNotImplementedIncremental
117
+ return ExitStatusNotImplementedIncremental , nil
121
118
}
122
119
}
123
120
return performCompilation (
124
121
sys ,
125
122
cb ,
126
123
commandLine ,
127
124
reportDiagnostic ,
128
- )
125
+ ), nil
129
126
}
130
127
131
128
func findConfigFile (searchPath string , fileExists func (string ) bool , configName string ) string {
@@ -171,6 +168,29 @@ func performCompilation(sys System, cb cbType, config *tsoptions.ParsedCommandLi
171
168
host := compiler .NewCompilerHost (config .CompilerOptions (), sys .GetCurrentDirectory (), sys .FS (), sys .DefaultLibraryPath ())
172
169
// todo: cache, statistics, tracing
173
170
program := compiler .NewProgramFromParsedCommandLine (config , host )
171
+
172
+ diagnostics , emitResult , exitStatus := compileAndEmit (sys , program , reportDiagnostic )
173
+ if exitStatus != ExitStatusSuccess {
174
+ // compile exited early
175
+ return exitStatus
176
+ }
177
+
178
+ reportStatistics (sys , program )
179
+ if cb != nil {
180
+ cb (program )
181
+ }
182
+
183
+ if emitResult .EmitSkipped && diagnostics != nil && len (diagnostics ) > 0 {
184
+ return ExitStatusDiagnosticsPresent_OutputsSkipped
185
+ } else if len (diagnostics ) > 0 {
186
+ return ExitStatusDiagnosticsPresent_OutputsGenerated
187
+ }
188
+ return ExitStatusSuccess
189
+ }
190
+
191
+ func compileAndEmit (sys System , program * compiler.Program , reportDiagnostic diagnosticReporter ) ([]* ast.Diagnostic , * compiler.EmitResult , ExitStatus ) {
192
+ // todo: check if third return needed after execute is fully implemented
193
+
174
194
options := program .Options ()
175
195
allDiagnostics := program .GetConfigFileParsingDiagnostics ()
176
196
@@ -188,7 +208,7 @@ func performCompilation(sys System, cb cbType, config *tsoptions.ParsedCommandLi
188
208
}
189
209
// TODO: declaration diagnostics
190
210
if len (diagnostics ) == 0 && options .NoEmit == core .TSTrue && (options .Declaration .IsTrue () && options .Composite .IsTrue ()) {
191
- return ExitStatusNotImplemented
211
+ return nil , nil , ExitStatusNotImplemented
192
212
// addRange(allDiagnostics, program.getDeclarationDiagnostics(/*sourceFile*/ undefined, cancellationToken));
193
213
}
194
214
@@ -213,19 +233,8 @@ func performCompilation(sys System, cb cbType, config *tsoptions.ParsedCommandLi
213
233
// todo: listFiles(program, sys.Writer())
214
234
}
215
235
216
- createReportErrorSummary (sys , config .CompilerOptions ())(allDiagnostics )
217
-
218
- reportStatistics (sys , program )
219
- if cb != nil {
220
- cb (program )
221
- }
222
-
223
- if emitResult .EmitSkipped && diagnostics != nil && len (diagnostics ) > 0 {
224
- return ExitStatusDiagnosticsPresent_OutputsSkipped
225
- } else if len (diagnostics ) > 0 {
226
- return ExitStatusDiagnosticsPresent_OutputsGenerated
227
- }
228
- return ExitStatusSuccess
236
+ createReportErrorSummary (sys , program .Options ())(allDiagnostics )
237
+ return allDiagnostics , emitResult , ExitStatusSuccess
229
238
}
230
239
231
240
// func isBuildCommand(args []string) bool {
0 commit comments