From 9308e1bf09637a75d4c99ddd056addd7fbb00f6f Mon Sep 17 00:00:00 2001 From: Ashcon Partovi Date: Tue, 3 Oct 2023 17:39:45 -0700 Subject: Support `bun run --if-present` (#6248) * Support --if-present Closes #5670 * More robust tests, handle more cases --- src/cli.zig | 7 +++++++ src/cli/run_command.zig | 4 ++++ 2 files changed, 11 insertions(+) (limited to 'src') diff --git a/src/cli.zig b/src/cli.zig index 9982e5e7f..a05fb3768 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -196,6 +196,7 @@ pub const Arguments = struct { clap.parseParam("--inspect ? Activate Bun's Debugger") catch unreachable, clap.parseParam("--inspect-wait ? Activate Bun's Debugger, wait for a connection before executing") catch unreachable, clap.parseParam("--inspect-brk ? 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("... ") catch unreachable, }; @@ -538,6 +539,7 @@ pub const Arguments = struct { ctx.preloads = preloads; } + ctx.runtime_options.if_present = args.flag("--if-present"); ctx.runtime_options.smol = args.flag("--smol"); if (args.option("--inspect")) |inspect_flag| { ctx.runtime_options.debugger = if (inspect_flag.len == 0) @@ -1022,6 +1024,7 @@ pub const Command = struct { pub const RuntimeOptions = struct { smol: bool = false, debugger: Debugger = .{ .unspecified = {} }, + if_present: bool = false, }; pub const Context = struct { @@ -1666,6 +1669,10 @@ pub const Command = struct { Global.exit(1); } + if (ctx.runtime_options.if_present) { + return; + } + if (was_js_like) { Output.prettyErrorln("error: module not found \"{s}\"", .{ ctx.positionals[0], diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index 1ef1342c0..19046e292 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -1160,6 +1160,10 @@ pub const RunCommand = struct { } } + if (ctx.runtime_options.if_present) { + return true; + } + if (comptime log_errors) { Output.prettyError("error: missing script \"{s}\"\n", .{script_name_to_search}); Global.exit(1); -- cgit v1.2.3