Package Exports
- safe-ts-commit
- safe-ts-commit/dist/bin/cli.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (safe-ts-commit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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
/.tsx
files. - π― 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 --install
This will:
- β Install Husky (if not already installed)
- β
Add
prepare
script to yourpackage.json
- β
Create a
.husky/pre-commit
hook 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
prepare
script 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
.ts
and.tsx
files - Runs
tsc --noEmit
on 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!