Skip to content

Commit aacefe6

Browse files
authored
Merge branch 'main' into feat/marmot-protocol-v2
2 parents 01fca3e + f31be1c commit aacefe6

4 files changed

Lines changed: 81 additions & 1 deletion

File tree

.changeset/husky-fallback.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"nostream": patch
3+
---
4+
5+
fix: add husky install fallback for non-dev environments

.husky/install.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { existsSync } from 'node:fs';
2+
import { execSync } from 'node:child_process';
3+
4+
// Skip Husky installation in environments where hooks are not needed
5+
if (
6+
process.env.NODE_ENV === 'production' ||
7+
process.env.CI === 'true' ||
8+
process.env.HUSKY === '0' ||
9+
!existsSync('.git')
10+
) {
11+
process.exit(0);
12+
}
13+
14+
try {
15+
execSync('npx husky install', { stdio: 'ignore' });
16+
} catch {
17+
process.exit(0);
18+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"docker:cover:integration": "pnpm run docker:integration:run pnpm exec nyc --report-dir .coverage/integration pnpm run test:integration -- -p cover",
9797
"postdocker:integration:run": "docker compose -f ./test/integration/docker-compose.yml down",
9898
"prepack": "pnpm run build",
99-
"prepare": "husky install || exit 0",
99+
"prepare": "test -f .husky/install.mjs && node .husky/install.mjs || true",
100100
"changeset:version": "changeset version && pnpm install --lockfile-only",
101101
"changeset:publish": "changeset publish"
102102
},

test/unit/husky/install.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { expect } from 'chai'
2+
import fs from 'fs'
3+
import os from 'os'
4+
import path from 'path'
5+
import { spawnSync } from 'child_process'
6+
7+
const projectRoot = process.cwd()
8+
const installScriptPath = path.join(projectRoot, '.husky', 'install.mjs')
9+
10+
const runInstall = (cwd: string, env: NodeJS.ProcessEnv = {}) => {
11+
return spawnSync('node', [installScriptPath], {
12+
cwd,
13+
env: {
14+
...process.env,
15+
...env,
16+
},
17+
encoding: 'utf-8',
18+
timeout: 10_000,
19+
})
20+
}
21+
22+
describe('husky install script', () => {
23+
it('exits successfully when .git is missing', () => {
24+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-no-git-'))
25+
26+
try {
27+
const result = runInstall(tmpDir)
28+
expect(result.status).to.equal(0)
29+
} finally {
30+
fs.rmSync(tmpDir, { recursive: true, force: true })
31+
}
32+
})
33+
34+
it('exits successfully when HUSKY is disabled', () => {
35+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-disabled-'))
36+
37+
try {
38+
const result = runInstall(tmpDir, { HUSKY: '0' })
39+
expect(result.status).to.equal(0)
40+
} finally {
41+
fs.rmSync(tmpDir, { recursive: true, force: true })
42+
}
43+
})
44+
45+
it('exits successfully when husky package is unavailable even if .git exists', function () {
46+
this.timeout(15_000)
47+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-missing-package-'))
48+
49+
try {
50+
fs.mkdirSync(path.join(tmpDir, '.git'))
51+
const result = runInstall(tmpDir)
52+
expect(result.status).to.equal(0)
53+
} finally {
54+
fs.rmSync(tmpDir, { recursive: true, force: true })
55+
}
56+
})
57+
})

0 commit comments

Comments
 (0)