UNPKG

lockfile-guardian

Version:

Never forget to install dependencies again! Automatically detect when your lock files change after git operations and warn you (or auto-install) when your dependencies are out of sync.

247 lines (174 loc) โ€ข 7.76 kB
# ๐Ÿ”’ Lockfile Guardian [![GitHub repo](https://img.shields.io/badge/github-repo-green)](https://github.com/statico/lockfile-guardian) [![npm](https://img.shields.io/npm/v/lockfile-guardian.svg)](https://www.npmjs.com/package/lockfile-guardian) [![Test](https://github.com/statico/lockfile-guardian/workflows/Test/badge.svg)](https://github.com/statico/lockfile-guardian/actions?query=workflow%3ATest) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/statico/lockfile-guardian/blob/main/LICENSE) Never forget to install dependencies again! Automatically detect when your lock files change after git operations and warn you (or auto-install) when your dependencies are out of sync. ## The Problem You know this scenario: - Teammate adds a new dependency - You `git pull` or switch branches - Your code breaks because you forgot to run `npm install` - You spend 10 minutes debugging before realizing the issue ## The Solution Lockfile Guardian installs git hooks that automatically detect when your lock files have changed and either warn you or auto-install dependencies. ## Features - ๐ŸŽฏ **Zero runtime dependencies** - Pure Node.js implementation - ๐Ÿ” **Smart detection** - Auto-detects npm, yarn, or pnpm lock files - โšก **One-command setup** - `npx lockfile-guardian install` and you're done - ๐Ÿช **Git hooks** - Works on checkout, pull, merge, and branch switching - ๐Ÿ”ง **Configurable** - Optional auto-install and silent modes - ๐Ÿ“ฆ **TypeScript** - Built with TypeScript, works with any Node.js project - ๐Ÿงน **Clean** - Stores metadata in `.git/` directory, not your working tree - โš ๏ธ **Visual warnings** - Eye-catching alerts when dependencies are out of sync - ๐Ÿถ **Husky compatible** - Works seamlessly with Husky, lint-staged, prettier, and other tools ## Quick Start ```bash # Install and setup git hooks (one time only) npx lockfile-guardian install # That's it! Now every git operation checks your dependencies ``` ## How It Works 1. **Secure tracking** - Stores SHA256 hash of your lock file in `.git/lockfile-guardian` 2. **Git integration** - Installs hooks for post-checkout, post-merge, and post-rewrite 3. **Smart detection** - Automatically finds and monitors the right lock file 4. **Helpful warnings** - Shows exactly which command to run for your package manager 5. **Optional automation** - Can automatically install dependencies if configured ## Husky Compatibility Lockfile Guardian is **fully compatible** with [Husky](https://typicode.github.io/husky/) and works seamlessly alongside other tools: ### โœ… What Works - **Husky v4+** - Automatically detects and uses `.husky/` directory - **lint-staged** - Runs after linting and formatting tools - **prettier** - Compatible with prettier hooks - **ESLint** - Works with ESLint pre-commit hooks - **Traditional hooks** - Falls back to `.git/hooks/` when Husky isn't detected ### ๐Ÿ”ง How It Works ```bash # Your existing .husky/pre-commit #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" npx lint-staged # After installing lockfile-guardian, your .husky/post-checkout becomes: #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" # Your existing hooks run first... # Lockfile Guardian npx lockfile-guardian check --hook ``` ### ๐Ÿš€ Setup with Husky If you're already using Husky, just install normally: ```bash npx lockfile-guardian install ``` Lockfile Guardian will: - โœ… Detect your Husky setup automatically - โœ… Install hooks in `.husky/` directory - โœ… Preserve all existing hook configurations - โœ… Run **after** your other tools (lint-staged, prettier, etc.) ### ๐Ÿ“‹ Example Integration ```json { "scripts": { "prepare": "husky" }, "lint-staged": { "*.{js,ts,tsx}": ["prettier --write", "eslint --fix"], "*.{json,md}": ["prettier --write"] }, "lockfileGuardian": { "autoInstall": true, "silent": false } } ``` ## Configuration Add optional configuration to your `package.json`: ```json { "lockfileGuardian": { "autoInstall": true, // Automatically run install commands "silent": false, // Suppress non-warning output "checkNodeModules": true // Warn if node_modules isn't gitignored (default: true) } } ``` ## Commands ```bash # Setup git hooks (one-time setup) npx lockfile-guardian install # Remove all hooks and cleanup npx lockfile-guardian uninstall # Manually check for lock file changes npx lockfile-guardian check # Show help and current configuration npx lockfile-guardian ``` ## Supported Package Managers Automatically detects and supports: - **pnpm** - `pnpm-lock.yaml` โ†’ `pnpm install` - **Yarn** - `yarn.lock` โ†’ `yarn install` - **npm** - `package-lock.json` โ†’ `npm install` Detection priority: pnpm โ†’ yarn โ†’ npm (first lock file found wins) ## Example Output ### Basic Warning ```bash $ git checkout feature/new-deps ===================================== โš ๏ธ DEPENDENCIES OUT OF DATE โš ๏ธ ===================================== Lock file pnpm-lock.yaml has changed! Run this command to update: pnpm install ===================================== ``` ### With Auto-Install ```bash $ git pull origin main ๐Ÿ”’ Lock file yarn.lock has changed! ๐Ÿ”’ Auto-installing dependencies with yarn... yarn install v1.22.19 [1/4] ๐Ÿ” Resolving packages... [2/4] ๐Ÿšš Fetching packages... [3/4] ๐Ÿ”— Linking dependencies... [4/4] ๐Ÿ”จ Building fresh packages... โœจ Done in 2.34s. ๐Ÿ”’ Dependencies updated successfully! ``` ## What Gets Created **Git hooks:** - `.git/hooks/post-checkout` - Runs after branch switching - `.git/hooks/post-merge` - Runs after `git pull`/`git merge` - `.git/hooks/post-rewrite` - Runs after `git rebase` **Metadata:** - `.git/lockfile-guardian` - Stores hash of current lock file **Nothing in your working directory!** All tool data stays in `.git/` where it belongs. ## Safety Features - **Non-destructive** - Only reads lock files, never modifies them - **Git validation** - Ensures you're in a git repository before installing - **Gitignore check** - Warns if `node_modules` isn't properly ignored - **Safe uninstall** - Completely removes all traces when uninstalled - **Error handling** - Graceful failure if install commands fail ## Why Use This? **For individuals:** - Never waste time debugging dependency issues again - Automatic detection works seamlessly in the background - Zero maintenance after initial setup **For teams:** - Ensures everyone has the same dependencies installed - Prevents "works on my machine" issues - Catches dependency updates immediately after pulling changes - Reduces onboarding friction for new team members ## Comparison with Alternatives | Tool | Setup | Auto-install | Zero deps | Git integrated | Husky compatible | | --------------------- | ------------------- | ------------------ | --------- | --------------- | ---------------- | | **Lockfile Guardian** | โœ… One command | โœ… Optional | โœ… Yes | โœ… Native hooks | โœ… Yes | | `@antfu/ni` | โŒ Manual usage | โŒ Manual | โŒ No | โŒ No | โŒ No | | Manual git hooks | โŒ Complex setup | โŒ Script required | โœ… Yes | โœ… Yes | โš ๏ธ Manual | | IDE extensions | โŒ Per-editor setup | โŒ Usually not | โŒ No | โŒ No | โŒ No | ## Requirements - Node.js 14+ - Git repository - One of: npm, yarn, or pnpm ## Contributing This tool is designed to be simple and focused. If you have ideas for improvements, please open an issue to discuss before implementing major changes. ## License MIT ## Caveats This tool was mostly written by Cursor and Claude 4 Sonnet.