🔧 Unify lefthook and package.json scripts via LEFTHOOK_FILES env var
lefthook and package.json had parallel command definitions for the
same tools (oxlint, oxfmt, cspell). Consolidate by making lefthook
call the npm scripts, with staged files passed via the
LEFTHOOK_FILES env var. The scripts use ${LEFTHOOK_FILES:-<default>}
so they default to the full project when invoked manually and to
the staged-files list when invoked from lefthook.
Changes:
- package.json#check:oxlint: `oxlint ${LEFTHOOK_FILES:-src}`
(lints src/ manually; staged files from lefthook)
- package.json#check:oxfmt: `oxfmt --check ${LEFTHOOK_FILES:-src}`
- package.json#check:cspell: `cspell lint ${LEFTHOOK_FILES:-.}`
(walks CWD manually; staged files from lefthook)
- package.json#check:tsc, check📦 unchanged (no file args)
- lefthook.yml: file-filtered hooks (oxlint, oxfmt, cspell) now use
`sh -c 'LEFTHOOK_FILES="$0" npm run check:*' {staged_files}` to
inject the staged-files list into the env. sort-package-json and
typecheck call npm scripts directly (no file args).
- project-specs.md: document the unification pattern
Why sh -c + env var instead of the simpler 'npm run ... -- {staged_files}':
'oxlint src file.ts' lints the whole src/ tree *plus* file.ts
(oxlint doesn't dedupe paths). Setting LEFTHOOK_FILES as an env
var (which lefthook's 'env:' config does not template) requires
the sh -c wrapper, but it gives the right semantics: when the
var is set, only the explicit files are checked; when unset,
the default (src/ or .) is used.
Verified:
- 'npm run check:oxlint' (no env) lints all of src/
- 'LEFTHOOK_FILES=src/match.ts npm run check:oxlint' lints only that file
- 'npx lefthook run pre-commit' with a staged TS file: cspell output
shows '1/1 src/match.ts' (only staged file, not whole project)
- All 5 hooks pass on a real staged change
- 'lefthook validate' reports 'All good'
- 'npm run check' exits 0
This commit is contained in:
1 parent
45b6c141e8
commit
e45592ae00
3 files changed
+38
-21
No files matched your search
+5
-5
@@ -5,13 +5,13 @@ pre-commit:
|
||||
commands:
|
||||
oxlint:
|
||||
glob: "*.{ts,tsx,js,jsx,mjs,cjs}"
|
||||
run: npx oxlint {staged_files}
|
||||
run: sh -c 'LEFTHOOK_FILES="$0" npm run check:oxlint' {staged_files}
|
||||
oxfmt:
|
||||
glob: "*.{ts,tsx,js,jsx,mjs,cjs}"
|
||||
run: npx oxfmt --check {staged_files}
|
||||
run: sh -c 'LEFTHOOK_FILES="$0" npm run check:oxfmt' {staged_files}
|
||||
cspell:
|
||||
run: npx cspell {staged_files}
|
||||
run: sh -c 'LEFTHOOK_FILES="$0" npm run check:cspell' {staged_files}
|
||||
sort-package-json:
|
||||
run: npx sort-package-json --check
|
||||
run: npm run check:package
|
||||
typecheck:
|
||||
run: npx tsc --noEmit
|
||||
run: npm run check:tsc
|
||||
Reference in new issue
Block a user