safe-ts-commit
v1.0.1
Published
A Git hook utility that prevents committing TypeScript files with type errors
Maintainers
Readme
safe-ts-commit 🛡️
Stop bad commits before they happen. Automatically block Git commits if any TypeScript errors are detected in your staged files.
✨ What is safe-ts-commit?
safe-ts-commit is a production-ready CLI tool designed to protect your codebase by blocking Git commits if TypeScript type errors are present in your staged files.
Perfect for teams and solo devs who value code quality and type safety.
🚀 Why use this?
- ✅ Type-Safe Commits – Prevent buggy or broken commits due to TS errors.
- ⚡ Fast & Lightweight – Only checks staged
.ts/.tsxfiles. - 🎯 Zero Config – No extra setup required beyond install.
- 🔌 Husky-Ready – Integrates seamlessly as a Git pre-commit hook.
- 🎨 Readable Output – Colorful and actionable terminal messages.
🔧 Features
- 🧠 Detects only staged TypeScript files
- 🚫 Aborts commit if errors are found
- ✅ Allows commit to proceed if everything passes
- 🧼 Supports filenames with spaces/special characters
- 🖍️ Clean and colorful CLI messages
- 🧪 One-line setup: Auto-installs Husky and configures pre-commit hook.
📦 Prerequisites
- Node.js v12+
- TypeScript installed locally (
npm install typescript --save-dev) - Git (with initialized repo)
📥 Installation
npm install --save-dev safe-ts-commit⚡ One-Liner Setup (Auto-Install)
Don’t want to manually configure Husky? Run this:
npx safe-ts-commit --installThis will:
- ✅ Install Husky (if not already installed)
- ✅ Add
preparescript to yourpackage.json - ✅ Create a
.husky/pre-commithook that runssafe-ts-commit - ✅ Lock your commits behind type safety in seconds
Perfect for teams or quick setups. One command and you’re protected.
⚙️ Setup with Husky (Manual)
- Install Husky:
npm install husky --save-dev
npx husky install- Add a
preparescript in yourpackage.json:
"scripts": {
"prepare": "husky install"
}- Add a pre-commit hook:
npx husky add .husky/pre-commit "npx safe-ts-commit"🛠 How it Works
When a commit is attempted, safe-ts-commit:
- Collects staged files via
git diff --cached --name-only - Filters
.tsand.tsxfiles - Runs
tsc --noEmiton those files - If any type errors are found:
- ❌ The commit is blocked with a detailed error report
- If all files pass:
- ✅ The commit proceeds as normal
🧪 Manual Usage
You can run it manually without a Git hook:
npx safe-ts-commit🔄 Optional CLI Flags
safe-ts-commit [options]
--install Run auto-setup with Husky
--debug Print detailed debug logs
--silent Hide output unless there's an error
--tsconfig Path to custom tsconfig.json🧰 Troubleshooting
| Issue | Solution |
|-----------------------------|--------------------------------------------------------------------------|
| TypeScript not found | Ensure you installed TypeScript locally (npm install typescript) |
| No files being checked | Make sure your TypeScript files are staged (git add path/to/file.ts) |
| Hook not running | Ensure Husky is installed and the pre-commit hook is executable |
🗂 Project Structure
safe-ts-commit/
├── bin/
│ └── cli.js # CLI entry (compiled from src/cli.ts)
├── src/
│ ├── getStagedFiles.ts
│ ├── checkTsErrors.ts
│ └── utils.ts
├── .husky/ # Husky hooks
├── tsconfig.json
├── package.json
└── README.md📜 License
MIT — feel free to fork, improve, and contribute!
