Skip to content

Commit

Permalink
link to documentation in command help text
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 8, 2020
1 parent 2a8a096 commit a8c1d25
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
31 changes: 20 additions & 11 deletions cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:]
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 5 additions & 3 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cli_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
})
Expand Down

0 comments on commit a8c1d25

Please sign in to comment.