From 0b80ca57d2865bb0a376ed431580018cd6d7e2f5 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Mon, 31 Jul 2023 06:51:09 -0700 Subject: Implement `bun start` --- src/cli/run_command.zig | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index 6a559b342..7fc83d5fc 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -893,7 +893,7 @@ pub const RunCommand = struct { } if (log_errors or force_using_bun) { - if (script_name_to_search.len > 0) { + if (script_name_to_search.len > 0 and !strings.eqlComptime(script_name_to_search, "start")) { possibly_open_with_bun_js: { const ext = std.fs.path.extension(script_name_to_search); var has_loader = false; @@ -998,6 +998,7 @@ pub const RunCommand = struct { var this_bundler: bundler.Bundler = undefined; var root_dir_info = try configureEnvForRun(ctx, &this_bundler, null, &ORIGINAL_PATH, log_errors, force_using_bun); this_bundler.env.map.put("npm_lifecycle_event", script_name_to_search) catch unreachable; + if (root_dir_info.enclosing_package_json) |package_json| { if (package_json.scripts) |scripts| { switch (script_name_to_search.len) { @@ -1132,6 +1133,41 @@ pub const RunCommand = struct { } } + if (strings.eqlComptime(script_name_to_search, "start")) { + var path_to_use: string = "."; + var relative_start_dir = root_dir_info; + if (root_dir_info.enclosing_package_json != root_dir_info.package_json or (root_dir_info.package_json != null and root_dir_info.abs_path.len < root_dir_info.package_json.?.source.path.name.dir.len)) { + if (root_dir_info.enclosing_package_json) |package_json| { + if (this_bundler.resolver.readDirInfo(package_json.source.path.name.dirWithTrailingSlash()) catch null) |read_dir| { + relative_start_dir = read_dir; + } + } + } + + if (relative_start_dir.getEntriesConst()) |entries| { + if (entries.data.contains("server.ts")) { + path_to_use = try bun.default_allocator.dupe(u8, bun.path.joinAbs(relative_start_dir.abs_path, .auto, "./server.ts")); + } else if (entries.data.contains("server.js")) { + path_to_use = try bun.default_allocator.dupe(u8, bun.path.joinAbs(relative_start_dir.abs_path, .auto, "./server.js")); + } + } + + Run.boot(ctx, path_to_use) catch |err| { + if (Output.enable_ansi_colors) { + ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {}; + } else { + ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {}; + } + + Output.prettyErrorln("error: Failed to run {s} due to error {s}", .{ + ".", + @errorName(err), + }); + Global.exit(1); + }; + return true; + } + if (comptime log_errors) { Output.prettyError("error: missing script \"{s}\"\n", .{script_name_to_search}); Global.exit(0); -- cgit v1.2.3