$ show antfu-collective/ni
ni is a CLI tool that abstracts away the differences between Node.js package managers (npm, yarn, pnpm, bun, deno, nub, aube). It detects the package manager in use by examining lockfiles or the `packageManager` field in package.json, then translates a universal command (e.g., `ni`, `nr`, `nlx`) into the corresponding native command. The architecture is modular: detection (`src/detect.ts`), command parsing (`src/parse.ts`), and execution (`src/runner.ts`) are cleanly separated. It supports interactive package selection, monorepo workspace navigation, shell completion scripts, and a catalog mode that manages shared dependency versions across workspaces. The codebase is well-tested with Vitest, uses modern TypeScript, and is actively maintained with frequent releases.
The LLM identified 5 files worth highlighting for reuse:
Determines the version range prefix (^, ~, exact) that a package manager would use when saving a resolved version. It reads npm/pnpm `.npmrc` files and yarn `.yarnrc.yml` files, respecting `save-exact`, `save-prefix`, and `defaultSemverRangePrefix` settings. This is a self-contained utility that solves a common cross-tool problem and could be extracted into a standalone library.
Provides pure functions for parsing package specifiers (e.g., `@scope/name@version`) and determining whether a version specifier should be kept verbatim (ranges, pins, protocols) or resolved against a registry. The logic correctly handles scoped packages, aliases, and edge cases like trailing `@`. This is a reusable building block for any tool that manipulates package dependency strings.
Implements `findClosestPackageJson` to walk up the directory tree for the nearest `package.json`, and `getPackageJSON` which parses JSON with support for comments and trailing commas (JSONC) as used by Bun. Falls back gracefully to plain JSON. The `withPackageRoot` helper adjusts the working directory to the package root. These functions are self-contained and solve a common need in Node.js tooling.
Loads configuration from a `.nirc` file (INI format) and environment variables, with sensible defaults. It validates keys, suggests corrections for misspelled options, and warns about unrecognized settings. The layered override (defaults < file < environment) is a clean pattern that could be reused in other CLI tools. The `getConfig` function is async and caches the result.
Contains `writeFileSafe`, which writes a file atomically by first writing to a temporary file in the same directory and then renaming. This avoids partial writes and race conditions. Also includes `cmdExists` (using the `which` library), `terminalLink` (OSC 8 hyperlinks), and `formatPackageWithUrl`. These are generic utilities that could be extracted into a shared library.