diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bunfig.zig | 16 | ||||
-rw-r--r-- | src/cli.zig | 39 | ||||
-rw-r--r-- | src/install/install.zig | 24 |
3 files changed, 50 insertions, 29 deletions
diff --git a/src/bunfig.zig b/src/bunfig.zig index 7eab2d002..f11e0377f 100644 --- a/src/bunfig.zig +++ b/src/bunfig.zig @@ -196,10 +196,10 @@ pub const Bunfig = struct { .capacity = count, }; - names.appendSliceAssumeCapacity(registry_map.names); + names.appendSliceAssumeCapacity(registry_map.scopes); for (scopes.data.e_object.properties.slice()) |prop| { - const name_ = prop.key.?.data.e_string.string(this.allocator) orelse continue; + const name_ = prop.key.?.asString(this.allocator) orelse continue; const value = prop.value orelse continue; if (name_.len == 0) continue; const name = if (name_[0] == '@') name_[1..] else name_; @@ -220,7 +220,7 @@ pub const Bunfig = struct { } registry_map.registries = registries.items; - registry_map.names = names.items; + registry_map.scopes = names.items; install.scoped = registry_map; } @@ -251,19 +251,19 @@ pub const Bunfig = struct { } if (lockfile_expr.get("save")) |lockfile| { - if (lockfile.asString()) |value| { + if (lockfile.asBool()) |value| { install.save_lockfile = value; } } if (lockfile_expr.get("path")) |lockfile| { - if (lockfile.asString()) |value| { + if (lockfile.asString(allocator)) |value| { install.lockfile_path = value; } } if (lockfile_expr.get("savePath")) |lockfile| { - if (lockfile.asString()) |value| { + if (lockfile.asString(allocator)) |value| { install.save_lockfile_path = value; } } @@ -288,13 +288,13 @@ pub const Bunfig = struct { } if (bun.get("globalDir")) |dir| { - if (dir.asString()) |value| { + if (dir.asString(allocator)) |value| { install.global_dir = value; } } if (bun.get("globalBinDir")) |dir| { - if (dir.asString()) |value| { + if (dir.asString(allocator)) |value| { install.global_bin_dir = value; } } diff --git a/src/cli.zig b/src/cli.zig index df65de4be..4a98775d4 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -240,34 +240,29 @@ pub const Arguments = struct { try Bunfig.parse(allocator, logger.Source.initPathString(std.mem.span(config_path), contents), ctx, cmd); } - fn getHomeConfigPath(cwd: string, buf: *[std.fs.MAX_PATH_BYTES]u8) ?[:0]const u8 { - if (std.os.getenvZ("XDG_CONFIG_HOME")) |data_dir| { - var paths = [_]string{ data_dir, ".bunfig.toml" }; - return resolve_path.joinAbsStringBufZ(cwd, buf, &paths, .auto); - } - - if (std.os.getenvZ("HOME")) |data_dir| { - var paths = [_]string{ data_dir, ".bunfig.toml" }; - return resolve_path.joinAbsStringBufZ(cwd, buf, &paths, .auto); + fn getHomeConfigPath(buf: *[std.fs.MAX_PATH_BYTES]u8) ?[:0]const u8 { + if (std.os.getenvZ("XDG_CONFIG_HOME") orelse std.os.getenvZ("HOME")) |data_dir| { + var paths = [_]string{".bunfig.toml"}; + var outbuf = resolve_path.joinAbsStringBuf(data_dir, buf, &paths, .auto); + buf[outbuf.len] = 0; + return std.meta.assumeSentinel(outbuf, 0); } return null; } - pub fn loadConfig(allocator: std.mem.Allocator, args: clap.Args(clap.Help, ¶ms), ctx: *Command.Context, comptime cmd: Command.Tag) !void { + pub fn loadConfig(allocator: std.mem.Allocator, user_config_path_: ?string, ctx: *Command.Context, comptime cmd: Command.Tag) !void { var config_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; if (comptime cmd.readGlobalConfig()) { - if (getHomeConfigPath(ctx.args.absolute_working_dir, &config_buf)) |path| { + if (getHomeConfigPath(&config_buf)) |path| { try loadConfigPath(allocator, true, path, ctx, comptime cmd); } } - var config_path_: []const u8 = ""; - if (args.option("--config")) |config_path__| { - config_path_ = config_path__; - } + var config_path_: []const u8 = user_config_path_ orelse ""; + var auto_loaded: bool = false; - if (config_path_.len == 0 and (args.option("--config") != null or Command.Tag.always_loads_config.get(cmd))) { + if (config_path_.len == 0 and (user_config_path_ != null or Command.Tag.always_loads_config.get(cmd))) { config_path_ = "bunfig.toml"; auto_loaded = true; } @@ -282,6 +277,11 @@ pub const Arguments = struct { config_buf[config_path_.len] = 0; config_path = config_buf[0..config_path_.len :0]; } else { + if (ctx.args.absolute_working_dir == null) { + var secondbuf: [std.fs.MAX_PATH_BYTES]u8 = undefined; + var cwd = std.os.getcwd(&secondbuf) catch return; + ctx.args.absolute_working_dir = try allocator.dupe(u8, cwd); + } var parts = [_]string{ ctx.args.absolute_working_dir.?, config_path_ }; config_path_ = resolve_path.joinAbsStringBuf( ctx.args.absolute_working_dir.?, @@ -296,6 +296,10 @@ pub const Arguments = struct { try loadConfigPath(allocator, auto_loaded, config_path, ctx, comptime cmd); } + pub fn loadConfigWithCmdArgs(allocator: std.mem.Allocator, args: clap.Args(clap.Help, ¶ms), ctx: *Command.Context, comptime cmd: Command.Tag) !void { + return try loadConfig(allocator, args.option("--config"), ctx, comptime cmd); + } + pub fn parse(allocator: std.mem.Allocator, ctx: *Command.Context, comptime cmd: Command.Tag) !Api.TransformOptions { var diag = clap.Diagnostic{}; @@ -323,7 +327,7 @@ pub const Arguments = struct { ctx.args.absolute_working_dir = cwd; if (comptime Command.Tag.loads_config.get(cmd)) { - try loadConfig(allocator, args, ctx, cmd); + try loadConfigWithCmdArgs(allocator, args, ctx, cmd); } var opts: Api.TransformOptions = ctx.args; @@ -1198,6 +1202,7 @@ pub const Command = struct { .InstallCommand = true, .AddCommand = true, .RemoveCommand = true, + .PackageManagerCommand = true, }); pub const uses_global_options: std.EnumArray(Tag, bool) = std.EnumArray(Tag, bool).initDefault(true, .{ diff --git a/src/install/install.zig b/src/install/install.zig index 963fb84ee..312c4808e 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -24,6 +24,7 @@ const Api = @import("../api/schema.zig").Api; const Path = @import("../resolver/resolve_path.zig"); const configureTransformOptionsForBun = @import("../javascript/jsc/config.zig").configureTransformOptionsForBun; const Command = @import("../cli.zig").Command; +const BunArguments = @import("../cli.zig").Arguments; const bundler = @import("../bundler.zig"); const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; const DotEnv = @import("../env_loader.zig"); @@ -4769,9 +4770,11 @@ pub const PackageManager = struct { switch (response.status_code) { 404 => { if (comptime log_level != .silent) { - const fmt = "\n<r><red>error<r>: package <b>\"{s}\"<r> not found <d>404<r>\n"; + const fmt = "\n<r><red>error<r>: package <b>\"{s}\"<r> not found <d>{s}{s} 404<r>\n"; const args = .{ name.slice(), + task.http.url.displayHostname(), + task.http.url.pathname, }; if (comptime log_level.showProgress()) { @@ -4784,9 +4787,11 @@ pub const PackageManager = struct { }, 401 => { if (comptime log_level != .silent) { - const fmt = "\n<r><red>error<r>: unauthorized while loading <b>\"{s}\"<r><d> 401<r>\n"; + const fmt = "\n<r><red>error<r>: unauthorized <b>\"{s}\"<r> <d>{s}{s} 401<r>\n"; const args = .{ name.slice(), + task.http.url.displayHostname(), + task.http.url.pathname, }; if (comptime log_level.showProgress()) { @@ -5750,8 +5755,10 @@ pub const PackageManager = struct { package_json_file_: ?std.fs.File, comptime params: []const ParamType, ) !*PackageManager { - var cli = try CommandLineArguments.parse(ctx.allocator, params); - return try initWithCLI(ctx, package_json_file_, cli); + var _ctx = ctx; + var cli = try CommandLineArguments.parse(ctx.allocator, params, &_ctx); + + return try initWithCLI(_ctx, package_json_file_, cli); } fn initWithCLI( @@ -5917,6 +5924,7 @@ pub const PackageManager = struct { const ParamType = clap.Param(clap.Help); pub const install_params_ = [_]ParamType{ + clap.parseParam("-c, --config <STR>? Load config (bunfig.toml)") catch unreachable, clap.parseParam("-y, --yarn Write a yarn.lock file (yarn v1)") catch unreachable, clap.parseParam("-p, --production Don't install devDependencies") catch unreachable, clap.parseParam("--no-save Don't save a lockfile") catch unreachable, @@ -5958,6 +5966,7 @@ pub const PackageManager = struct { lockfile: string = "", token: string = "", global: bool = false, + config: ?string = null, backend: ?PackageInstall.Method = null, @@ -5998,6 +6007,7 @@ pub const PackageManager = struct { pub fn parse( allocator: std.mem.Allocator, comptime params: []const ParamType, + ctx: *Command.Context, ) !CommandLineArguments { var diag = clap.Diagnostic{}; @@ -6032,6 +6042,12 @@ pub const PackageManager = struct { cli.silent = args.flag("--silent"); cli.verbose = args.flag("--verbose"); + if (args.option("--config")) |opt| { + cli.config = opt; + } + + try BunArguments.loadConfig(allocator, cli.config, ctx, .InstallCommand); + cli.link_native_bins = args.options("--link-native-bins"); if (comptime params.len == add_params.len) { |