Skip to content

Commit

Permalink
Support --if-present
Browse files Browse the repository at this point in the history
Closes #5670
  • Loading branch information
Electroid committed Oct 3, 2023
1 parent a6af1c8 commit 627d4ef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub const Arguments = struct {
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,
};

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/cli/run_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,11 @@ pub const RunCommand = struct {
}
}

// If the script is not present and --if-present is passed, exit with 0
if (ctx.runtime_options.if_present) {
Global.exit(0);
}

if (comptime log_errors) {
Output.prettyError("<r><red>error<r><d>:<r> missing script \"<b>{s}<r>\"\n", .{script_name_to_search});
Global.exit(1);
Expand Down
26 changes: 26 additions & 0 deletions test/cli/run/if-present.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { it, expect } from "bun:test";
import { bunEnv, bunExe } from "harness";

it("should not error using `bun --if-present`", () => {
const { stdout, stderr, exitCode } = Bun.spawnSync({
cmd: [bunExe(), "--if-present", "doesnotexist"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toBeEmpty();
expect(stderr.toString()).toBeEmpty();
expect(exitCode).toBe(0);
});

it("should not error using `bun --if-present`", () => {
const { stdout, stderr, exitCode } = Bun.spawnSync({
cmd: [bunExe(), "run", "--if-present", "doesnotexist"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toBeEmpty();
expect(stderr.toString()).toBeEmpty();
expect(exitCode).toBe(0);
});

0 comments on commit 627d4ef

Please sign in to comment.