@hexlabelposition/ward
v0.3.0
Published
Git hooks and staged-file linting in one tool. One config, zero hook folders.
Maintainers
Readme
ward
Git hooks and staged-file tasks from one config. No hook folder in your repo.
@hexlabelposition/ward runs your git hooks from a single config file and runs tasks on the files of a commit or a push. It replaces husky and lint-staged with one tool.
- One config for all hooks -
ward.config.ts(typed, with autocomplete),ward.yml, or a"ward"section inpackage.json - No
.husky/-style folder - hooks are generated locally into.git/hooks, only the config is committed - Tasks on staged files -
eslint --fix {staged_files}, withglob/excludeper task, fixes staged automatically - Safe with partial staging - unstaged changes are hidden while tasks run, and a failed run is reverted from a backup
- Careful with existing hooks - a hook that is already there (git-lfs, a hand-written script) is never overwritten silently
- Clear config errors - every problem is listed with its place in the config and a suggestion for typos
⚠️ Status: early versions.
0.xreleases may change the config and behavior. See GitHub Releases for what changed and BACKLOG.md for what is planned.
Install
Requires Git ≥ 2.20 and Node ≥ 24, Bun or Deno 2. CI tests the latest Bun and Deno 2.x.
npm install -D @hexlabelposition/ward
npm pkg set scripts.prepare=ward
npm run prepareThe prepare script runs on every npm install, so hooks are set up for everyone on the team automatically. With Bun, bun add -d @hexlabelposition/ward and the same prepare script work: bun install runs it too, also in workspace packages.
With Deno, there is no prepare lifecycle: add a task and run it after cloning and after updating Ward.
deno add npm:@hexlabelposition/ward{
"tasks": {
"prepare": "deno run -A npm:@hexlabelposition/ward"
}
}deno task prepareThen add a config. The shortest one lives in package.json:
{
"scripts": {
"prepare": "ward"
},
"ward": {
"hooks": {
"pre-commit": "npm run lint",
"commit-msg": "npx commitlint --edit {1}",
"pre-push": ["npm run typecheck", "npm test"]
}
}
}That's all: git commit now runs npm run lint and stops the commit if it fails.
Configuration
Ward looks for the config in the repo root, in this order:
ward.config.tsward.yml/ward.yaml"ward"section inpackage.json
If more than one is found, Ward stops with an error instead of silently picking one. Without a config every hook does nothing.
TypeScript
// ward.config.ts
import { defineConfig } from "@hexlabelposition/ward";
export default defineConfig({
hooks: {
"pre-commit": {
tasks: {
eslint: {
run: "npx eslint --fix {staged_files}",
glob: "*.{js,ts}",
},
prettier: {
run: "npx prettier --write --ignore-unknown {staged_files}",
},
},
},
"commit-msg": "npx commitlint --edit {1}",
"pre-push": ["npm run typecheck", "npm test"],
},
});defineConfig gives types and autocomplete for hook names and options. The file is loaded by Node's built-in type stripping, with no extra loader, so keep it to erasable syntax: no enum, namespace or constructor parameter properties.
YAML
# ward.yml
hooks:
pre-commit:
tasks:
eslint:
run: npx eslint --fix {staged_files}
glob: "*.{js,ts}"
prettier:
run: npx prettier --write --ignore-unknown {staged_files}
commit-msg: npx commitlint --edit {1}
pre-push:
- npm run typecheck
- npm testHook value forms
| Form | Meaning |
| ------------------------------------- | --------------------------------------------- |
| "cmd" | Run one command |
| ["a", "b"] | Run commands in order, stop on first failure |
| { tasks: { name: { run: "cmd" } } } | Named tasks, run in the order they are listed |
Any git hook can be used as a key: pre-commit, commit-msg, pre-push, post-checkout, post-merge and so on.
How tasks run
Commands run through the system shell from the repo root, so
&&, pipes andnpm run …work as in a terminal.node_modules/.binis not added toPATH: call local tools withnpxor throughnpm run.Tasks run one after another. The first one that fails stops the hook, and git gets its exit code:
$ git commit -m "feat: add login" … output of npm run lint … ward: pre-commit failed: npm run lint (exit 1)Output is not captured: you see what the command prints, as it prints it.
Hooks that get data from git on stdin (
pre-push) pass it on to the command.
Hook arguments
{1}, {2}, … are replaced with the arguments git passed to the hook. For commit-msg, {1} is the file with the commit message:
hooks:
commit-msg: npx commitlint --edit {1}
prepare-commit-msg: node scripts/prefill-message.js {1} {2}Values are quoted for the shell when needed, so write {1}, not "{1}". An argument git did not pass expands to nothing.
Tasks on files
File placeholders
| Placeholder | Files |
| ---------------- | -------------------------------------------------------------- |
| {staged_files} | Files staged for the commit (added, copied, modified, renamed) |
| {push_files} | Files changed by the commits being pushed, pre-push only |
| {all_files} | All tracked files |
Paths are relative to the repo root and quoted for the shell, so spaces and other special characters are safe. A task whose placeholders get no files is skipped, so eslint {staged_files} never runs on the whole project. A long list is split into several runs of the command, so it fits the command line limit on Windows.
glob and exclude
hooks:
pre-commit:
tasks:
eslint:
run: npx eslint --fix {staged_files}
glob: ["*.ts", "*.tsx"]
exclude: "src/generated/**"- A pattern without
/matches the file name in any directory (*.ts), one with/matches the path from the repo root (src/**/*.ts), as in.gitignore. globandexcludetake one pattern or a list.- A task with
globand no placeholder (npx tsc --noEmit) runs only when one of the hook's files matches: staged files inpre-commit, pushed files inpre-push.
Task options
| Option | Default | Meaning |
| ------------ | ------- | ---------------------------------------------------------------------------------------------------- |
| run | - | Command, or a function in ward.config.ts |
| glob | all | Files the task runs on |
| exclude | none | Files to leave out even when glob matches them |
| symlinks | false | Pass symlinks to the task too. Off by default: a formatter would rewrite the file the link points to |
| stageFixed | true | git add the staged files the task changed, so a formatter's fixes go into the commit |
Function tasks
In ward.config.ts, run can be a function. It runs in Ward's process and gets the task's files (after glob / exclude) and the hook arguments. The task fails when the function throws or its promise rejects.
import { readFileSync } from "node:fs";
import { defineConfig } from "@hexlabelposition/ward";
export default defineConfig({
hooks: {
"pre-commit": {
tasks: {
"no-only": {
glob: "*.test.ts",
run({ files }) {
const bad = files.filter((file) =>
readFileSync(file, "utf8").includes(".only("),
);
if (bad.length > 0) {
throw new Error(`.only() left in ${bad.join(", ")}`);
}
},
},
},
},
},
});files holds staged files in pre-commit, pushed files in pre-push and nothing in other hooks.
Parallel tasks
hooks:
pre-push:
parallel: true
tasks:
typecheck:
run: npm run typecheck
test:
run: npm testWith parallel: true all tasks start at once and each runs to the end; the hook fails if any of them fails. Every line of output is prefixed with the task name. Don't run tasks that write the same files in parallel, such as ESLint --fix and Prettier.
Safety
When a hook with tasks runs, Ward protects your working changes:
- Backup. Before the first task, the index and working tree are saved with
git stash create. While the hook runs, the backup is also ingit stash listas "ward backup", so a killed run can be restored by hand; it is kept there if the revert does not happen or fails. - Partial staging. When tasks get staged files, the unstaged changes of partially staged files are hidden, so tasks see exactly what will be committed. They are put back after the tasks.
- Revert. When a task fails, or the unstaged changes conflict with a task's fixes, the index and working tree go back to the backup.
- Empty commit. If the tasks undo every staged change in
pre-commit(a formatter reverts a whitespace-only change), the hook fails instead of making an empty commit.
Untracked files are not touched. Binary files, renames, deletions and symlinks are handled.
| Hook option | Flag | Meaning |
| ----------- | ------------- | ---------------------------------------------------------------------- |
| backup | --no-backup | false skips the backup to save time in large repos; no revert then |
| revert | --no-revert | false keeps the changes of the tasks after a failure, for inspection |
The flags are for ward run, right after the hook name: ward run pre-commit --no-revert.
Config errors
The whole config is checked on every run, and every problem is reported at once:
$ git commit -m "fix: typo"
ward: ward.yml is invalid:
hooks.pre-comit: unknown hook. Did you mean "pre-commit"?
hooks.pre-push[1]: command is emptyAn invalid config fails every hook, not only the broken one: a typo in a hook name would otherwise never be noticed. Options planned for later versions are rejected with "not supported yet" instead of being silently ignored.
CLI
| Command | What it does |
| ----------------- | ----------------------------------------------------------- |
| ward | Install hook stubs into .git/hooks (what prepare runs) |
| ward run <hook> | Run a hook manually - useful for testing without committing |
| ward uninstall | Remove Ward's hook stubs, put chained hooks back |
ward flags:
| Flag | Meaning |
| ---------------------- | -------------------------------------------------------------- |
| --chain | Keep existing hooks and run Ward after them |
| --force | Replace existing hooks; the old ones are lost |
| --respect-hooks-path | Install into the core.hooksPath directory instead of failing |
| --bin <path> | Binary the hooks call instead of node_modules/.bin/ward |
ward run flags: --no-backup, --no-revert (see Safety).
ward does nothing in CI (when CI is set), outside a git repository and when git is not installed, so npm install in Docker or CI does not fail.
Existing hooks
If a hook file that Ward did not write is already in .git/hooks, Ward leaves it alone:
- In a terminal,
npx wardlists such hooks and asks whether to keep them. - From
prepare, it prints a warning with the command to run and does not fail the install. npx ward --chainkeeps them without asking: the old hook is renamed to<hook>.pre-wardand runs before Ward with the same arguments. If it fails, git stops and Ward does not run.npx ward --forcereplaces them.
ward uninstall removes only Ward's own stubs and moves each <hook>.pre-ward back to its place.
core.hooksPath
When core.hooksPath is set, git ignores .git/hooks, so Ward's hooks would never run. Ward stops with a message that names the value, where it is set and the command that removes it. To install into that directory instead, run ward --respect-hooks-path.
Skipping
git commit --no-verify # git's built-in skip: no hooks at all
WARD=0 git commit # disable Ward for one commandWARD=0 skips only Ward: a hook that was there before Ward and is chained from the stub still runs.
Hooks are skipped automatically while a merge or rebase is in progress: the commits git makes there are not your working changes.
If dependencies are not installed yet (node_modules/.bin/ward is missing), the hook prints a warning and lets git continue.
Moving from husky
Remove husky and install Ward:
npm uninstall husky npm install -D @hexlabelposition/ward npm pkg set scripts.prepare=wardMove the commands from
.husky/*into the config. A.husky/pre-commitwithnpm testand a.husky/commit-msgwithnpx commitlint --edit $1become:# ward.yml hooks: pre-commit: npm test commit-msg: npx commitlint --edit {1}Delete the
.husky/folder and unset the hooks path husky configured, then install the hooks:git config --unset core.hooksPath npm run prepareEach teammate needs the
git config --unsetonce in their clone; Ward prints this command when it finds the setting.
Moving from lint-staged
Each lint-staged entry becomes a task with glob and {staged_files}. This lint-staged config:
{
"lint-staged": {
"*.{js,ts}": "eslint --fix",
"*": "prettier --write --ignore-unknown"
}
}becomes:
# ward.yml
hooks:
pre-commit:
tasks:
eslint:
run: npx eslint --fix {staged_files}
glob: "*.{js,ts}"
prettier:
run: npx prettier --write --ignore-unknown {staged_files}Then remove lint-staged and the npx lint-staged call from the pre-commit hook. Differences to keep in mind:
- Ward does not append the files to the command: write
{staged_files}where they go. - Tasks run one after another unless the hook sets
parallel: true; lint-staged runs different globs in parallel. - Fixed files are staged by default, as in lint-staged;
stageFixed: falseturns it off per task.
How it works
ward writes a small stub for every git hook into .git/hooks. Each stub only calls ward run <hook>. The config is read at run time, so changes apply on the next git command, with no reinstall. A hook with no tasks exits right away, before anything else is loaded.
Stubs start Ward on the runtime that ran the install. bun install starts prepare through the binary's node shebang, so hooks run on Node when it is installed and on Bun when it is not. Installed from Deno, hooks run deno run -A npm:@hexlabelposition/ward, at the version from deno.json or package.json. WARD_RUNTIME=node, bun or deno overrides it, for example WARD_RUNTIME=node git commit.
.git/hooks is not committed, so each clone generates its own stubs through prepare. The hooks directory is resolved by git itself, so worktrees and submodules work.
See ARCHITECTURE.md for the design and the details of each part.
Roadmap
Planned versions and items are in BACKLOG.md, released changes on GitHub Releases.
