bun-git-hooks 0.2.19 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,59 @@
1
+ import process from 'node:process';
2
+ import type { GitHooksConfig, StagedLintConfig } from './types';
3
+ /**
4
+ * Enhanced staged lint function with configurable auto-restaging
5
+ */
6
+ export declare function runEnhancedStagedLint(config: StagedLintConfig, projectRoot?: string, options?: { verbose?: boolean, autoRestage?: boolean }): Promise<boolean>;
7
+ /**
8
+ * Check if a hook name is valid (supports both camelCase and kebab-case)
9
+ */
10
+ export declare function isValidHookName(hookName: string, validHooks: readonly string[]): boolean;
11
+ /**
12
+ * Main staged lint function that should be used by git hooks
13
+ * This is the primary entry point for staged lint functionality
14
+ */
15
+ export declare function runStagedLint(hook: string, config: GitHooksConfig, projectRoot: string, verbose?: boolean, autoRestage?: boolean): Promise<boolean>;
16
+ /**
17
+ * Hook name mapping between camelCase and kebab-case
18
+ */
19
+ export declare const HOOK_NAME_MAP: {
20
+ 'preCommit': 'pre-commit';
21
+ 'prepareCommitMsg': 'prepare-commit-msg';
22
+ 'commitMsg': 'commit-msg';
23
+ 'postCommit': 'post-commit';
24
+ 'prePush': 'pre-push';
25
+ 'postMerge': 'post-merge';
26
+ 'postCheckout': 'post-checkout';
27
+ 'preRebase': 'pre-rebase';
28
+ 'postRewrite': 'post-rewrite';
29
+ // Reverse mapping
30
+ 'pre-commit': 'preCommit';
31
+ 'prepare-commit-msg': 'prepareCommitMsg';
32
+ 'commit-msg': 'commitMsg';
33
+ 'post-commit': 'postCommit';
34
+ 'pre-push': 'prePush';
35
+ 'post-merge': 'postMerge';
36
+ 'post-checkout': 'postCheckout';
37
+ 'pre-rebase': 'preRebase';
38
+ 'post-rewrite': 'postRewrite'
39
+ };
40
+ /**
41
+ * Enhanced staged lint processor with configurable auto-restaging
42
+ */
43
+ export declare class StagedLintProcessor {
44
+ private projectRoot: string;
45
+ private verbose: boolean;
46
+ private autoRestage: boolean;
47
+ constructor(projectRoot?: string, verbose?: boolean, autoRestage?: boolean);
48
+ process(config: StagedLintConfig): Promise<boolean>;
49
+ private getStagedFiles(): string[];
50
+ private captureStagedContent(files: string[]): Map<string, string>;
51
+ private getMatchingFiles(files: string[], pattern: string): string[];
52
+ private expandBracePattern(pattern: string): string[];
53
+ private matchesGlob(file: string, pattern: string): boolean;
54
+ private runLintCommand(command: string, files: string[]): Promise<boolean>;
55
+ private getModifiedFiles(originalContent: Map<string, string>): string[];
56
+ private restageFiles(files: string[]): void;
57
+ private validateStagedFiles(config: StagedLintConfig): Promise<boolean>;
58
+ private log(message: string): void;
59
+ }
package/dist/types.d.ts CHANGED
@@ -1,20 +1,33 @@
1
- export declare type StagedLintTask = string | string[]
1
+ import type { VALID_GIT_HOOKS } from './git-hooks';
2
+ export declare interface StagedLintConfig {
2
3
 
3
- export interface StagedLintConfig {
4
- [pattern: string]: StagedLintTask
5
4
  }
6
-
5
+ export declare interface SetHooksFromConfigOptions {
6
+ configFile?: GitHooksConfig
7
+ verbose?: boolean
8
+ }
9
+ export type StagedLintTask = string | string[]
10
+ // Define camelCase hook names
11
+ declare type CamelCaseHooks = | 'preCommit'
12
+ | 'prepareCommitMsg'
13
+ | 'commitMsg'
14
+ | 'postCommit'
15
+ | 'prePush'
16
+ | 'postMerge'
17
+ | 'postCheckout'
18
+ | 'preRebase'
19
+ | 'postRewrite'
7
20
  export type GitHooksConfig = {
8
- [K in typeof VALID_GIT_HOOKS[number]]?: string | {
21
+ // Support both kebab-case (from VALID_GIT_HOOKS) and camelCase
22
+ [K in typeof VALID_GIT_HOOKS[number] | CamelCaseHooks]?: string | {
9
23
  'stagedLint'?: StagedLintConfig
10
- 'staged-lint'?: StagedLintConfig
24
+ 'staged-lint'?: StagedLintConfig // Legacy support
25
+ 'autoRestage'?: boolean // Allow per-hook autoRestage setting
11
26
  }
12
27
  } & {
13
- 'preserveUnused'?: boolean | typeof VALID_GIT_HOOKS[number][]
28
+ 'preserveUnused'?: boolean | (typeof VALID_GIT_HOOKS[number] | CamelCaseHooks)[]
14
29
  'verbose'?: boolean
15
- 'staged-lint'?: StagedLintConfig
16
- }
17
-
18
- export interface SetHooksFromConfigOptions {
19
- configFile?: GitHooksConfig
30
+ 'stagedLint'?: StagedLintConfig
31
+ 'staged-lint'?: StagedLintConfig // Legacy support
32
+ 'autoRestage'?: boolean // Global autoRestage setting
20
33
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-git-hooks",
3
3
  "type": "module",
4
- "version": "0.2.19",
4
+ "version": "0.3.0",
5
5
  "description": "A modern, zero dependency tool for managing git hooks in Bun projects.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -41,7 +41,11 @@
41
41
  "git-hooks": "./dist/bin/cli.js",
42
42
  "bun-git-hooks": "./dist/bin/cli.js"
43
43
  },
44
- "files": ["README.md", "dist"],
44
+ "files": [
45
+ "README.md",
46
+ "dist",
47
+ "scripts"
48
+ ],
45
49
  "scripts": {
46
50
  "build": "bun build.ts && bun run compile",
47
51
  "compile": "bun build ./bin/cli.ts --compile --minify --outfile bin/git-hooks",
@@ -53,13 +57,14 @@
53
57
  "compile:darwin-arm64": "bun build ./bin/cli.ts --compile --minify --target=bun-darwin-arm64 --outfile bin/git-hooks-darwin-arm64",
54
58
  "postinstall": "bun ./scripts/postinstall.ts",
55
59
  "uninstall": "bun ./scripts/uninstall.ts",
56
- "lint": "bunx --bun eslint .",
57
- "lint:fix": "bunx --bun eslint . --fix",
58
60
  "fresh": "bunx rimraf node_modules/ bun.lock && bun i",
59
- "changelog": "bunx changelogen --output CHANGELOG.md",
60
61
  "prepublishOnly": "bun --bun run build && bun run compile:all",
61
- "release": "bun run changelog && bunx bumpp package.json --all",
62
62
  "test": "bun test",
63
+ "lint": "bunx --bun eslint .",
64
+ "lint:fix": "bunx --bun eslint . --fix",
65
+ "changelog": "bunx logsmith --verbose",
66
+ "changelog:generate": "bunx logsmith --output CHANGELOG.md",
67
+ "release": "bunx bumpx prompt --recursive --all",
63
68
  "dev:docs": "bun --bun vitepress dev docs",
64
69
  "build:docs": "bun --bun vitepress build docs",
65
70
  "preview:docs": "bun --bun vitepress preview docs",
@@ -73,17 +78,27 @@
73
78
  "zip:darwin-arm64": "zip -j bin/git-hooks-darwin-arm64.zip bin/git-hooks-darwin-arm64"
74
79
  },
75
80
  "devDependencies": {
76
- "@stacksjs/clarity": "^0.3.15",
81
+ "@stacksjs/bumpx": "^0.1.84",
82
+ "@stacksjs/clarity": "^0.3.23",
77
83
  "@stacksjs/docs": "^0.70.23",
78
- "@stacksjs/eslint-config": "^4.10.2-beta.3",
84
+ "@stacksjs/eslint-config": "^4.14.0-beta.3",
79
85
  "@stacksjs/gitlint": "^0.1.5",
80
- "@types/bun": "^1.2.19",
81
- "bumpp": "^10.2.0",
82
- "bun-plugin-dtsx": "^0.21.12",
83
- "bunfig": "^0.10.1",
86
+ "@stacksjs/logsmith": "^0.1.18",
87
+ "@types/bun": "^1.2.22",
88
+ "buddy-bot": "^0.9.7",
89
+ "bun-plugin-dtsx": "0.9.5",
90
+ "bunfig": "^0.15.0",
84
91
  "cac": "^6.7.14",
85
- "changelogen": "^0.6.2",
86
- "typescript": "^5.8.3"
92
+ "typescript": "^5.9.2"
93
+ },
94
+ "git-hooks": {
95
+ "pre-commit": {
96
+ "stagedLint": {
97
+ "*.{js,ts,json,yaml,yml,md}": "bunx --bun eslint --fix"
98
+ },
99
+ "autoRestage": true
100
+ },
101
+ "commit-msg": "bunx gitlint --edit .git/COMMIT_EDITMSG"
87
102
  },
88
103
  "overrides": {
89
104
  "unconfig": "0.3.10"
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { mkdir, symlink } from 'node:fs/promises'
4
+ import { join } from 'node:path'
5
+ import process from 'node:process'
6
+ import { areHooksInstalled, checkBunGitHooksInDependencies, getProjectRootDirectoryFromNodeModules, setHooksFromConfig } from '../src/git-hooks'
7
+
8
+ /**
9
+ * Creates the pre-commit from command in config by default
10
+ */
11
+ async function postinstall() {
12
+ let projectDirectory
13
+
14
+ /* When script is run after install, the process.cwd() would be like <project_folder>/node_modules/simple-git-hooks
15
+ Here we try to get the original project directory by going upwards by 2 levels
16
+ If we were not able to get new directory we assume, we are already in the project root */
17
+ const parsedProjectDirectory = getProjectRootDirectoryFromNodeModules(process.cwd())
18
+ if (parsedProjectDirectory !== undefined) {
19
+ projectDirectory = parsedProjectDirectory
20
+ }
21
+ else {
22
+ projectDirectory = process.cwd()
23
+ }
24
+
25
+ // Link the binary
26
+ const binDir = join(projectDirectory, 'node_modules', '.bin')
27
+ await mkdir(binDir, { recursive: true })
28
+
29
+ const sourcePath = join(process.cwd(), 'dist', 'bin', 'cli.js')
30
+ const targetPath = join(binDir, 'bun-git-hooks')
31
+ const targetPath2 = join(binDir, 'git-hooks')
32
+
33
+ try {
34
+ await symlink(sourcePath, targetPath, 'file')
35
+ await symlink(sourcePath, targetPath2, 'file')
36
+ }
37
+ catch (err) {
38
+ if ((err as NodeJS.ErrnoException).code !== 'EEXIST') {
39
+ console.error(`[ERROR] Failed to link binary: ${err}`)
40
+ }
41
+ }
42
+
43
+ if (checkBunGitHooksInDependencies(projectDirectory)) {
44
+ try {
45
+ // Check if hooks are already installed to avoid unnecessary reinstalls
46
+ if (areHooksInstalled(projectDirectory)) {
47
+ console.log('[INFO] Git hooks are already installed, skipping setup')
48
+ return
49
+ }
50
+
51
+ setHooksFromConfig(projectDirectory)
52
+ console.log('[INFO] Git hooks installed successfully')
53
+ }
54
+ catch (err) {
55
+ console.log(`[ERROR] Was not able to set git hooks. Reason: ${err}`)
56
+ }
57
+ }
58
+ }
59
+
60
+ postinstall()
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { removeHooks } from '../src/git-hooks'
4
+
5
+ /**
6
+ * Removes the pre-commit from command in config by default
7
+ */
8
+ function uninstall() {
9
+ console.log('[INFO] Removing git hooks from .git/hooks')
10
+
11
+ try {
12
+ removeHooks()
13
+ console.log('[INFO] Successfully removed all git hooks')
14
+ }
15
+ catch (e) {
16
+ console.log(`[INFO] Couldn't remove git hooks. Reason: ${e}`)
17
+ }
18
+ }
19
+
20
+ uninstall()