Protect critical files by marking them @readonly. Prevent commits unless files are unlocked with @unlock.
npm install react-readonly-guard/ @readonly / and / @unlock /, developers get intentional, safe editing workflows β all enforced automatically through Git hooks.
@readonly unless @unlock is present.
@readonly (so you can add markers in a migration commit).
@unlock to make edits, and allows removing @unlock in the next commit.
.husky/pre-commit.
lib/ for zero-config usage.
bash
npm
npm install --save-dev react-readonly-guard
pnpm
pnpm add -D react-readonly-guard
yarn
yarn add -D react-readonly-guard
`
The package runs a postinstall-style installer that will ensure Husky is installed (if Husky isnβt installed) in your project and will inject a pre-commit hook that runs the readonly check.
PS: No manual setup required.
Behavior
The guard enforces these rules for existing files (files that exist in HEAD):
- If the file had @readonly in HEAD and the staged version still has @readonly but no @unlock, the commit is blocked.
- If the staged version contains @unlock, the commit is allowed.
- If the old version had @unlock and the staged does not, the commit is allowed (you can remove unlock after changes).
- New files (not present in HEAD) are allowed even if they contain @readonly.
Usage
After install, the hook is injected automatically. Then include any of the below in your component
LOCKING:
`bash
/**
*@readonly
*/
`
or
/* @readonly /
You can automatically add it using CLI with:
npx react-readonly-guard lock path/example-file.tsx
UNLOCKING:
`bash
/**
*@unlock
*/
`
or
/* @unlock /
You can automatically add it using CLI with:
npx react-readonly-guard unlock path/example-file.tsx
Commit as usual:
`bash
git add
git commit -m "Your message"
`
Result:
If you edit a readonly file without unlocking, the commit will fail:
`bash
β Cannot commit changes to readonly files (add @unlock to modify):
- src/components/Button.tsx
`
To bypass for a one-off commit (not recommended), use:
`bash
git commit -m "..." --no-verify
``