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