From a8c1d250459b9231a028130e55b4c47babdb65b9 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Mon, 7 Dec 2020 21:39:58 -0800 Subject: [PATCH] link to documentation in command help text --- cmd/esbuild/main.go | 31 ++++++++++++++++++++----------- internal/logger/logger.go | 8 +++++--- pkg/cli/cli_impl.go | 4 ++-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/cmd/esbuild/main.go b/cmd/esbuild/main.go index 9c21a15abb4..724afe45097 100644 --- a/cmd/esbuild/main.go +++ b/cmd/esbuild/main.go @@ -13,11 +13,18 @@ import ( "github.com/evanw/esbuild/pkg/cli" ) -const helpText = ` -Usage: +var helpText = func(colors logger.Colors) string { + return ` +` + colors.Bold + `Usage:` + colors.Default + ` esbuild [options] [entry points] -Options: +` + colors.Bold + `Documentation:` + colors.Default + ` + ` + colors.Underline + `https://esbuild.github.io/` + colors.Default + ` + +` + colors.Bold + `Repository:` + colors.Default + ` + ` + colors.Underline + `https://github.com/evanw/esbuild` + colors.Default + ` + +` + colors.Bold + `Simple options:` + colors.Default + ` --bundle Bundle all dependencies into the output files --define:K=V Substitute K with V while parsing --external:M Exclude module M from the bundle @@ -43,7 +50,7 @@ Options: --target=... Environment target (e.g. es2017, chrome58, firefox57, safari11, edge16, node10, default esnext) -Advanced options: +` + colors.Bold + `Advanced options:` + colors.Default + ` --banner=... Text to be prepended to each output file --charset=utf8 Do not escape UTF-8 code points --color=... Force use of color terminal escapes (true | false) @@ -73,19 +80,21 @@ Advanced options: --tsconfig=... Use this tsconfig.json file instead of other ones --version Print the current version and exit (` + esbuildVersion + `) -Examples: - # Produces dist/entry_point.js and dist/entry_point.js.map +` + colors.Bold + `Examples:` + colors.Default + ` + ` + colors.Dim + `# Produces dist/entry_point.js and dist/entry_point.js.map` + colors.Default + ` esbuild --bundle entry_point.js --outdir=dist --minify --sourcemap - # Allow JSX syntax in .js files + ` + colors.Dim + `# Allow JSX syntax in .js files` + colors.Default + ` esbuild --bundle entry_point.js --outfile=out.js --loader:.js=jsx - # Substitute the identifier RELEASE for the literal true + ` + colors.Dim + `# Substitute the identifier RELEASE for the literal true` + colors.Default + ` esbuild example.js --outfile=out.js --define:RELEASE=true - # Provide input via stdin, get output via stdout + ` + colors.Dim + `# Provide input via stdin, get output via stdout` + colors.Default + ` esbuild --minify --loader=ts < input.ts > output.js + ` +} func main() { osArgs := os.Args[1:] @@ -100,7 +109,7 @@ func main() { switch { // Show help if a common help flag is provided case arg == "-h", arg == "-help", arg == "--help", arg == "/?": - fmt.Fprintf(os.Stderr, "%s\n", helpText) + logger.PrintText(os.Stdout, logger.LevelSilent, os.Args, helpText) os.Exit(0) // Special-case the version flag here @@ -149,7 +158,7 @@ func main() { // Print help text when there are no arguments if len(osArgs) == 0 && logger.GetTerminalInfo(os.Stdin).IsTTY { - fmt.Fprintf(os.Stderr, "%s\n", helpText) + logger.PrintText(os.Stdout, logger.LevelSilent, osArgs, helpText) os.Exit(0) } diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 834d19b67db..a0f0a70e9d2 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -353,13 +353,14 @@ func PrintMessageToStderr(osArgs []string, msg Msg) { type Colors struct { Default string + Bold string Dim string Red string Green string Underline string } -func PrintTextToStderr(level LogLevel, osArgs []string, callback func(Colors) string) { +func PrintText(file *os.File, level LogLevel, osArgs []string, callback func(Colors) string) { options := StderrOptionsForArgs(osArgs) // Skip logging these if these logs are disabled @@ -374,18 +375,19 @@ func PrintTextToStderr(level LogLevel, osArgs []string, callback func(Colors) st case ColorAlways: useColorEscapes = SupportsColorEscapes case ColorIfTerminal: - useColorEscapes = GetTerminalInfo(os.Stderr).UseColorEscapes + useColorEscapes = GetTerminalInfo(file).UseColorEscapes } var colors Colors if useColorEscapes { colors.Default = colorReset + colors.Bold = colorResetBold colors.Dim = colorResetDim colors.Red = colorRed colors.Green = colorGreen colors.Underline = colorResetUnderline } - writeStringWithColor(os.Stderr, callback(colors)) + writeStringWithColor(file, callback(colors)) } func NewDeferLog() Log { diff --git a/pkg/cli/cli_impl.go b/pkg/cli/cli_impl.go index 71c27f18f23..60130c8f6d0 100644 --- a/pkg/cli/cli_impl.go +++ b/pkg/cli/cli_impl.go @@ -613,7 +613,7 @@ func serveImpl(serveText string, osArgs []string) error { Port: uint16(port), Host: host, OnRequest: func(args api.ServeOnRequestArgs) { - logger.PrintTextToStderr(logger.LevelInfo, osArgs, func(colors logger.Colors) string { + logger.PrintText(os.Stderr, logger.LevelInfo, osArgs, func(colors logger.Colors) string { statusColor := colors.Red if args.Status == 200 { statusColor = colors.Green @@ -631,7 +631,7 @@ func serveImpl(serveText string, osArgs []string) error { } // Show what actually got bound if the port was 0 - logger.PrintTextToStderr(logger.LevelInfo, osArgs, func(colors logger.Colors) string { + logger.PrintText(os.Stderr, logger.LevelInfo, osArgs, func(colors logger.Colors) string { return fmt.Sprintf("%s\n > %shttp://%s:%d/%s\n\n", colors.Default, colors.Underline, result.Host, result.Port, colors.Default) })