@howells/husky 0.1.2 → 0.2.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Daniel Howells
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -18,7 +18,7 @@ In `package.json`:
18
18
  "prepare": "howells-husky"
19
19
  },
20
20
  "lint-staged": {
21
- "*.{js,ts,jsx,tsx,json,jsonc,css}": "howells-ox-fix"
21
+ "*.{js,ts,jsx,tsx,json,jsonc,css}": "howells-fix"
22
22
  }
23
23
  }
24
24
  ```
@@ -29,21 +29,38 @@ That's it. On `pnpm install`, the hooks are installed automatically.
29
29
 
30
30
  ### Pre-commit
31
31
 
32
- Runs `pnpm lint-staged` — formats staged files with the configured Howells formatter.
33
- Use `howells-ox-fix` for projects on the Ox lint path. `howells-format` remains supported
34
- for older projects.
32
+ Runs `pnpm lint-staged` — formats staged files with the configured Howells formatter. Use `howells-fix`. The older names `howells-ox-fix` and `howells-format` are still accepted (legacy) for projects mid-migration.
35
33
 
36
34
  ### Pre-push
37
35
 
38
36
  Runs `pnpm typecheck` and `pnpm lint`. Both must pass before code reaches the remote.
39
37
 
38
+ Both run against the **working directory**, so their result only describes what is being pushed when the working directory is what is being pushed. Git names the refs on stdin, so the hook can tell:
39
+
40
+ | What you are pushing | What happens |
41
+ | --- | --- |
42
+ | The commit you have checked out | Typecheck and lint run. A failure blocks the push. |
43
+ | A ref that is not `HEAD` | Skipped, and said out loud. The checks would describe a different tree. |
44
+ | A branch deletion | Ignored; nothing is being added to check. |
45
+ | Nothing on stdin | Checked anyway. An unverified push is what this hook exists to stop. |
46
+
47
+ **Why a non-`HEAD` push is skipped rather than failed.** It is neither a pass nor a failure - it is unverifiable, and reporting the wrong tree's result is worse than admitting the hook cannot see. The case this matters for is the rescue path: work that exists in one place, in a tree that does not typecheck, is exactly what most needs pushing. The old behaviour failed that push for a reason unrelated to what was being pushed, leaving `--no-verify` as the only route - which disables every gate at once.
48
+
49
+ ### The hooks are immutable, and now they say so
50
+
51
+ `.husky/pre-commit` and `.husky/pre-push` are overwritten from this package on every install. That is deliberate: a repo cannot weaken its own gate to get something to pass.
52
+
53
+ What changed is that it is no longer silent. If an install replaces a hook whose contents differ from the packaged one, it says so and names the file. Before, a customised hook vanished on the next `pnpm install` with nothing printed, so whoever wrote it believed it was in effect until it demonstrably was not.
54
+
55
+ **To change a hook, change it here and publish.** There is no per-repo override, by design.
56
+
40
57
  ## Requirements
41
58
 
42
59
  Your `package.json` must have:
43
60
 
44
61
  - `"typecheck"` script (e.g. `tsc --noEmit` or `turbo run typecheck`)
45
62
  - `"lint"` script (e.g. `howells-lint` or `turbo run lint`)
46
- - `"lint-staged"` config using `howells-ox-fix` or `howells-format`
63
+ - `"lint-staged"` config using `howells-fix` (`howells-ox-fix` and `howells-format` still accepted, legacy)
47
64
 
48
65
  ## Why a package?
49
66
 
package/bin/detect.mjs ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Pure formatter-detection helpers for @howells/husky.
3
+ *
4
+ * Kept free of side effects so the install-time bin can stay a thin shell
5
+ * around behaviour that is unit-testable in isolation.
6
+ */
7
+
8
+ // `howells-fix` is the current canonical @howells/lint fixer; `howells-ox-fix`
9
+ // and `howells-format` are earlier names still in use by repos mid-migration.
10
+ // Accept all three so a correct lint-staged config never draws a false warning.
11
+ export const lintStagedFormatterCommands = [
12
+ "howells-fix",
13
+ "howells-ox-fix",
14
+ "howells-format",
15
+ ];
16
+
17
+ export const recommendedLintStagedCommand = "howells-fix";
18
+
19
+ /**
20
+ * Flatten a lint-staged config object into the list of command strings it runs.
21
+ * Values may be a single command string or an array of them.
22
+ */
23
+ export function lintStagedCommandValues(config) {
24
+ return Object.values(config).flatMap((value) => {
25
+ if (typeof value === "string") {
26
+ return [value];
27
+ }
28
+ if (Array.isArray(value)) {
29
+ return value.filter((item) => typeof item === "string");
30
+ }
31
+ return [];
32
+ });
33
+ }
34
+
35
+ /**
36
+ * True when a single lint-staged command invokes a supported Howells formatter.
37
+ */
38
+ export function usesSupportedFormatter(command) {
39
+ return lintStagedFormatterCommands.some((formatter) =>
40
+ command.includes(formatter)
41
+ );
42
+ }
43
+
44
+ /**
45
+ * Decide whether the primary `husky` spawn failed to launch and we should
46
+ * fall back to `npx husky`. A spawn that launched but exited non-zero is not a
47
+ * launch failure — only a spawn `error` (e.g. ENOENT) warrants the fallback.
48
+ */
49
+ export function shouldUseNpxFallback(primaryResult) {
50
+ return Boolean(primaryResult.error);
51
+ }
52
+
53
+ /**
54
+ * True when the npx fallback itself failed — either it could not launch, or it
55
+ * launched and exited with a non-zero status.
56
+ */
57
+ export function fallbackFailed(fallbackResult) {
58
+ return Boolean(
59
+ fallbackResult.error ||
60
+ (fallbackResult.status !== null && fallbackResult.status !== 0)
61
+ );
62
+ }
@@ -23,39 +23,19 @@ import {
23
23
  readFileSync,
24
24
  chmodSync,
25
25
  } from "node:fs";
26
- import { dirname, join, resolve } from "node:path";
27
- import { fileURLToPath } from "node:url";
26
+ import path from "node:path";
28
27
 
29
- const __dirname = import.meta.dirname;
30
- const packageRoot = resolve(__dirname, "..");
28
+ import {
29
+ fallbackFailed,
30
+ lintStagedCommandValues,
31
+ recommendedLintStagedCommand,
32
+ shouldUseNpxFallback,
33
+ usesSupportedFormatter,
34
+ } from "./detect.mjs";
35
+
36
+ const scriptDir = import.meta.dirname;
37
+ const packageRoot = path.resolve(scriptDir, "..");
31
38
  const projectRoot = process.cwd();
32
- // `howells-fix` is the current canonical @howells/lint fixer; `howells-ox-fix`
33
- // and `howells-format` are earlier names still in use by repos mid-migration.
34
- // Accept all three so a correct lint-staged config never draws a false warning.
35
- const lintStagedFormatterCommands = [
36
- "howells-fix",
37
- "howells-ox-fix",
38
- "howells-format",
39
- ];
40
- const recommendedLintStagedCommand = "howells-fix";
41
-
42
- function lintStagedCommandValues(config) {
43
- return Object.values(config).flatMap((value) => {
44
- if (typeof value === "string") {
45
- return [value];
46
- }
47
- if (Array.isArray(value)) {
48
- return value.filter((item) => typeof item === "string");
49
- }
50
- return [];
51
- });
52
- }
53
-
54
- function usesSupportedFormatter(command) {
55
- return lintStagedFormatterCommands.some((formatter) =>
56
- command.includes(formatter)
57
- );
58
- }
59
39
 
60
40
  // Skip in CI environments — hooks aren't needed there
61
41
  if (process.env.CI === "true" || process.env.VERCEL === "1") {
@@ -63,19 +43,19 @@ if (process.env.CI === "true" || process.env.VERCEL === "1") {
63
43
  }
64
44
 
65
45
  // Skip if not in a git repo (e.g. during npm pack)
66
- if (!existsSync(join(projectRoot, ".git"))) {
46
+ if (!existsSync(path.join(projectRoot, ".git"))) {
67
47
  process.exit(0);
68
48
  }
69
49
 
70
50
  // Step 1: Run husky to initialise .husky/ directory
71
- const huskyBin = resolve(packageRoot, "node_modules", ".bin", "husky");
51
+ const huskyBin = path.resolve(packageRoot, "node_modules", ".bin", "husky");
72
52
  const huskyResult = spawnSync(huskyBin, [], {
73
53
  cwd: projectRoot,
74
54
  env: process.env,
75
55
  stdio: "inherit",
76
56
  });
77
57
 
78
- if (huskyResult.error) {
58
+ if (shouldUseNpxFallback(huskyResult)) {
79
59
  // Fallback: try resolving husky from the project's node_modules
80
60
  const fallbackResult = spawnSync("npx", ["husky"], {
81
61
  cwd: projectRoot,
@@ -83,33 +63,54 @@ if (huskyResult.error) {
83
63
  stdio: "inherit",
84
64
  });
85
65
 
86
- if (
87
- fallbackResult.error ||
88
- (fallbackResult.status !== null && fallbackResult.status !== 0)
89
- ) {
66
+ if (fallbackFailed(fallbackResult)) {
90
67
  console.error("[@howells/husky] Failed to initialise husky");
91
68
  process.exit(1);
92
69
  }
93
70
  }
94
71
 
95
72
  // Step 2: Copy canonical hook scripts
96
- const huskyDir = join(projectRoot, ".husky");
73
+ const huskyDir = path.join(projectRoot, ".husky");
97
74
  if (!existsSync(huskyDir)) {
98
75
  mkdirSync(huskyDir, { recursive: true });
99
76
  }
100
77
 
101
78
  const hooks = ["pre-commit", "pre-push"];
102
- const hooksDir = join(packageRoot, "hooks");
79
+ const hooksDir = path.join(packageRoot, "hooks");
103
80
 
81
+ // The overwrite is deliberate: the hooks are the package's, not the project's,
82
+ // so a weakened gate cannot survive an install. What was missing is that it
83
+ // happened in silence — a customised hook vanished on the next `pnpm install`
84
+ // with nothing said, so the author believed their change was in effect until
85
+ // it demonstrably was not. Replacing is still unconditional; it is now audible.
104
86
  for (const hook of hooks) {
105
- const source = join(hooksDir, hook);
106
- const dest = join(huskyDir, hook);
87
+ const source = path.join(hooksDir, hook);
88
+ const dest = path.join(huskyDir, hook);
89
+
90
+ let replacedLocalEdit = false;
91
+ if (existsSync(dest)) {
92
+ replacedLocalEdit =
93
+ readFileSync(dest, "utf-8") !== readFileSync(source, "utf-8");
94
+ }
95
+
107
96
  copyFileSync(source, dest);
108
97
  chmodSync(dest, 0o755);
98
+
99
+ if (replacedLocalEdit) {
100
+ console.warn(
101
+ `[@howells/husky] Replaced .husky/${hook} with the packaged version; your local edits to it are gone.`
102
+ );
103
+ console.warn(
104
+ " These hooks are intentionally immutable, so a repo cannot weaken its own gate."
105
+ );
106
+ console.warn(
107
+ " Change them in @howells/husky and publish, or the next install reverts you again."
108
+ );
109
+ }
109
110
  }
110
111
 
111
112
  // Step 3: Validate lint-staged config
112
- const packageJsonPath = join(projectRoot, "package.json");
113
+ const packageJsonPath = path.join(projectRoot, "package.json");
113
114
  if (existsSync(packageJsonPath)) {
114
115
  const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
115
116
 
package/hooks/pre-push CHANGED
@@ -1,2 +1,58 @@
1
+ #!/usr/bin/env sh
2
+ #
3
+ # Pre-push gate: typecheck and lint before anything reaches a remote.
4
+ #
5
+ # Both checks run against the WORKING DIRECTORY, so their result only describes
6
+ # what is being pushed when the working directory *is* what is being pushed.
7
+ #
8
+ # Git says what is actually being pushed on stdin, one line per ref:
9
+ #
10
+ # <local ref> <local sha> <remote ref> <remote sha>
11
+ #
12
+ # When a pushed sha is not HEAD - archiving a salvage ref, pushing a branch that
13
+ # is not checked out - the checks would be measuring a different tree. That is
14
+ # neither a pass nor a failure. It is unverifiable, and it is reported as such
15
+ # rather than folded into either, because a gate that reports the wrong tree's
16
+ # result is worse than one that admits it cannot see.
17
+ #
18
+ # This matters most on the rescue path. Work that exists in one place, in a tree
19
+ # that does not typecheck, is exactly what most needs pushing - and the old
20
+ # behaviour failed that push for a reason unrelated to the thing being pushed,
21
+ # leaving `--no-verify` as the only route and disabling every gate at once.
22
+
23
+ ZERO=0000000000000000000000000000000000000000
24
+
25
+ head_sha=$(git rev-parse HEAD 2>/dev/null)
26
+ unverifiable=""
27
+ saw_ref=0
28
+
29
+ while read -r local_ref local_sha _remote_ref _remote_sha; do
30
+ [ -z "$local_sha" ] && continue
31
+ saw_ref=1
32
+
33
+ # A zero local sha is a branch deletion. Nothing is being added to check.
34
+ [ "$local_sha" = "$ZERO" ] && continue
35
+
36
+ # The common case: pushing the commit that is checked out. The working
37
+ # directory is the right tree and the checks below describe it.
38
+ [ "$local_sha" = "$head_sha" ] && continue
39
+
40
+ unverifiable="$unverifiable $local_ref"
41
+ done
42
+
43
+ if [ -n "$unverifiable" ]; then
44
+ echo "[@howells/husky] Not verified:$unverifiable"
45
+ echo " These refs are not HEAD, so typecheck and lint would describe a"
46
+ echo " different tree than the one being pushed. Skipped rather than"
47
+ echo " reported as passing. Verify on the branch itself, or in CI."
48
+ exit 0
49
+ fi
50
+
51
+ # No refs on stdin means git did not tell us what it is pushing. Check anyway:
52
+ # an unverified push is the thing this hook exists to prevent.
53
+ if [ "$saw_ref" = "0" ]; then
54
+ echo "[@howells/husky] No refs on stdin; checking the working directory."
55
+ fi
56
+
1
57
  pnpm typecheck || exit 1
2
58
  pnpm lint || exit 1
package/package.json CHANGED
@@ -1,29 +1,41 @@
1
1
  {
2
2
  "name": "@howells/husky",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Standardised git hooks for Howells projects. Immutable pre-commit and pre-push configuration.",
5
5
  "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/commoninstruments/husky.git"
9
+ },
6
10
  "bin": {
7
11
  "howells-husky": "bin/howells-husky.mjs"
8
12
  },
9
13
  "files": [
10
- "bin/*.mjs",
14
+ "bin/detect.mjs",
15
+ "bin/howells-husky.mjs",
11
16
  "hooks/*",
12
- "README.md"
17
+ "README.md",
18
+ "LICENSE"
13
19
  ],
20
+ "type": "module",
14
21
  "exports": {
15
22
  "./package.json": "./package.json"
16
23
  },
17
24
  "scripts": {
18
- "lint": "howells-ox-check --config=oxlint.config.ts package.json bin hooks README.md oxlint.config.ts oxfmt.config.ts",
19
- "lint:fix": "howells-ox-fix --config=oxlint.config.ts package.json bin hooks README.md oxlint.config.ts oxfmt.config.ts",
20
- "format": "howells-oxfmt --write package.json bin hooks README.md oxlint.config.ts oxfmt.config.ts README.md"
25
+ "lint": "howells-check --config=oxlint.config.ts package.json bin hooks README.md oxlint.config.ts oxfmt.config.ts",
26
+ "lint:fix": "howells-fix --config=oxlint.config.ts package.json bin hooks README.md oxlint.config.ts oxfmt.config.ts",
27
+ "format": "howells-oxfmt --write package.json bin hooks README.md oxlint.config.ts oxfmt.config.ts",
28
+ "test": "node --test bin/detect.test.mjs bin/pre-push.test.mjs"
21
29
  },
22
30
  "dependencies": {
23
31
  "husky": "^9.1.7",
24
32
  "lint-staged": "^16.4.0"
25
33
  },
26
34
  "devDependencies": {
27
- "@howells/lint": "^0.2.1"
28
- }
35
+ "@howells/lint": "^1.1.0"
36
+ },
37
+ "engines": {
38
+ "node": ">=24.15.0"
39
+ },
40
+ "packageManager": "pnpm@11.11.0"
29
41
  }