@howells/husky 0.1.1 → 0.1.3
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 +21 -0
- package/README.md +3 -5
- package/bin/detect.mjs +62 -0
- package/bin/howells-husky.mjs +37 -42
- package/package.json +28 -8
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-
|
|
21
|
+
"*.{js,ts,jsx,tsx,json,jsonc,css}": "howells-fix"
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
```
|
|
@@ -29,9 +29,7 @@ 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
|
|
|
@@ -43,7 +41,7 @@ Your `package.json` must have:
|
|
|
43
41
|
|
|
44
42
|
- `"typecheck"` script (e.g. `tsc --noEmit` or `turbo run typecheck`)
|
|
45
43
|
- `"lint"` script (e.g. `howells-lint` or `turbo run lint`)
|
|
46
|
-
- `"lint-staged"` config using `howells-ox-fix`
|
|
44
|
+
- `"lint-staged"` config using `howells-fix` (`howells-ox-fix` and `howells-format` still accepted, legacy)
|
|
47
45
|
|
|
48
46
|
## Why a package?
|
|
49
47
|
|
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
|
+
}
|
package/bin/howells-husky.mjs
CHANGED
|
@@ -16,31 +16,26 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { spawnSync } from "node:child_process";
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
import {
|
|
20
|
+
copyFileSync,
|
|
21
|
+
existsSync,
|
|
22
|
+
mkdirSync,
|
|
23
|
+
readFileSync,
|
|
24
|
+
chmodSync,
|
|
25
|
+
} from "node:fs";
|
|
26
|
+
import path from "node:path";
|
|
27
|
+
|
|
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, "..");
|
|
25
38
|
const projectRoot = process.cwd();
|
|
26
|
-
const lintStagedFormatterCommands = ["howells-format", "howells-ox-fix"];
|
|
27
|
-
const recommendedLintStagedCommand = "howells-ox-fix";
|
|
28
|
-
|
|
29
|
-
function lintStagedCommandValues(config) {
|
|
30
|
-
return Object.values(config).flatMap((value) => {
|
|
31
|
-
if (typeof value === "string") {
|
|
32
|
-
return [value];
|
|
33
|
-
}
|
|
34
|
-
if (Array.isArray(value)) {
|
|
35
|
-
return value.filter((item) => typeof item === "string");
|
|
36
|
-
}
|
|
37
|
-
return [];
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function usesSupportedFormatter(command) {
|
|
42
|
-
return lintStagedFormatterCommands.some((formatter) => command.includes(formatter));
|
|
43
|
-
}
|
|
44
39
|
|
|
45
40
|
// Skip in CI environments — hooks aren't needed there
|
|
46
41
|
if (process.env.CI === "true" || process.env.VERCEL === "1") {
|
|
@@ -48,59 +43,59 @@ if (process.env.CI === "true" || process.env.VERCEL === "1") {
|
|
|
48
43
|
}
|
|
49
44
|
|
|
50
45
|
// Skip if not in a git repo (e.g. during npm pack)
|
|
51
|
-
if (!existsSync(join(projectRoot, ".git"))) {
|
|
46
|
+
if (!existsSync(path.join(projectRoot, ".git"))) {
|
|
52
47
|
process.exit(0);
|
|
53
48
|
}
|
|
54
49
|
|
|
55
50
|
// Step 1: Run husky to initialise .husky/ directory
|
|
56
|
-
const huskyBin = resolve(packageRoot, "node_modules", ".bin", "husky");
|
|
51
|
+
const huskyBin = path.resolve(packageRoot, "node_modules", ".bin", "husky");
|
|
57
52
|
const huskyResult = spawnSync(huskyBin, [], {
|
|
58
53
|
cwd: projectRoot,
|
|
59
|
-
stdio: "inherit",
|
|
60
54
|
env: process.env,
|
|
55
|
+
stdio: "inherit",
|
|
61
56
|
});
|
|
62
57
|
|
|
63
|
-
if (huskyResult
|
|
58
|
+
if (shouldUseNpxFallback(huskyResult)) {
|
|
64
59
|
// Fallback: try resolving husky from the project's node_modules
|
|
65
60
|
const fallbackResult = spawnSync("npx", ["husky"], {
|
|
66
61
|
cwd: projectRoot,
|
|
67
|
-
stdio: "inherit",
|
|
68
62
|
env: process.env,
|
|
63
|
+
stdio: "inherit",
|
|
69
64
|
});
|
|
70
65
|
|
|
71
|
-
if (
|
|
66
|
+
if (fallbackFailed(fallbackResult)) {
|
|
72
67
|
console.error("[@howells/husky] Failed to initialise husky");
|
|
73
68
|
process.exit(1);
|
|
74
69
|
}
|
|
75
70
|
}
|
|
76
71
|
|
|
77
72
|
// Step 2: Copy canonical hook scripts
|
|
78
|
-
const huskyDir = join(projectRoot, ".husky");
|
|
73
|
+
const huskyDir = path.join(projectRoot, ".husky");
|
|
79
74
|
if (!existsSync(huskyDir)) {
|
|
80
75
|
mkdirSync(huskyDir, { recursive: true });
|
|
81
76
|
}
|
|
82
77
|
|
|
83
78
|
const hooks = ["pre-commit", "pre-push"];
|
|
84
|
-
const hooksDir = join(packageRoot, "hooks");
|
|
79
|
+
const hooksDir = path.join(packageRoot, "hooks");
|
|
85
80
|
|
|
86
81
|
for (const hook of hooks) {
|
|
87
|
-
const source = join(hooksDir, hook);
|
|
88
|
-
const dest = join(huskyDir, hook);
|
|
82
|
+
const source = path.join(hooksDir, hook);
|
|
83
|
+
const dest = path.join(huskyDir, hook);
|
|
89
84
|
copyFileSync(source, dest);
|
|
90
85
|
chmodSync(dest, 0o755);
|
|
91
86
|
}
|
|
92
87
|
|
|
93
88
|
// Step 3: Validate lint-staged config
|
|
94
|
-
const packageJsonPath = join(projectRoot, "package.json");
|
|
89
|
+
const packageJsonPath = path.join(projectRoot, "package.json");
|
|
95
90
|
if (existsSync(packageJsonPath)) {
|
|
96
|
-
const packageJson = JSON.parse(readFileSync(packageJsonPath, "
|
|
91
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
97
92
|
|
|
98
93
|
if (!packageJson["lint-staged"]) {
|
|
99
94
|
console.warn(
|
|
100
|
-
'[@howells/husky] Warning: no "lint-staged" config found in package.json.'
|
|
95
|
+
'[@howells/husky] Warning: no "lint-staged" config found in package.json.'
|
|
101
96
|
);
|
|
102
97
|
console.warn(
|
|
103
|
-
` Add: "lint-staged": { "*.{js,ts,jsx,tsx,json,jsonc,css}": "${recommendedLintStagedCommand}" }
|
|
98
|
+
` Add: "lint-staged": { "*.{js,ts,jsx,tsx,json,jsonc,css}": "${recommendedLintStagedCommand}" }`
|
|
104
99
|
);
|
|
105
100
|
}
|
|
106
101
|
|
|
@@ -111,10 +106,10 @@ if (existsSync(packageJsonPath)) {
|
|
|
111
106
|
const usesHowells = commands.some(usesSupportedFormatter);
|
|
112
107
|
if (!usesHowells) {
|
|
113
108
|
console.warn(
|
|
114
|
-
"[@howells/husky] Warning: lint-staged should use a supported Howells formatter."
|
|
109
|
+
"[@howells/husky] Warning: lint-staged should use a supported Howells formatter."
|
|
115
110
|
);
|
|
116
111
|
console.warn(
|
|
117
|
-
` Expected: "*.{js,ts,jsx,tsx,json,jsonc,css}": "${recommendedLintStagedCommand}"
|
|
112
|
+
` Expected: "*.{js,ts,jsx,tsx,json,jsonc,css}": "${recommendedLintStagedCommand}"`
|
|
118
113
|
);
|
|
119
114
|
}
|
|
120
115
|
}
|
|
@@ -123,12 +118,12 @@ if (existsSync(packageJsonPath)) {
|
|
|
123
118
|
const scripts = packageJson.scripts || {};
|
|
124
119
|
if (!scripts.typecheck) {
|
|
125
120
|
console.warn(
|
|
126
|
-
'[@howells/husky] Warning: no "typecheck" script found. Pre-push hook requires it.'
|
|
121
|
+
'[@howells/husky] Warning: no "typecheck" script found. Pre-push hook requires it.'
|
|
127
122
|
);
|
|
128
123
|
}
|
|
129
124
|
if (!scripts.lint) {
|
|
130
125
|
console.warn(
|
|
131
|
-
'[@howells/husky] Warning: no "lint" script found. Pre-push hook requires it.'
|
|
126
|
+
'[@howells/husky] Warning: no "lint" script found. Pre-push hook requires it.'
|
|
132
127
|
);
|
|
133
128
|
}
|
|
134
129
|
}
|
package/package.json
CHANGED
|
@@ -1,21 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howells/husky",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
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
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"howells-husky": "bin/howells-husky.mjs"
|
|
12
|
+
},
|
|
6
13
|
"files": [
|
|
7
|
-
"bin
|
|
14
|
+
"bin/detect.mjs",
|
|
15
|
+
"bin/howells-husky.mjs",
|
|
8
16
|
"hooks/*",
|
|
9
|
-
"README.md"
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
10
19
|
],
|
|
11
|
-
"
|
|
12
|
-
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
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"
|
|
13
29
|
},
|
|
14
30
|
"dependencies": {
|
|
15
31
|
"husky": "^9.1.7",
|
|
16
32
|
"lint-staged": "^16.4.0"
|
|
17
33
|
},
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
}
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@howells/lint": "^1.1.0"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=24.15.0"
|
|
39
|
+
},
|
|
40
|
+
"packageManager": "pnpm@11.11.0"
|
|
21
41
|
}
|