From 3da9c728d6756de9e3280ac8f8fce8a7515d641f Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 22 Dec 2022 23:38:56 -0800 Subject: Make `bun pm ls` only show top-level by default --- src/cli/package_manager_command.zig | 31 +++++++++++++++++++++++++++++-- src/string_immutable.zig | 9 +++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/cli/package_manager_command.zig b/src/cli/package_manager_command.zig index 5de5d3c18..8fbf21af2 100644 --- a/src/cli/package_manager_command.zig +++ b/src/cli/package_manager_command.zig @@ -199,8 +199,34 @@ pub const PackageManagerCommand = struct { // TODO: find max depth beforehand var more_packages = [_]bool{false} ** 16; if (first_directory.packages.len > 1) more_packages[0] = true; - printNodeModulesFolderStructure(&first_directory, null, 0, &directories, lockfile, more_packages); - Output.enableBuffering(); + const recurse = strings.leftHasAnyInRight(args, &.{ "-A", "-a", "--all" }); + + if (recurse) { + printNodeModulesFolderStructure(&first_directory, null, 0, &directories, lockfile, more_packages); + Output.enableBuffering(); + } else { + var cwd_buf: [bun.MAX_PATH_BYTES]u8 = undefined; + const path = std.os.getcwd(&cwd_buf) catch { + Output.prettyErrorln("error: Could not get current working directory", .{}); + Global.exit(1); + }; + const package_ids = lockfile.packages.items(.resolutions)[0].get(lockfile.buffers.resolutions.items); + + Output.println("{s} node_modules ({d})", .{ path, package_ids.len }); + Output.enableBuffering(); + const string_bytes = lockfile.buffers.string_bytes.items; + + for (package_ids) |package_id, i| { + if (package_id >= lockfile.packages.len) continue; + + if (i == package_ids.len - 1) { + Output.prettyln("└── {s}@{any}\n", .{ names[package_id].slice(string_bytes), lockfile.packages.items(.resolution)[package_id].fmt(string_bytes) }); + } else { + Output.prettyln("├── {s}@{any}\n", .{ names[package_id].slice(string_bytes), lockfile.packages.items(.resolution)[package_id].fmt(string_bytes) }); + } + } + } + Global.exit(0); } @@ -210,6 +236,7 @@ pub const PackageManagerCommand = struct { \\ bun pm bin print the path to bin folder \\ bun pm -g bin print the global path to bin folder \\ bun pm ls list the dependency tree according to the current lockfile + \\ bun pm ls --all list the entire dependency tree according to the current lockfile \\ bun pm hash generate & print the hash of the current lockfile \\ bun pm hash-string print the string used to hash the lockfile \\ bun pm hash-print print the hash stored in the current lockfile diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 76b8b7073..f3a7cdf5e 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -3983,3 +3983,12 @@ pub fn cloneNormalizingSeparators( return buf[0 .. @ptrToInt(remain.ptr) - @ptrToInt(buf.ptr)]; } + +pub fn leftHasAnyInRight(to_check: []const string, against: []const string) bool { + for (to_check) |check| { + for (against) |item| { + if (eqlLong(check, item, true)) return true; + } + } + return false; +} -- cgit v1.2.3