diff options
author | 2023-10-06 22:55:42 -0700 | |
---|---|---|
committer | 2023-10-12 16:03:28 -0700 | |
commit | bd2c8ca3d3d4c9ab6b89917395b13989bb08b051 (patch) | |
tree | 5f2a159bc0e7f8cf8d5b732a76a1ac435e10dbc3 | |
parent | c12ae0c46451eddb23c44d3335e79b56f6307ed9 (diff) | |
download | bun-bd2c8ca3d3d4c9ab6b89917395b13989bb08b051.tar.gz bun-bd2c8ca3d3d4c9ab6b89917395b13989bb08b051.tar.zst bun-bd2c8ca3d3d4c9ab6b89917395b13989bb08b051.zip |
WIP
-rw-r--r-- | package.json | 8 | ||||
-rw-r--r-- | packages/bun-polyfills/tsconfig.json | 2 | ||||
-rw-r--r-- | src/cli.zig | 506 | ||||
-rw-r--r-- | src/deps/zig-clap/clap.zig | 125 | ||||
-rw-r--r-- | src/install/install.zig | 14 |
5 files changed, 446 insertions, 209 deletions
diff --git a/package.json b/package.json index 623f230cd..f74c6a1d1 100644 --- a/package.json +++ b/package.json @@ -29,5 +29,11 @@ "bun-webkit": "0.0.1-acc1e092b4758a712998d462ee54c52e75e71780" }, "version": "0.0.0", - "prettier": "./.prettierrc.cjs" + "prettier": "./.prettierrc.cjs", + "name": "core", + "module": "index.ts", + "type": "module", + "peerDependencies": { + "typescript": "^5.0.0" + } } diff --git a/packages/bun-polyfills/tsconfig.json b/packages/bun-polyfills/tsconfig.json index e90140678..1bf957d97 100644 --- a/packages/bun-polyfills/tsconfig.json +++ b/packages/bun-polyfills/tsconfig.json @@ -15,5 +15,5 @@ "outDir": "dist", "types": ["node"] }, - "include": ["src", "lib", "../bun-types/index.d.ts"], + "include": ["src", "lib", "../bun-types/index.d.ts"] } diff --git a/src/cli.zig b/src/cli.zig index 197e6ea9b..5ec2146ea 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -165,68 +165,108 @@ pub const Arguments = struct { pub const ParamType = clap.Param(clap.Help); - const shared_public_params = [_]ParamType{ + const base_params_ = [_]ParamType{ clap.parseParam("-h, --help Display this menu and exit") catch unreachable, - clap.parseParam("--all") catch unreachable, - clap.parseParam("-b, --bun Force a script or package to use Bun's runtime instead of Node.js (via symlinking node)") catch unreachable, + // clap.parseParam("--all") catch unreachable, clap.parseParam("--cwd <STR> Absolute path to resolve files & entry points from. This just changes the process' cwd.") catch unreachable, clap.parseParam("-c, --config <PATH>? Config file to load Bun from (e.g. -c bunfig.toml") catch unreachable, + clap.parseParam("<POS>...") catch unreachable, + }; + + const transpiler_params_ = [_]ParamType{ + clap.parseParam("--main-fields <STR>... Main fields to lookup in package.json. Defaults to --target dependent") catch unreachable, clap.parseParam("--extension-order <STR>... Defaults to: .tsx,.ts,.jsx,.js,.json ") catch unreachable, + clap.parseParam("--tsconfig-override <STR> Load tsconfig from path instead of cwd/tsconfig.json") catch unreachable, + clap.parseParam("-d, --define <STR>... Substitute K:V while parsing, e.g. --define process.env.NODE_ENV:\"development\". Values are parsed as JSON.") catch unreachable, + clap.parseParam("-e, --external <STR>... Exclude module from transpilation (can use * wildcards). ex: -e react") catch unreachable, + clap.parseParam("-l, --loader <STR>... Parse files with .ext:loader, e.g. --loader .js:jsx. Valid loaders: js, jsx, ts, tsx, json, toml, text, file, wasm, napi") catch unreachable, + clap.parseParam("--no-macros Disable macros from being executed in the bundler, transpiler and runtime") catch unreachable, clap.parseParam("--jsx-factory <STR> Changes the function called when compiling JSX elements using the classic JSX runtime") catch unreachable, clap.parseParam("--jsx-fragment <STR> Changes the function called when compiling JSX fragments") catch unreachable, clap.parseParam("--jsx-import-source <STR> Declares the module specifier to be used for importing the jsx and jsxs factory functions. Default: \"react\"") catch unreachable, clap.parseParam("--jsx-runtime <STR> \"automatic\" (default) or \"classic\"") catch unreachable, + }; + const runtime_params_ = [_]ParamType{ + // clap.parseParam("--cwd <STR> Absolute path to resolve files & entry points from. This just changes the process' cwd.") catch unreachable, + // clap.parseParam("-c, --config <PATH>? Config file to load Bun from (e.g. -c bunfig.toml") catch unreachable, + clap.parseParam("-r, --preload <STR>... Import a module before other modules are loaded") catch unreachable, - clap.parseParam("--main-fields <STR>... Main fields to lookup in package.json. Defaults to --target dependent") catch unreachable, - clap.parseParam("--no-summary Don't print a summary (when generating .bun)") catch unreachable, - clap.parseParam("-v, --version Print version and exit") catch unreachable, - clap.parseParam("--revision Print version with revision and exit") catch unreachable, - clap.parseParam("--tsconfig-override <STR> Load tsconfig from path instead of cwd/tsconfig.json") catch unreachable, - clap.parseParam("-d, --define <STR>... Substitute K:V while parsing, e.g. --define process.env.NODE_ENV:\"development\". Values are parsed as JSON.") catch unreachable, - clap.parseParam("-e, --external <STR>... Exclude module from transpilation (can use * wildcards). ex: -e react") catch unreachable, - clap.parseParam("-l, --loader <STR>... Parse files with .ext:loader, e.g. --loader .js:jsx. Valid loaders: js, jsx, ts, tsx, json, toml, text, file, wasm, napi") catch unreachable, - clap.parseParam("-u, --origin <STR> Rewrite import URLs to start with --origin. Default: \"\"") catch unreachable, - clap.parseParam("-p, --port <STR> Port to serve Bun's dev server on. Default: \"3000\"") catch unreachable, + clap.parseParam("--hot Enable auto reload in the Bun runtime, test runner, or bundler") catch unreachable, + clap.parseParam("--watch Automatically restart the process on file change") catch unreachable, + // clap.parseParam("--main-fields <STR>... Main fields to lookup in package.json. Defaults to --target dependent") catch unreachable, + // clap.parseParam("--no-summary Don't print a summary (when generating .bun)") catch unreachable, + + // clap.parseParam("-u, --origin <STR> Rewrite import URLs to start with --origin. Default: \"\"") catch unreachable, + // clap.parseParam("-p, --port <STR> Port to serve Bun's dev server on. Default: \"3000\"") catch unreachable, clap.parseParam("--smol Use less memory, but run garbage collection more often") catch unreachable, - clap.parseParam("--minify Minify (experimental)") catch unreachable, - clap.parseParam("--minify-syntax Minify syntax and inline data (experimental)") catch unreachable, - clap.parseParam("--minify-whitespace Minify whitespace (experimental)") catch unreachable, - clap.parseParam("--minify-identifiers Minify identifiers") catch unreachable, - clap.parseParam("--no-macros Disable macros from being executed in the bundler, transpiler and runtime") catch unreachable, - clap.parseParam("--target <STR> The intended execution environment for the bundle. \"browser\", \"bun\" or \"node\"") catch unreachable, + // clap.parseParam("--minify Minify (experimental)") catch unreachable, + // clap.parseParam("--minify-syntax Minify syntax and inline data (experimental)") catch unreachable, + // clap.parseParam("--minify-whitespace Minify whitespace (experimental)") catch unreachable, + // clap.parseParam("--minify-identifiers Minify identifiers") catch unreachable, + clap.parseParam("--inspect <STR>? Activate Bun's Debugger") catch unreachable, clap.parseParam("--inspect-wait <STR>? Activate Bun's Debugger, wait for a connection before executing") catch unreachable, clap.parseParam("--inspect-brk <STR>? Activate Bun's Debugger, set breakpoint on first line of code and wait") catch unreachable, clap.parseParam("--if-present Exit if the entrypoint does not exist") catch unreachable, - clap.parseParam("<POS>... ") catch unreachable, - }; - - // note: we are keeping --port and --origin as it can be reused for bun - // build and elsewhere - pub const not_bun_dev_flags = [_]ParamType{ - clap.parseParam("--hot Enable auto reload in the Bun runtime, test runner, or bundler") catch unreachable, - clap.parseParam("--watch Automatically restart the process on file change") catch unreachable, clap.parseParam("--no-install Disable auto install in the Bun runtime") catch unreachable, clap.parseParam("-i Automatically install dependencies and use global cache in Bun's runtime, equivalent to --install=fallback") catch unreachable, clap.parseParam("--install <STR> Install dependencies automatically when no node_modules are present, default: \"auto\". \"force\" to ignore node_modules, fallback to install any missing") catch unreachable, clap.parseParam("--prefer-offline Skip staleness checks for packages in the Bun runtime and resolve from disk") catch unreachable, clap.parseParam("--prefer-latest Use the latest matching versions of packages in the Bun runtime, always checking npm") catch unreachable, - clap.parseParam("--silent Don't repeat the command for bun run") catch unreachable, }; - const public_params = shared_public_params ++ not_bun_dev_flags; + // const runtime_params = [_]ParamType{ + // clap.parseParam("--silent Don't repeat the command for bun run") catch unreachable, + // }; + // const positional_params_ = [_]ParamType{ + // clap.parseParam("<POS>...") catch unreachable, + // }; - const debug_params = [_]ParamType{ - clap.parseParam("--dump-environment-variables Dump environment variables from .env and process as JSON and quit. Useful for debugging") catch unreachable, - clap.parseParam("--dump-limits Dump system limits. Useful for debugging") catch unreachable, + const auto_only_params = [_]ParamType{ + // clap.parseParam("-h, --help Display this menu and exit") catch unreachable, + clap.parseParam("--all") catch unreachable, + clap.parseParam("-v, --version Print version and exit") catch unreachable, + clap.parseParam("--revision Print version with revision and exit") catch unreachable, + clap.parseParam("--silent Don't repeat the command for bun run") catch unreachable, + clap.parseParam("-b, --bun Force a script or package to use Bun's runtime instead of Node.js (via symlinking node)") catch unreachable, }; + const auto_params = auto_only_params ++ runtime_params_ ++ transpiler_params_ ++ base_params_; - pub const params = public_params ++ debug_params; + const run_only_params = [_]ParamType{ + clap.parseParam("--silent Don't repeat the command for bun run") catch unreachable, + clap.parseParam("-b, --bun Force a script or package to use Bun's runtime instead of Node.js (via symlinking node)") catch unreachable, + }; + const run_params = run_only_params ++ runtime_params_ ++ transpiler_params_ ++ base_params_; + + // these commands don't use clap + // Command.Tag.BunxCommand + // Command.Tag.InitCommand + // Command.Tag.CreateCommand + // Command.Tag.DiscordCommand + // Command.Tag.GetCompletionsCommand + // Command.Tag.HelpCommand + // Command.Tag.InstallCompletionsCommand + // Command.Tag.PackageManagerCommand + // Command.Tag.UpgradeCommand + // Command.Tag.ReplCommand + // Command.Tag.ReservedCommand + + // const public_params = runtime_params_ ++ auto_install_params; + + // these were only used in bun dev + // const debug_params = [_]ParamType{ + // clap.parseParam("--dump-environment-variables Dump environment variables from .env and process as JSON and quit. Useful for debugging") catch unreachable, + // clap.parseParam("--dump-limits Dump system limits. Useful for debugging") catch unreachable, + // }; + + // pub const params = public_params ++ debug_params; const build_only_params = [_]ParamType{ clap.parseParam("--format <STR> Specifies the module format to build to. Only esm is supported.") catch unreachable, + clap.parseParam("--target <STR> The intended execution environment for the bundle. \"browser\", \"bun\" or \"node\"") catch unreachable, clap.parseParam("--outdir <STR> Default to \"dist\" if multiple files") catch unreachable, clap.parseParam("--outfile <STR> Write to a file") catch unreachable, + clap.parseParam("--watch Automatically restart the process on file change") catch unreachable, clap.parseParam("--root <STR> Root directory used for multiple entry points") catch unreachable, clap.parseParam("--splitting Enable code splitting") catch unreachable, clap.parseParam("--public-path <STR> A prefix to be appended to any import paths in bundled code") catch unreachable, @@ -237,7 +277,13 @@ pub const Arguments = struct { clap.parseParam("--server-components Enable React Server Components (experimental)") catch unreachable, clap.parseParam("--no-bundle Transpile file only, do not bundle") catch unreachable, clap.parseParam("--compile Generate a standalone Bun executable containing your bundled code") catch unreachable, + clap.parseParam("--minify Minify (experimental)") catch unreachable, + clap.parseParam("--minify-syntax Minify syntax and inline data (experimental)") catch unreachable, + clap.parseParam("--minify-whitespace Minify whitespace (experimental)") catch unreachable, + clap.parseParam("--minify-identifiers Minify identifiers") catch unreachable, + clap.parseParam("--dump-environment-variables") catch unreachable, }; + pub const build_params = build_only_params ++ transpiler_params_ ++ base_params_; // TODO: update test completions const test_only_params = [_]ParamType{ @@ -250,10 +296,7 @@ pub const Arguments = struct { clap.parseParam("--bail <NUMBER>?") catch unreachable, clap.parseParam("-t, --test-name-pattern <STR>") catch unreachable, }; - - const build_params_public = public_params ++ build_only_params; - pub const build_params = build_params_public ++ debug_params; - pub const test_params = params ++ test_only_params; + pub const test_params = test_only_params ++ runtime_params_ ++ transpiler_params_ ++ base_params_; fn printVersionAndExit() noreturn { @setCold(true); @@ -378,9 +421,10 @@ pub const Arguments = struct { pub fn parse(allocator: std.mem.Allocator, ctx: *Command.Context, comptime cmd: Command.Tag) !Api.TransformOptions { std.debug.print("starting parse\n", .{}); var diag = clap.Diagnostic{}; - const params_to_use = comptime cmd.params(); + const params_to_parse = comptime cmd.params(); + const params_to_print = comptime cmd.params_to_print(); - var args = clap.parse(clap.Help, params_to_use, .{ + var args = clap.parse(clap.Help, params_to_parse, .{ .diagnostic = &diag, .allocator = allocator, .stop_after_positional_at = if (cmd == .RunCommand) 2 else if (cmd == .AutoCommand) @@ -390,7 +434,7 @@ pub const Arguments = struct { }) catch |err| { std.debug.print("error caught\n", .{}); // Report useful error and exit - clap.help(Output.errorWriter(), params_to_use) catch {}; + clap.help(Output.errorWriter(), params_to_parse) catch {}; Output.errorWriter().writeAll("\n") catch {}; diag.report(Output.errorWriter(), err) catch {}; std.debug.print("done printing\n", .{}); @@ -398,13 +442,26 @@ pub const Arguments = struct { }; std.debug.print("past parsing\n", .{}); - - if (args.flag("--version")) { - printVersionAndExit(); + // const print_all_help = args.flag("--all"); + const print_help = args.flag("--help"); + if (print_help) { + std.debug.print("printing help\n", .{}); + cmd.print_helptext(params_to_print); + // clap.help(Output.writer(), params_to_parse[0..params_to_parse.len]) catch {}; + // Output.prettyln("\n-------\n\n", .{}); + Output.flush(); + // HelpCommand.printWithReason(.explicit); + Global.exit(0); } - if (args.flag("--revision")) { - printRevisionAndExit(); + if (cmd == .AutoCommand) { + if (args.flag("--version")) { + printVersionAndExit(); + } + + if (args.flag("--revision")) { + printRevisionAndExit(); + } } var cwd: []u8 = undefined; @@ -517,13 +574,13 @@ pub const Arguments = struct { else null; - if (args.option("--origin")) |origin| { - opts.origin = origin; - } + // if (args.option("--origin")) |origin| { + // opts.origin = origin; + // } - if (args.option("--port")) |port_str| { - opts.port = std.fmt.parseInt(u16, port_str, 10) catch return error.InvalidPort; - } + // if (args.option("--port")) |port_str| { + // opts.port = std.fmt.parseInt(u16, port_str, 10) catch return error.InvalidPort; + // } opts.serve = false; // TODO opts.main_fields = args.options("--main-fields"); // we never actually supported inject. @@ -532,11 +589,43 @@ pub const Arguments = struct { ctx.passthrough = args.remaining(); - opts.no_summary = args.flag("--no-summary"); + // opts.no_summary = args.flag("--no-summary"); if (cmd == .AutoCommand or cmd == .RunCommand or cmd == .TestCommand) { std.debug.print("auto/run/test\n", .{}); const preloads = args.options("--preload"); + + if (args.flag("--hot")) { + ctx.debug.hot_reload = .hot; + } else if (args.flag("--watch")) { + ctx.debug.hot_reload = .watch; + bun.auto_reload_on_crash = true; + } + + ctx.debug.offline_mode_setting = if (args.flag("--prefer-offline")) + Bunfig.OfflineMode.offline + else if (args.flag("--prefer-latest")) + Bunfig.OfflineMode.latest + else + Bunfig.OfflineMode.online; + + if (args.flag("--no-install")) { + ctx.debug.global_cache = .disable; + } else if (args.flag("-i")) { + ctx.debug.global_cache = .fallback; + } else if (args.option("--install")) |enum_value| { + // -i=auto --install=force, --install=disable + if (options.GlobalCache.Map.get(enum_value)) |result| { + ctx.debug.global_cache = result; + // -i, --install + } else if (enum_value.len == 0) { + ctx.debug.global_cache = options.GlobalCache.force; + } else { + Output.prettyErrorln("Invalid value for --install: \"{s}\". Must be either \"auto\", \"fallback\", \"force\", or \"disable\"\n", .{enum_value}); + Global.exit(1); + } + } + if (ctx.preloads.len > 0 and preloads.len > 0) { var all = std.ArrayList(string).initCapacity(ctx.allocator, ctx.preloads.len + preloads.len) catch unreachable; all.appendSliceAssumeCapacity(ctx.preloads); @@ -584,33 +673,39 @@ pub const Arguments = struct { opts.origin = try std.fmt.allocPrint(allocator, "http://localhost:{d}/", .{opts.port.?}); } - const print_all_help = args.flag("--all"); - const print_help = args.flag("--help"); - - if (print_help) { - std.debug.print("printing help\n", .{}); - cmd.print_helptext(print_all_help); - // clap.help(Output.writer(), params_to_use[0..params_to_use.len]) catch {}; - // Output.prettyln("\n-------\n\n", .{}); - Output.flush(); - // HelpCommand.printWithReason(.explicit); - Global.exit(0); - } - - ctx.debug.dump_environment_variables = args.flag("--dump-environment-variables"); - ctx.debug.dump_limits = args.flag("--dump-limits"); + // ctx.debug.dump_environment_variables = args.flag("--dump-environment-variables"); + // ctx.debug.dump_limits = args.flag("--dump-limits"); var output_dir: ?string = null; var output_file: ?string = null; - const minify_flag = args.flag("--minify"); - ctx.bundler_options.minify_syntax = minify_flag or args.flag("--minify-syntax"); - ctx.bundler_options.minify_whitespace = minify_flag or args.flag("--minify-whitespace"); - ctx.bundler_options.minify_identifiers = minify_flag or args.flag("--minify-identifiers"); - if (cmd == .BuildCommand) { ctx.bundler_options.transform_only = args.flag("--no-bundle"); + const minify_flag = args.flag("--minify"); + ctx.bundler_options.minify_syntax = minify_flag or args.flag("--minify-syntax"); + ctx.bundler_options.minify_whitespace = minify_flag or args.flag("--minify-whitespace"); + ctx.bundler_options.minify_identifiers = minify_flag or args.flag("--minify-identifiers"); + + const TargetMatcher = strings.ExactSizeMatcher(8); + + if (args.option("--target")) |_target| { + opts.target = opts.target orelse switch (TargetMatcher.match(_target)) { + TargetMatcher.case("browser") => Api.Target.browser, + TargetMatcher.case("node") => Api.Target.node, + TargetMatcher.case("macro") => if (cmd == .BuildCommand) Api.Target.bun_macro else Api.Target.bun, + TargetMatcher.case("bun") => Api.Target.bun, + else => invalidTarget(&diag, _target), + }; + + ctx.debug.run_in_bun = opts.target.? == .bun; + } + + if (args.flag("--watch")) { + ctx.debug.hot_reload = .watch; + bun.auto_reload_on_crash = true; + } + if (args.flag("--compile")) { ctx.bundler_options.compile = true; } @@ -723,57 +818,13 @@ pub const Arguments = struct { var jsx_runtime = args.option("--jsx-runtime"); const react_fast_refresh = true; - if (args.flag("--hot")) { - ctx.debug.hot_reload = .hot; - } else if (args.flag("--watch")) { - ctx.debug.hot_reload = .watch; - bun.auto_reload_on_crash = true; - } - - ctx.debug.offline_mode_setting = if (args.flag("--prefer-offline")) - Bunfig.OfflineMode.offline - else if (args.flag("--prefer-latest")) - Bunfig.OfflineMode.latest - else - Bunfig.OfflineMode.online; - - if (args.flag("--no-install")) { - ctx.debug.global_cache = .disable; - } else if (args.flag("-i")) { - ctx.debug.global_cache = .fallback; - } else if (args.option("--install")) |enum_value| { - // -i=auto --install=force, --install=disable - if (options.GlobalCache.Map.get(enum_value)) |result| { - ctx.debug.global_cache = result; - // -i, --install - } else if (enum_value.len == 0) { - ctx.debug.global_cache = options.GlobalCache.force; - } else { - Output.prettyErrorln("Invalid value for --install: \"{s}\". Must be either \"auto\", \"fallback\", \"force\", or \"disable\"\n", .{enum_value}); - Global.exit(1); - } + if (cmd == .AutoCommand or cmd == .RunCommand) { + ctx.debug.silent = args.flag("--silent"); + ctx.debug.run_in_bun = args.flag("--bun") or ctx.debug.run_in_bun; } - ctx.debug.silent = args.flag("--silent"); - opts.resolve = Api.ResolveMode.lazy; - const TargetMatcher = strings.ExactSizeMatcher(8); - - if (args.option("--target")) |_target| { - opts.target = opts.target orelse switch (TargetMatcher.match(_target)) { - TargetMatcher.case("browser") => Api.Target.browser, - TargetMatcher.case("node") => Api.Target.node, - TargetMatcher.case("macro") => if (cmd == .BuildCommand) Api.Target.bun_macro else Api.Target.bun, - TargetMatcher.case("bun") => Api.Target.bun, - else => invalidTarget(&diag, _target), - }; - - ctx.debug.run_in_bun = opts.target.? == .bun; - } - - ctx.debug.run_in_bun = args.flag("--bun") or ctx.debug.run_in_bun; - if (jsx_factory != null or jsx_fragment != null or jsx_import_source != null or @@ -1309,10 +1360,6 @@ pub const Command = struct { .HelpCommand => return try HelpCommand.exec(allocator), .InitCommand => return try InitCommand.exec(allocator, bun.argv()), .ReservedCommand => return try ReservedCommand.exec(allocator), - else => {}, - } - - switch (tag) { .BuildCommand => { if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .BuildCommand) unreachable; const ctx = try Command.Context.create(allocator, log, .BuildCommand); @@ -1716,7 +1763,6 @@ pub const Command = struct { // Output.flush(); try HelpCommand.exec(allocator); }, - else => unreachable, } } @@ -1818,40 +1864,71 @@ pub const Command = struct { pub fn params(comptime cmd: Tag) []const Arguments.ParamType { return &comptime switch (cmd) { + Command.Tag.AutoCommand => Arguments.auto_params, + Command.Tag.RunCommand => Arguments.run_params, Command.Tag.BuildCommand => Arguments.build_params, Command.Tag.TestCommand => Arguments.test_params, - else => Arguments.params, + Command.Tag.BunxCommand => Arguments.run_params, + else => Arguments.base_params_ ++ Arguments.runtime_params_ ++ Arguments.transpiler_params_, + }; + } + + pub fn params_to_print(comptime cmd: Tag) []const Arguments.ParamType { + return &comptime switch (cmd) { + Command.Tag.BuildCommand => Arguments.build_only_params, + Command.Tag.TestCommand => Arguments.test_only_params, + Command.Tag.InstallCommand => Install.PackageManager.install_params, + Command.Tag.AddCommand => Install.PackageManager.add_params, + Command.Tag.UpdateCommand => Install.PackageManager.update_params, + Command.Tag.RemoveCommand => Install.PackageManager.remove_params, + Command.Tag.LinkCommand => Install.PackageManager.link_params, + Command.Tag.UnlinkCommand => Install.PackageManager.unlink_params, + Command.Tag.AutoCommand => Arguments.auto_params, + Command.Tag.RunCommand => Arguments.run_params, + Command.Tag.BunxCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.InitCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.CreateCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.DiscordCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.GetCompletionsCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.HelpCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.InstallCompletionsCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.PackageManagerCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.UpgradeCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.ReplCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, + Command.Tag.ReservedCommand => Arguments.runtime_params_ ++ Arguments.transpiler_params_, }; } - const shared_flags = - \\ asdf - ; - pub fn print_helptext(cmd: Tag, all: bool) void { - return switch (cmd) { + // fn print_helptext_(intro: []u8, outro: []u8, params_list: []const Arguments.ParamType) void { + // Output.pretty("\n{s}", .{intro}); + // Output.flush(); + // Output.pretty("\n<b>Flags:<r>", .{}); + // Output.flush(); + // clap.simpleHelp(params_list); + // Output.pretty("\n\n{s}", .{outro}); + // Output.flush(); + // } + + pub fn print_helptext(cmd: Tag, params_list: []const Arguments.ParamType) void { + switch (cmd) { + Command.Tag.AutoCommand => { + Output.pretty("asdf", .{}); + Output.flush(); + }, + Command.Tag.RunCommand => { + Output.pretty("asdf", .{}); + Output.flush(); + }, + Command.Tag.BunxCommand => { + Output.pretty("asdf", .{}); + Output.flush(); + }, Command.Tag.BuildCommand => { - const build_helptext = + const intro_text = \\<b>Usage<r>: <b><green>bun build<r> [flags] [...entrypoints] - \\ - \\<b>Flags:<r> - // \\ <cyan>--format<r> Specify the module format of the bundle. Only "esm". - \\ <cyan>--target<r> The intended execution environment for the bundle. - \\ One of <magenta>browser<r> | <magenta>bun<r> | <magenta>node<r> - \\ <cyan>--outfile<r> Write the output to a specific file (default: stdout) - \\ <cyan>--outdir<r> Write the output to a directory (required for splitting) - \\ <cyan>--sourcemap<r> Generate sourcemaps - \\ One of <magenta>none<r> | <magenta>inline<r> | <magenta>external<r> - \\ <cyan>--splitting<r> Enable code splitting (requires --outdir) - \\ <cyan>--watch<r> Run bundler in watch mode - \\ <cyan>--compile<r> Compile code into a standalone executable - \\ <cyan>--root<r> Root directory used for multiple entry points - \\ <cyan>--public-path<r> A prefix, often a URL, appended to all import paths - \\ <cyan>--minify<r> Enable all minification flags - \\ <cyan>--minify-whitespace<r> Remove unneeded whitespace - \\ <cyan>--minify-syntax<r> Transform code to use less syntax - \\ <cyan>--minify-identifiers<r> Shorten variable names - \\ - \\ {s} + ; + + 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> @@ -1867,24 +1944,22 @@ pub const Command = struct { \\ ; - Output.pretty(build_helptext, .{if (all) "<cyan>if</r>" else "<green>else</r>"}); + Output.pretty(intro_text, .{}); + Output.pretty("\n\n", .{}); + Output.flush(); + Output.pretty("<b>Flags:<r>", .{}); + Output.flush(); + clap.simpleHelp(params_list); + Output.pretty("\n\n", .{}); + Output.pretty(outro_text, .{}); Output.flush(); }, Command.Tag.TestCommand => { - const test_helptext = + const intro_text = \\<b>Usage<r>: <b><green>bun test<r> [flags] [patterns...] \\ Run all matching test files and print the results to stdout - \\ - \\<b>Flags:<r> - \\ <cyan> --timeout N<r> Set the per-test timeout in milliseconds. Default 5000. - \\ <cyan> --update-snapshots<r> Update snapshot files - \\ <cyan> --rerun-each N<r> Re-run each test file N times, helps catch certain bugs - \\ <cyan> --only<r> Only run tests that are marked with "test.only()" - \\ <cyan> --todo<r> Include tests that are marked with "test.todo()" - \\ <cyan> --coverage<r> Generate a coverage profile - \\ <cyan> --bail [N]<r> Exit the test suite after N failures. Default 1. - \\ <cyan>-t, --test-name-pattern [...]<r> Run only tests with a name that matches the given regex. - \\ + ; + const outro_text = \\<b>Examples:<r> \\ <d>Run all test files <r> \\ <b><green>bun test<r> @@ -1895,44 +1970,59 @@ pub const Command = struct { \\ <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> \\ - \\Full documenatation is available at <magenta>https://bun.sh/docs/cli/test<r> + \\Full documentation is available at <magenta>https://bun.sh/docs/cli/test<r> ; - Output.pretty(test_helptext, .{}); + Output.pretty("\n", .{}); + Output.pretty(intro_text, .{}); Output.flush(); - }, - Command.Tag.AddCommand => { - Output.pretty("asdf", .{}); + Output.pretty("\n<b>Flags:<r>", .{}); Output.flush(); - }, - Command.Tag.AutoCommand => { - Output.pretty("asdf", .{}); + clap.simpleHelp(params_list); + Output.pretty("\n\n", .{}); + Output.pretty(outro_text, .{}); Output.flush(); }, - Command.Tag.BuildCommand => { - Output.pretty("asdf", .{}); + Command.Tag.InitCommand => { + const intro_text = + \\<b>Usage<r>: <b><green>bun init<r> [flags] + \\ Run all matching test files and print the results to stdout + ; + const outro_text = + \\<b>Examples:<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> + \\ + \\ <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> + \\ + \\Full documentation is available at <magenta>https://bun.sh/docs/cli/test<r> + ; + Output.pretty("\n", .{}); + Output.pretty(intro_text, .{}); Output.flush(); - }, - Command.Tag.BunxCommand => { - Output.pretty("asdf", .{}); + Output.pretty("\n<b>Flags:<r>", .{}); Output.flush(); - }, - Command.Tag.CreateCommand => { - Output.pretty("asdf", .{}); + clap.simpleHelp(params_list); + Output.pretty("\n\n", .{}); + Output.pretty(outro_text, .{}); Output.flush(); }, - Command.Tag.DiscordCommand => { + Command.Tag.CreateCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.GetCompletionsCommand => { + Command.Tag.HelpCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.HelpCommand => { + Command.Tag.UpgradeCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.InitCommand => { + Command.Tag.ReplCommand => { Output.pretty("asdf", .{}); Output.flush(); }, @@ -1940,27 +2030,44 @@ pub const Command = struct { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.InstallCompletionsCommand => { - Output.pretty("asdf", .{}); + Command.Tag.AddCommand => { + const intro_text = + \\<b>Usage<r>: <b><green>bun add<r> [flags] \<pkg\> [...\<pkg\>] + \\ Install a package and add it to your package.json + ; + const outro_text = + \\<b>Examples:<r> + \\ <d>Add a dependency from the npm registry<r> + \\ <b><green>bun add zod<r> + \\ <b><green>bun add zod@next<r> + \\ <b><green>bun add zod@3.0.0<r> + \\ + \\ <d>Add a dev, optional, or peer dependency <r> + \\ <b><green>bun add -d typescript<r> + \\ <b><green>bun add -o lodash<r> + \\ <b><green>bun add --peer esbuild<r> + \\ + \\Full documentation is available at <magenta>https://bun.sh/docs/cli/install<r> + ; + Output.pretty("\n", .{}); + Output.pretty(intro_text, .{}); Output.flush(); - }, - Command.Tag.LinkCommand => { - Output.pretty("asdf", .{}); + Output.pretty("\n<b>Flags:<r>", .{}); Output.flush(); - }, - Command.Tag.PackageManagerCommand => { - Output.pretty("asdf", .{}); + clap.simpleHelp(params_list); + Output.pretty("\n\n", .{}); + Output.pretty(outro_text, .{}); Output.flush(); }, Command.Tag.RemoveCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.RunCommand => { + Command.Tag.UpdateCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.TestCommand => { + Command.Tag.LinkCommand => { Output.pretty("asdf", .{}); Output.flush(); }, @@ -1968,27 +2075,26 @@ pub const Command = struct { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.UpdateCommand => { + Command.Tag.PackageManagerCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.UpgradeCommand => { + Command.Tag.DiscordCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.ReplCommand => { + Command.Tag.GetCompletionsCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - Command.Tag.ReservedCommand => { + Command.Tag.InstallCompletionsCommand => { Output.pretty("asdf", .{}); Output.flush(); }, - // Command.Tag else => { HelpCommand.printWithReason(.explicit); }, - }; + } } pub fn readGlobalConfig(this: Tag) bool { diff --git a/src/deps/zig-clap/clap.zig b/src/deps/zig-clap/clap.zig index ced958e3a..bba58d445 100644 --- a/src/deps/zig-clap/clap.zig +++ b/src/deps/zig-clap/clap.zig @@ -5,6 +5,7 @@ const heap = std.heap; const io = std.io; const mem = std.mem; const testing = std.testing; +const Output = @import("../../output.zig"); pub const args = @import("clap/args.zig"); @@ -222,6 +223,9 @@ pub const Diagnostic = struct { Arg{ .prefix = "", .name = diag.arg }; std.debug.print("switching in diag.report", .{}); + Output.pretty("Invalid argument '{s}{s}'\n", .{ a.prefix, a.name }); + Output.pretty("{}", .{err}); + Output.flush(); switch (err) { error.DoesntTakeValue => try stream.print("The argument '{s}{s}' does not take a value\n", .{ a.prefix, a.name }), error.MissingValue => try stream.print("The argument '{s}{s}' requires a value but none was supplied\n", .{ a.prefix, a.name }), @@ -436,6 +440,127 @@ pub fn helpEx( ); } +pub fn simplePrintParam( + param: Param(Help), + // comptime Error: type, + // context: anytype, + // valueText: fn (@TypeOf(context), Param(Id)) Error![]const u8, +) !void { + Output.pretty("\n", .{}); + if (param.names.short) |s| { + Output.pretty("<cyan>-{c}<r>", .{s}); + } else { + Output.pretty(" ", .{}); + } + if (param.names.long) |l| { + if (param.names.short) |_| { + Output.pretty(", ", .{}); + } else { + Output.pretty(" ", .{}); + } + + Output.pretty("<cyan>--{s}<r>", .{l}); + } + + // switch (param.takes_value) { + // .none => {}, + // .one => Output.pretty(" \\<{s}\\>", .{getValueSimple(param)}), + // .one_optional => Output.pretty(" \\<{s}\\>?", .{getValueSimple(param)}), + // .many => Output.pretty(" \\<{s}\\>...", .{getValueSimple(param)}), + // } +} +pub fn simpleHelp( + // stream: anytype, + params: []const Param(Help), + // helpText: *const fn (Param(Help)) []const u8, + // valueText: *const fn (Param(Help)) []const u8, +) void { + // _ = stream; + // const Context = struct { + // helpText: *const fn (Param(Help)) []const u8, + // valueText: *const fn (Param(Help)) []const u8, + + // pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 { + // return c.helpText(p); + // } + + // pub fn value(c: @This(), p: Param(Id)) error{}![]const u8 { + // return c.valueText(p); + // } + // }; + + // return helpFull( + // stream, + // Id, + // params, + // error{}, + // Context{ + // .helpText = helpText, + // .valueText = valueText, + // }, + // Context.help, + // Context.value, + // ); + + const max_spacing = blk: { + var res: usize = 2; + for (params) |param| { + // param.names.long.?.len orelse 0; + // var flags_len = brk: { + // if (param.names.long) |l| { + // break :brk l.len; + // } + // break :brk 0; + // }; + var flags_len = if (param.names.long) |l| l.len else 0; + // if(param.names.short) flags_len += 4; + // var cs = io.countingWriter(io.null_writer); + // var flag_text = getValueSimple(param); + // try printParam(cs.writer(), Help, param, error{}, context, valueText); + if (res < flags_len) + res = flags_len; + } + + break :blk res; + }; + + for (params) |param| { + if (param.names.short == null and param.names.long == null) + continue; + + // create a string with spaces_len spaces + const default_allocator = @import("root").bun.default_allocator; + + // var spaces_before = default_allocator.alloc(u8, num_spaces_after) catch unreachable; + // defer default_allocator.free(spaces_before); + // for (0..spaces_before) |i| { + // spaces_before[i] = ' '; + // } + + const flags_len = if (param.names.long) |l| l.len else 0; + const num_spaces_after = max_spacing - flags_len; + var spaces_after = default_allocator.alloc(u8, num_spaces_after) catch unreachable; + defer default_allocator.free(spaces_after); + for (0..num_spaces_after) |i| { + spaces_after[i] = ' '; + } + + simplePrintParam(param) catch unreachable; + Output.pretty(" ", .{}); + const desc_text = getHelpSimple(param); + Output.pretty("{s} {s}", .{ spaces_after, desc_text }); + // print space spaces_len times + + // if (help_text.len > 0) { + // var cs = io.countingWriter(stream); + // try stream.print("\t", .{}); + // try printParam(cs.writer(), Help, param, error{}, context, valueText); + // try stream.writeByteNTimes(' ', max_spacing - @as(usize, @intCast(cs.bytes_written))); + // try stream.print("\t{s}\n", .{getHelpSimple(param)}); + // } + } +} + pub const Help = struct { msg: []const u8 = "", value: []const u8 = "", diff --git a/src/install/install.zig b/src/install/install.zig index 6bff738df..9b2dc4502 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -6062,7 +6062,7 @@ pub const PackageManager = struct { clap.parseParam("--help Print this help menu") catch unreachable, }; - const install_params = install_params_ ++ [_]ParamType{ + pub const install_params = install_params_ ++ [_]ParamType{ clap.parseParam("-d, --dev Add dependency to \"devDependencies\"") catch unreachable, clap.parseParam("-D, --development") catch unreachable, clap.parseParam("--optional Add dependency to \"optionalDependencies\"") catch unreachable, @@ -6070,15 +6070,15 @@ pub const PackageManager = struct { clap.parseParam("<POS> ... ") catch unreachable, }; - const update_params = install_params_ ++ [_]ParamType{ + pub const update_params = install_params_ ++ [_]ParamType{ clap.parseParam("<POS> ... \"name\" of packages to update") catch unreachable, }; - const pm_params = install_params_ ++ [_]ParamType{ + pub const pm_params = install_params_ ++ [_]ParamType{ clap.parseParam("<POS> ... ") catch unreachable, }; - const add_params = install_params_ ++ [_]ParamType{ + pub const add_params = install_params_ ++ [_]ParamType{ clap.parseParam("-d, --dev Add dependency to \"devDependencies\"") catch unreachable, clap.parseParam("-D, --development") catch unreachable, clap.parseParam("--optional Add dependency to \"optionalDependencies\"") catch unreachable, @@ -6086,15 +6086,15 @@ pub const PackageManager = struct { clap.parseParam("<POS> ... \"name\" or \"name@version\" of package(s) to install") catch unreachable, }; - const remove_params = install_params_ ++ [_]ParamType{ + pub const remove_params = install_params_ ++ [_]ParamType{ clap.parseParam("<POS> ... \"name\" of package(s) to remove from package.json") catch unreachable, }; - const link_params = install_params_ ++ [_]ParamType{ + pub const link_params = install_params_ ++ [_]ParamType{ clap.parseParam("<POS> ... \"name\" install package as a link") catch unreachable, }; - const unlink_params = install_params_ ++ [_]ParamType{ + pub const unlink_params = install_params_ ++ [_]ParamType{ clap.parseParam("<POS> ... \"name\" uninstall package as a link") catch unreachable, }; |