@@ -2,13 +2,15 @@ package execute
2
2
3
3
import (
4
4
"fmt"
5
+ "slices"
5
6
"strings"
6
7
7
8
"github.com/microsoft/typescript-go/internal/ast"
8
9
"github.com/microsoft/typescript-go/internal/compiler"
9
10
"github.com/microsoft/typescript-go/internal/compiler/diagnostics"
10
11
"github.com/microsoft/typescript-go/internal/core"
11
12
"github.com/microsoft/typescript-go/internal/diagnosticwriter"
13
+ "github.com/microsoft/typescript-go/internal/tsoptions"
12
14
"github.com/microsoft/typescript-go/internal/tspath"
13
15
)
14
16
@@ -85,3 +87,186 @@ func printVersion(sys System) {
85
87
fmt .Fprint (sys .Writer (), diagnostics .Version_0 .Format (core .Version )+ sys .NewLine ())
86
88
sys .EndWrite ()
87
89
}
90
+
91
+ func printHelp (sys System , commandLine * tsoptions.ParsedCommandLine ) {
92
+ if commandLine .CompilerOptions ().All .IsFalseOrUnknown () {
93
+ printEasyHelp (sys , getOptionsForHelp (commandLine ))
94
+ } else {
95
+ // !!! printAllHelp(sys, getOptionsForHelp(commandLine))
96
+ }
97
+ }
98
+
99
+ func getOptionsForHelp (commandLine * tsoptions.ParsedCommandLine ) []* tsoptions.CommandLineOption {
100
+ // Sort our options by their names, (e.g. "--noImplicitAny" comes before "--watch")
101
+ opts := slices .Clone (tsoptions .OptionsDeclarations )
102
+ opts = append (opts , & tsoptions .TscBuildOption )
103
+
104
+ if commandLine .CompilerOptions ().All .IsTrue () {
105
+ slices .SortFunc (opts , func (a , b * tsoptions.CommandLineOption ) int {
106
+ return strings .Compare (strings .ToLower (a .Name ), strings .ToLower (b .Name ))
107
+ })
108
+ return opts
109
+ } else {
110
+ return core .Filter (opts , func (opt * tsoptions.CommandLineOption ) bool {
111
+ return opt .ShowInSimplifiedHelpView
112
+ })
113
+ }
114
+ }
115
+
116
+ func getHeader (sys System , message string ) []string {
117
+ // !!! const colors = createColors(sys);
118
+ var header []string
119
+ // !!! terminalWidth := sys.GetWidthOfTerminal?.() ?? 0
120
+ const tsIconLength = 5
121
+
122
+ // const tsIconFirstLine = colors.blueBackground("".padStart(tsIconLength));
123
+ // const tsIconSecondLine = colors.blueBackground(colors.brightWhite("TS ".padStart(tsIconLength)));
124
+ // // If we have enough space, print TS icon.
125
+ // if (terminalWidth >= message.length + tsIconLength) {
126
+ // // right align of the icon is 120 at most.
127
+ // const rightAlign = terminalWidth > 120 ? 120 : terminalWidth;
128
+ // const leftAlign = rightAlign - tsIconLength;
129
+ // header.push(message.padEnd(leftAlign) + tsIconFirstLine + sys.newLine);
130
+ // header.push("".padStart(leftAlign) + tsIconSecondLine + sys.newLine);
131
+ // }
132
+ // else {
133
+ header = append (header , message + sys .NewLine (), sys .NewLine ())
134
+ // }
135
+ return header
136
+ }
137
+
138
+ func printEasyHelp (sys System , simpleOptions []* tsoptions.CommandLineOption ) {
139
+ // !!! const colors = createColors(sys);
140
+ var output []string
141
+ example := func (examples []string , desc * diagnostics.Message ) {
142
+ for _ , example := range examples {
143
+ // !!! colors
144
+ // output.push(" " + colors.blue(example) + sys.newLine);
145
+ output = append (output , " " , example , sys .NewLine ())
146
+ }
147
+ output = append (output , " " , desc .Format (), sys .NewLine (), sys .NewLine ())
148
+ }
149
+
150
+ msg := diagnostics .X_tsc_Colon_The_TypeScript_Compiler .Format () + " - " + diagnostics .Version_0 .Format (core .Version )
151
+ output = append (output , getHeader (sys , msg )... )
152
+
153
+ output = append (output /*colors.bold(*/ , diagnostics .COMMON_COMMANDS .Format () /*)*/ , sys .NewLine (), sys .NewLine ())
154
+
155
+ example ([]string {"tsc" }, diagnostics .Compiles_the_current_project_tsconfig_json_in_the_working_directory )
156
+ example ([]string {"tsc app.ts util.ts" }, diagnostics .Ignoring_tsconfig_json_compiles_the_specified_files_with_default_compiler_options )
157
+ example ([]string {"tsc -b" }, diagnostics .Build_a_composite_project_in_the_working_directory )
158
+ example ([]string {"tsc --init" }, diagnostics .Creates_a_tsconfig_json_with_the_recommended_settings_in_the_working_directory )
159
+ example ([]string {"tsc -p ./path/to/tsconfig.json" }, diagnostics .Compiles_the_TypeScript_project_located_at_the_specified_path )
160
+ example ([]string {"tsc --help --all" }, diagnostics .An_expanded_version_of_this_information_showing_all_possible_compiler_options )
161
+ example ([]string {"tsc --noEmit" , "tsc --target esnext" }, diagnostics .Compiles_the_current_project_with_additional_settings )
162
+
163
+ var cliCommands []* tsoptions.CommandLineOption
164
+ var configOpts []* tsoptions.CommandLineOption
165
+ for _ , opt := range simpleOptions {
166
+ if opt .IsCommandLineOnly || opt .Category == diagnostics .Command_line_Options {
167
+ cliCommands = append (cliCommands , opt )
168
+ } else {
169
+ configOpts = append (configOpts , opt )
170
+ }
171
+ }
172
+
173
+ output = append (output , generateSectionOptionsOutput (sys , diagnostics .COMMAND_LINE_FLAGS .Format (), cliCommands /*subCategory*/ , false /*beforeOptionsDescription*/ , nil /*afterOptionsDescription*/ , nil )... )
174
+
175
+ after := diagnostics .You_can_learn_about_all_of_the_compiler_options_at_0 .Format ("https://aka.ms/tsc" )
176
+ output = append (output , generateSectionOptionsOutput (sys , diagnostics .COMMON_COMPILER_OPTIONS .Format (), configOpts /*subCategory*/ , false /*beforeOptionsDescription*/ , nil ,
177
+ // !!! locale formatMessage(Diagnostics.You_can_learn_about_all_of_the_compiler_options_at_0, "https://aka.ms/tsc")),
178
+ & after )... )
179
+
180
+ for _ , chunk := range output {
181
+ fmt .Fprint (sys .Writer (), chunk )
182
+ }
183
+ sys .EndWrite ()
184
+ }
185
+
186
+ func generateSectionOptionsOutput (
187
+ sys System ,
188
+ sectionName string ,
189
+ options []* tsoptions.CommandLineOption ,
190
+ subCategory bool ,
191
+ beforeOptionsDescription ,
192
+ afterOptionsDescription * string ,
193
+ ) (output []string ) {
194
+ // !!! color
195
+ output = append (output /*createColors(sys).bold(*/ , sectionName /*)*/ , sys .NewLine (), sys .NewLine ())
196
+
197
+ if beforeOptionsDescription != nil {
198
+ output = append (output , * beforeOptionsDescription , sys .NewLine (), sys .NewLine ())
199
+ }
200
+ if ! subCategory {
201
+ output = append (output , generateGroupOptionOutput (sys , options )... )
202
+ if afterOptionsDescription != nil {
203
+ output = append (output , * afterOptionsDescription , sys .NewLine (), sys .NewLine ())
204
+ }
205
+ return output
206
+ }
207
+ categoryMap := make (map [string ][]* tsoptions.CommandLineOption )
208
+ for _ , option := range options {
209
+ if option .Category == nil {
210
+ continue
211
+ }
212
+ curCategory := option .Category .Format ()
213
+ categoryMap [curCategory ] = append (categoryMap [curCategory ], option )
214
+ }
215
+ for key , value := range categoryMap {
216
+ output = append (output , "### " , key , sys .NewLine (), sys .NewLine ())
217
+ output = append (output , generateGroupOptionOutput (sys , value )... )
218
+ }
219
+ if afterOptionsDescription != nil {
220
+ output = append (output , * afterOptionsDescription , sys .NewLine (), sys .NewLine ())
221
+ }
222
+
223
+ return output
224
+ }
225
+
226
+ func generateGroupOptionOutput (sys System , optionsList []* tsoptions.CommandLineOption ) []string {
227
+ var maxLength int
228
+ for _ , option := range optionsList {
229
+ curLenght := len (getDisplayNameTextOfOption (option ))
230
+ maxLength = max (curLenght , maxLength )
231
+ }
232
+
233
+ // left part should be right align, right part should be left align
234
+
235
+ // assume 2 space between left margin and left part.
236
+ rightAlignOfLeftPart := maxLength + 2
237
+ // assume 2 space between left and right part
238
+ leftAlignOfRightPart := rightAlignOfLeftPart + 2
239
+
240
+ var lines []string
241
+ for _ , option := range optionsList {
242
+ tmp := generateOptionOutput (sys , option , rightAlignOfLeftPart , leftAlignOfRightPart )
243
+ lines = append (lines , tmp ... )
244
+ }
245
+
246
+ // make sure always a blank line in the end.
247
+ // !!! if lines[len(lines)-2] != sys.NewLine() {
248
+ // !!! lines = append(lines, sys.NewLine())
249
+ // !!! }
250
+
251
+ return lines
252
+ }
253
+
254
+ func generateOptionOutput (
255
+ sys System ,
256
+ option * tsoptions.CommandLineOption ,
257
+ rightAlignOfLeftPart , leftAlignOfRightPart int ,
258
+ ) []string {
259
+ var text []string
260
+ // !!! const colors = createColors(sys);
261
+
262
+ // name and description
263
+ // !!! name := getDisplayNameTextOfOption(option)
264
+
265
+ // !!!
266
+
267
+ return text
268
+ }
269
+
270
+ func getDisplayNameTextOfOption (option * tsoptions.CommandLineOption ) string {
271
+ return "--" + option .Name + core .IfElse (option .ShortName != "" , ", -" + option .ShortName , "" )
272
+ }
0 commit comments