Skip to content

Commit

Permalink
Unify syntax highlighting and ouput for --help (#16360)
Browse files Browse the repository at this point in the history
  • Loading branch information
fel1x-developer authored Jan 14, 2025
1 parent acae4a3 commit e5ea345
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 73 deletions.
45 changes: 25 additions & 20 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2503,6 +2503,11 @@ pub const Command = struct {
pub fn printHelp(comptime cmd: Tag, show_all_flags: bool) void {
switch (cmd) {

// the output of --help uses the following syntax highlighting
// template: <b>Usage<r>: <b><green>bun <command><r> <cyan>[flags]<r> <blue>[arguments]<r>
// use [foo] for multiple arguments or flags for foo.
// use <bar> to emphasize 'bar'

// these commands do not use Context
// .DiscordCommand => return try DiscordCommand.exec(allocator),
// .HelpCommand => return try HelpCommand.exec(allocator),
Expand All @@ -2527,7 +2532,7 @@ pub const Command = struct {

.InitCommand => {
const intro_text =
\\<b>Usage<r>: <b><green>bun init<r> <cyan>[...flags]<r> <blue>[\<entrypoint\> ...]<r>
\\<b>Usage<r>: <b><green>bun init<r> <cyan>[flags]<r> <blue>[\<entrypoints\>]<r>
\\ Initialize a Bun project in the current directory.
\\ Creates a package.json, tsconfig.json, and bunfig.toml if they don't exist.
\\
Expand All @@ -2537,7 +2542,7 @@ pub const Command = struct {
\\
\\<b>Examples:<r>
\\ <b><green>bun init<r>
\\ <b><green>bun init <cyan>--yes<r>
\\ <b><green>bun init<r> <cyan>--yes<r>
;

Output.pretty(intro_text ++ "\n", .{});
Expand All @@ -2546,37 +2551,37 @@ pub const Command = struct {

Command.Tag.BunxCommand => {
Output.prettyErrorln(
\\<b>Usage: bunx <r><cyan>[...flags]<r> \<package\><d>[@version] [...flags and arguments]<r>
\\<b>Usage<r>: <b><green>bunx<r> <cyan>[flags]<r> <blue>\<package\><r><d>\<@version\><r> [flags and arguments for the package]<r>
\\Execute an npm package executable (CLI), automatically installing into a global shared cache if not installed in node_modules.
\\
\\Flags:
\\ <cyan>--bun<r> Force the command to run with Bun instead of Node.js
\\
\\Examples<d>:<r>
\\ <b>bunx prisma migrate<r>
\\ <b>bunx prettier foo.js<r>
\\ <b>bunx<r> <cyan>--bun<r> <b>vite dev foo.js<r>
\\ <b><green>bunx<r> <blue>prisma<r> migrate<r>
\\ <b><green>bunx<r> <blue>prettier<r> foo.js<r>
\\ <b><green>bunx<r> <cyan>--bun<r> <blue>vite<r> dev foo.js<r>
\\
, .{});
},
Command.Tag.BuildCommand => {
const intro_text =
\\<b>Usage<r>:
\\ Transpile and bundle one or more files.
\\ <b><green>bun build<r> <cyan>[...flags]<r> [...entrypoints]
\\ <b><green>bun build<r> <cyan>[flags]<r> <blue>\<entrypoint\><r>
;

const outro_text =
\\<b>Examples:<r>
\\ <d>Frontend web apps:<r>
\\ <b><green>bun build<r> <blue>./src/index.ts<r> <cyan>--outfile=bundle.js<r>
\\ <b><green>bun build<r> <blue>./index.jsx ./lib/worker.ts<r> <cyan>--minify --splitting --outdir=out<r>
\\ <b><green>bun build<r> <cyan>--outfile=bundle.js<r> <blue>./src/index.ts<r>
\\ <b><green>bun build<r> <cyan>--minify --splitting --outdir=out<r> <blue>./index.jsx ./lib/worker.ts<r>
\\
\\ <d>Bundle code to be run in Bun (reduces server startup time)<r>
\\ <b><green>bun build<r> <blue>./server.ts<r> <cyan>--target=bun --outfile=server.js<r>
\\ <b><green>bun build<r> <cyan>--target=bun --outfile=server.js<r> <blue>./server.ts<r>
\\
\\ <d>Creating a standalone executable (see https://bun.sh/docs/bundler/executables)<r>
\\ <b><green>bun build<r> <blue>./cli.ts<r> <cyan>--compile --outfile=my-app<r>
\\ <b><green>bun build<r> <cyan>--compile --outfile=my-app<r> <blue>./cli.ts<r>
\\
\\A full list of flags is available at <magenta>https://bun.sh/docs/bundler<r>
\\
Expand All @@ -2592,24 +2597,24 @@ pub const Command = struct {
},
Command.Tag.TestCommand => {
const intro_text =
\\<b>Usage<r>: <b><green>bun test<r> <cyan>[...flags]<r> <blue>[\<pattern\>...]<r>
\\<b>Usage<r>: <b><green>bun test<r> <cyan>[flags]<r> <blue>[\<patterns\>]<r>
\\ Run all matching test files and print the results to stdout
;
const outro_text =
\\<b>Examples:<r>
\\ <d>Run all test files <r>
\\ <d>Run all test files<r>
\\ <b><green>bun test<r>
\\
\\ <d>Run all test files with "foo" or "bar" in the file name<r>
\\ <b><green>bun test foo bar<r>
\\ <b><green>bun test<r> <blue>foo bar<r>
\\
\\ <d>Run all test files, only including tests whose names includes "baz"<r>
\\ <b><green>bun test<r> <cyan>--test-name-pattern<r> baz<r>
\\ <b><green>bun test<r> <cyan>--test-name-pattern<r> <blue>baz<r>
\\
\\Full documentation is available at <magenta>https://bun.sh/docs/cli/test<r>
\\
;
// Output.pretty("\n", .{});

Output.pretty(intro_text, .{});
Output.flush();
Output.pretty("\n\n<b>Flags:<r>", .{});
Expand All @@ -2622,8 +2627,8 @@ pub const Command = struct {
Command.Tag.CreateCommand => {
const intro_text =
\\<b>Usage<r>:
\\ <b><green>bun create<r> <blue>\<template\><r> <cyan>[...flags]<r> <blue>[dest]<r>
\\ <b><green>bun create<r> <blue>\<username/repo\><r> <cyan>[...flags]<r> <blue>[dest]<r>
\\ <b><green>bun create<r> <magenta>\<template\><r> <cyan>[...flags]<r> <blue>dest<r>
\\ <b><green>bun create<r> <magenta>\<username/repo\><r> <cyan>[...flags]<r> <blue>dest<r>
\\
\\<b>Environment variables:<r>
\\ <cyan>GITHUB_TOKEN<r> <d>Supply a token to download code from GitHub with a higher rate limit<r>
Expand All @@ -2649,7 +2654,7 @@ pub const Command = struct {
},
Command.Tag.UpgradeCommand => {
const intro_text =
\\<b>Usage<r>: <b><green>bun upgrade<r> <cyan>[...flags]<r>
\\<b>Usage<r>: <b><green>bun upgrade<r> <cyan>[flags]<r>
\\ Upgrade Bun
;
const outro_text =
Expand Down Expand Up @@ -2677,7 +2682,7 @@ pub const Command = struct {
},
Command.Tag.ReplCommand => {
const intro_text =
\\<b>Usage<r>: <b><green>bun repl<r> <cyan>[...flags]<r>
\\<b>Usage<r>: <b><green>bun repl<r> <cyan>[flags]<r>
\\ Open a Bun REPL
\\
;
Expand Down
60 changes: 38 additions & 22 deletions src/cli/package_manager_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -102,33 +102,49 @@ pub const PackageManagerCommand = struct {
}

pub fn printHelp() void {
Output.prettyln(
\\<b><blue>bun pm<r>: Package manager utilities

// the output of --help uses the following syntax highlighting
// template: <b>Usage<r>: <b><green>bun <command><r> <cyan>[flags]<r> <blue>[arguments]<r>
// use [foo] for multiple arguments or flags for foo.
// use <bar> to emphasize 'bar'

const intro_text =
\\<b>Usage<r>: <b><green>bun pm<r> <cyan>[flags]<r> <blue>[\<command\>]<r>
\\ Run package manager utilities
;
const outro_text =
\\<b>Examples:<r>
\\
\\ bun pm <b>pack<r> create a tarball of the current workspace
\\ <d>├<r> <cyan>--dry-run<r> do everything except for writing the tarball to disk
\\ <d>├<r> <cyan>--destination<r> the directory the tarball will be saved in
\\ <d>├<r> <cyan>--ignore-scripts<r> don't run pre/postpack and prepare scripts
\\ <d>└<r> <cyan>--gzip-level<r> specify a custom compression level for gzip (0-9, default is 9)
\\ bun pm <b>bin<r> print the path to bin folder
\\ <d>└<r> <cyan>-g<r> print the <b>global<r> path to bin folder
\\ bun pm <b>ls<r> list the dependency tree according to the current lockfile
\\ <d>└<r> <cyan>--all<r> list the entire dependency tree according to the current lockfile
\\ bun pm <b>whoami<r> print the current npm username
\\ bun pm <b>hash<r> generate & print the hash of the current lockfile
\\ bun pm <b>hash-string<r> print the string used to hash the lockfile
\\ bun pm <b>hash-print<r> print the hash stored in the current lockfile
\\ bun pm <b>cache<r> print the path to the cache folder
\\ bun pm <b>cache rm<r> clear the cache
\\ bun pm <b>migrate<r> migrate another package manager's lockfile without installing anything
\\ bun pm <b>untrusted<r> print current untrusted dependencies with scripts
\\ bun pm <b>trust<r> <d>names ...<r> run scripts for untrusted dependencies and add to `trustedDependencies`
\\ <b><green>bun pm<r> <blue>pack<r> create a tarball of the current workspace
\\ <d>├<r> <cyan>--dry-run<r> do everything except for writing the tarball to disk
\\ <d>├<r> <cyan>--destination<r> the directory the tarball will be saved in
\\ <d>├<r> <cyan>--ignore-scripts<r> don't run pre/postpack and prepare scripts
\\ <d>└<r> <cyan>--gzip-level<r> specify a custom compression level for gzip (0-9, default is 9)
\\ <b><green>bun pm<r> <blue>bin<r> print the path to bin folder
\\ <d>└<r> <cyan>-g<r> print the <b>global<r> path to bin folder
\\ <b><green>bun pm<r> <blue>ls<r> list the dependency tree according to the current lockfile
\\ <d>└<r> <cyan>--all<r> list the entire dependency tree according to the current lockfile
\\ <b><green>bun pm<r> <blue>whoami<r> print the current npm username
\\ <b><green>bun pm<r> <blue>hash<r> generate & print the hash of the current lockfile
\\ <b><green>bun pm<r> <blue>hash-string<r> print the string used to hash the lockfile
\\ <b><green>bun pm<r> <blue>hash-print<r> print the hash stored in the current lockfile
\\ <b><green>bun pm<r> <blue>cache<r> print the path to the cache folder
\\ <b><green>bun pm<r> <blue>cache rm<r> clear the cache
\\ <b><green>bun pm<r> <blue>migrate<r> migrate another package manager's lockfile without installing anything
\\ <b><green>bun pm<r> <blue>untrusted<r> print current untrusted dependencies with scripts
\\ <b><green>bun pm<r> <blue>trust<r> <d>names ...<r> run scripts for untrusted dependencies and add to `trustedDependencies`
\\ <d>└<r> <cyan>--all<r> trust all untrusted dependencies
\\ bun pm <b>default-trusted<r> print the default trusted dependencies list
\\ <b><green>bun pm<r> <blue>default-trusted<r> print the default trusted dependencies list
\\
\\Learn more about these at <magenta>https://bun.sh/docs/cli/pm<r>
\\
, .{});
;

Output.pretty(intro_text, .{});
Output.flush();
Output.pretty("\n\n", .{});
Output.pretty(outro_text, .{});
Output.flush();
}

pub fn exec(ctx: Command.Context) !void {
Expand Down
Loading

0 comments on commit e5ea345

Please sign in to comment.