Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/husky-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nostream": patch
---

fix: add husky install fallback for non-dev environments
18 changes: 18 additions & 0 deletions .husky/install.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { existsSync } from 'node:fs';
import { execSync } from 'node:child_process';

// Skip Husky installation in environments where hooks are not needed
if (
process.env.NODE_ENV === 'production' ||
process.env.CI === 'true' ||
process.env.HUSKY === '0' ||
!existsSync('.git')
) {
process.exit(0);
}

try {
execSync('npx husky install', { stdio: 'ignore' });
} catch {
process.exit(0);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"docker:cover:integration": "pnpm run docker:integration:run pnpm exec nyc --report-dir .coverage/integration pnpm run test:integration -- -p cover",
"postdocker:integration:run": "docker compose -f ./test/integration/docker-compose.yml down",
"prepack": "pnpm run build",
"prepare": "husky install || exit 0",
"prepare": "test -f .husky/install.mjs && node .husky/install.mjs || true",
"changeset:version": "changeset version && pnpm install --lockfile-only",
"changeset:publish": "changeset publish"
},
Expand Down
57 changes: 57 additions & 0 deletions test/unit/husky/install.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expect } from 'chai'
import fs from 'fs'
import os from 'os'
import path from 'path'
import { spawnSync } from 'child_process'

const projectRoot = process.cwd()
const installScriptPath = path.join(projectRoot, '.husky', 'install.mjs')

const runInstall = (cwd: string, env: NodeJS.ProcessEnv = {}) => {
return spawnSync('node', [installScriptPath], {
cwd,
env: {
...process.env,
...env,
},
encoding: 'utf-8',
timeout: 10_000,
})
}

describe('husky install script', () => {
it('exits successfully when .git is missing', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-no-git-'))

try {
const result = runInstall(tmpDir)
expect(result.status).to.equal(0)
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true })
}
})

it('exits successfully when HUSKY is disabled', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-disabled-'))

try {
const result = runInstall(tmpDir, { HUSKY: '0' })
expect(result.status).to.equal(0)
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true })
}
})

it('exits successfully when husky package is unavailable even if .git exists', function () {
this.timeout(15_000)
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-missing-package-'))

try {
fs.mkdirSync(path.join(tmpDir, '.git'))
const result = runInstall(tmpDir)
expect(result.status).to.equal(0)
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true })
}
})
})
Loading