Skip to content

Commit 6f22387

Browse files
committed
chore: add stagedLint validations
1 parent bf050c4 commit 6f22387

File tree

5 files changed

+47
-71
lines changed

5 files changed

+47
-71
lines changed

bin/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ cli
4141
log.success('Successfully set all git hooks')
4242
}
4343
catch (err) {
44-
log.error('Was not able to set git hooks. Error:', err)
44+
log.error(err)
4545
process.exit(1)
4646
}
4747
})

docs/config.md

+7-33
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ You can configure your git hooks using any of these file formats:
3232
import type { GitHooksConfig } from 'bun-git-hooks'
3333

3434
const config: GitHooksConfig = {
35-
// Git hook commands
35+
// Note: staged-lint is only available in pre-commit hook
3636
'pre-commit': {
3737
'staged-lint': {
3838
'*.{js,ts}': 'bunx --bun eslint . --fix',
@@ -62,9 +62,7 @@ export default config
6262
}
6363
},
6464
"commit-msg": "bun commitlint --edit $1",
65-
"pre-push": "bun run build",
66-
"verbose": true,
67-
"preserveUnused": false
65+
"pre-push": "bun run build"
6866
}
6967
}
7068
```
@@ -73,32 +71,26 @@ export default config
7371

7472
### Hook Commands
7573

76-
Any valid git hook can be configured with either a command string or a staged-lint configuration:
74+
Any valid git hook can be configured with either a command string or a staged-lint configuration (pre-commit only):
7775

7876
```ts
7977
const config: GitHooksConfig = {
8078
// Simple command
8179
'pre-commit': 'bun run lint',
8280

83-
// Staged lint configuration
81+
// Staged lint configuration (pre-commit only)
8482
'pre-commit': {
8583
'staged-lint': {
8684
'*.{js,ts}': 'bunx --bun eslint . --fix',
8785
'*.{css,scss}': 'stylelint --fix'
8886
}
89-
},
90-
91-
// Global staged lint configuration
92-
'staged-lint': {
93-
'*.{js,ts}': 'bunx --bun eslint . --fix',
94-
'*.{css,scss}': 'stylelint --fix'
9587
}
9688
}
9789
```
9890

9991
### Staged Lint Configuration
10092

101-
The `staged-lint` feature allows you to run specific commands on staged files matching certain patterns:
93+
The `staged-lint` feature is only available in the pre-commit hook. It allows you to run specific commands on staged files matching certain patterns:
10294

10395
```ts
10496
const config: GitHooksConfig = {
@@ -123,25 +115,6 @@ const config: GitHooksConfig = {
123115
}
124116
```
125117

126-
You can also use `staged-lint` as a top-level configuration to apply the same rules to all hooks:
127-
128-
```ts
129-
const config: GitHooksConfig = {
130-
// Global staged lint configuration
131-
'staged-lint': {
132-
'*.{js,ts}': 'bunx --bun eslint . --fix',
133-
'*.{css,scss}': 'stylelint --fix'
134-
},
135-
136-
// Hook-specific staged lint configuration (takes precedence)
137-
'pre-commit': {
138-
'staged-lint': {
139-
'*.{js,ts}': 'bunx --bun eslint . --fix --max-warnings=0'
140-
}
141-
}
142-
}
143-
```
144-
145118
### Global Options
146119

147120
| Option | Type | Default | Description |
@@ -219,4 +192,5 @@ If no configuration is found, an error will be thrown.
219192
4. **Preserve Critical Hooks**: Use `preserveUnused` for important custom hooks
220193
5. **Enable Verbose Mode**: When debugging hook issues
221194
6. **Use Exit Codes**: Hooks should exit with non-zero for failures
222-
7. **Use Staged Lint**: For efficient file-specific linting
195+
7. **Use Staged Lint**: For efficient file-specific linting (pre-commit only)
196+
8. **Follow Restrictions**: Remember that staged-lint is only available in pre-commit hook

docs/index.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ cat > git-hooks.config.ts << EOL
5555
import type { GitHooksConfig } from 'bun-git-hooks'
5656
5757
const config: GitHooksConfig = {
58-
'pre-commit': 'bun run lint && bun run test',
58+
// Note: staged-lint is only available in pre-commit hook
59+
'pre-commit': {
60+
'staged-lint': {
61+
'*.{js,ts}': 'bunx --bun eslint . --fix --max-warnings=0'
62+
}
63+
},
5964
'commit-msg': 'bun commitlint --edit $1',
6065
'pre-push': 'bun run build',
6166
}
@@ -74,6 +79,7 @@ EOL
7479
- 📦 **Zero Dependencies**: Minimal footprint in your project
7580
-**Fast**: Built for Bun with performance in mind
7681
- 🔧 **Flexible**: Multiple configuration formats and options
82+
- 🚫 **Restricted Features**: Clear boundaries for features like staged-lint (pre-commit only)
7783

7884
## Configuration Options
7985

@@ -95,8 +101,13 @@ EOL
95101

96102
```ts
97103
const config: GitHooksConfig = {
98-
// Lint and test before commits
99-
'pre-commit': 'bun run lint && bun run test',
104+
// Lint and test before commits (using staged-lint)
105+
'pre-commit': {
106+
'staged-lint': {
107+
'*.{js,ts}': 'bunx --bun eslint . --fix --max-warnings=0',
108+
'*.{css,scss}': 'stylelint --fix'
109+
}
110+
},
100111

101112
// Validate commit messages
102113
'commit-msg': 'bun commitlint --edit $1',
@@ -108,10 +119,7 @@ const config: GitHooksConfig = {
108119
'post-checkout': 'bun install',
109120

110121
// Run security checks before push
111-
'pre-push': 'bun run security:check',
112-
113-
// Format code before commit
114-
'pre-commit': 'bun run format && bun run lint',
122+
'pre-push': 'bun run security:check'
115123
}
116124
```
117125

docs/usage.md

+7-28
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Create a `git-hooks.config.{ts,js,mjs,cjs,json}` file in your project root:
1717
import type { GitHooksConfig } from 'bun-git-hooks'
1818

1919
const config: GitHooksConfig = {
20-
// Using staged-lint for efficient file-specific linting
20+
// Note: staged-lint is only available in pre-commit hook
2121
'pre-commit': {
2222
'staged-lint': {
2323
'*.{js,ts}': 'bunx --bun eslint . --fix',
@@ -67,13 +67,13 @@ git-hooks remove # alias
6767
# Enable verbose logging
6868
git-hooks --verbose
6969

70-
# Run staged lint for a specific hook
70+
# Run staged lint for pre-commit hook
7171
git-hooks run-staged-lint pre-commit
7272
```
7373

7474
## Staged Lint Usage
7575

76-
The `staged-lint` feature allows you to run specific commands on staged files matching certain patterns. This is more efficient than running commands on all files.
76+
The `staged-lint` feature is only available in the pre-commit hook. It allows you to run specific commands on staged files matching certain patterns. This is more efficient than running commands on all files.
7777

7878
### Basic Staged Lint
7979

@@ -109,27 +109,6 @@ const config: GitHooksConfig = {
109109
}
110110
```
111111

112-
### Global Staged Lint
113-
114-
You can define staged lint rules globally that apply to all hooks:
115-
116-
```ts
117-
const config: GitHooksConfig = {
118-
// Global staged lint configuration
119-
'staged-lint': {
120-
'*.{js,ts}': 'bunx --bun eslint . --fix',
121-
'*.{css,scss}': 'stylelint --fix'
122-
},
123-
124-
// Hook-specific configuration (takes precedence)
125-
'pre-commit': {
126-
'staged-lint': {
127-
'*.{js,ts}': 'bunx --bun eslint . --fix --max-warnings=0'
128-
}
129-
}
130-
}
131-
```
132-
133112
## Environment Variables
134113

135114
### SKIP_INSTALL_GIT_HOOKS
@@ -196,7 +175,7 @@ const config: GitHooksConfig = {
196175

197176
The following git hooks are supported:
198177

199-
- `pre-commit`: Run before committing
178+
- `pre-commit`: Run before committing (supports staged-lint)
200179
- `prepare-commit-msg`: Run before the commit message editor is opened
201180
- `commit-msg`: Run to verify commit message
202181
- `post-commit`: Run after committing
@@ -221,6 +200,9 @@ catch (err) {
221200
else if (err.message.includes('git root')) {
222201
console.error('Not a Git repository')
223202
}
203+
else if (err.message.includes('staged-lint is only allowed in pre-commit hook')) {
204+
console.error('Staged-lint can only be used in pre-commit hook')
205+
}
224206
}
225207
```
226208

@@ -239,9 +221,6 @@ interface GitHooksConfig {
239221
'commit-msg'?: string
240222
'post-merge'?: string
241223
// ... other git hooks
242-
'staged-lint'?: {
243-
[pattern: string]: string | string[]
244-
}
245224
'preserveUnused'?: Array<string> | boolean
246225
'verbose'?: boolean
247226
}

src/git-hooks.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,13 @@ function _getPackageJson(projectPath = process.cwd()): { packageJsonContent: any
188188
*/
189189
export function setHooksFromConfig(projectRootPath: string = process.cwd(), options?: SetHooksFromConfigOptions): void {
190190
if (!config || Object.keys(config).length === 0)
191-
throw new Error('[ERROR] Config was not found! Please add `.git-hooks.config.{ts,js,mjs,cjs,mts,cts,json}` or `git-hooks.config.{ts,js,mjs,cjs,mts,cts,json}` or the `git-hooks` entry in package.json.\r\nCheck README for details')
191+
throw new Error('[ERROR] Config was not found! Please add `.git-hooks.config.{ts,js,mjs,cjs,json}` or `git-hooks.config.{ts,js,mjs,cjs,json}` or the `git-hooks` entry in package.json.\r\nCheck README for details')
192+
193+
// Always use the provided configFile if available, otherwise use the cached config
194+
const configFile = options?.configFile || { ...config }
195+
196+
_validateStagedLintConfig(configFile)
192197

193-
const configFile = options?.configFile ? options.configFile : config
194198
// Only validate hook names that aren't options
195199
const hookKeys = Object.keys(configFile).filter(key => !VALID_OPTIONS.includes(key as typeof VALID_OPTIONS[number]))
196200
const isValidConfig = hookKeys.every(key => VALID_GIT_HOOKS.includes(key as typeof VALID_GIT_HOOKS[number]))
@@ -427,6 +431,17 @@ function _validateHooks(config: Record<string, any>): boolean {
427431
return true
428432
}
429433

434+
function _validateStagedLintConfig(config: GitHooksConfig): void {
435+
for (const hook of VALID_GIT_HOOKS) {
436+
if (hook !== 'pre-commit' && config[hook] && typeof config[hook] === 'object') {
437+
const hookConfig = config[hook] as { 'stagedLint'?: StagedLintConfig; 'staged-lint'?: StagedLintConfig }
438+
if (hookConfig['stagedLint'] || hookConfig['staged-lint']) {
439+
throw new Error(`staged-lint is only allowed in pre-commit hook. Found in ${hook} hook.`)
440+
}
441+
}
442+
}
443+
}
444+
430445
const gitHooks: {
431446
PREPEND_SCRIPT: typeof PREPEND_SCRIPT
432447
setHooksFromConfig: typeof setHooksFromConfig

0 commit comments

Comments
 (0)