Skip to content

Commit 2616531

Browse files
ci: add git hooks
1 parent 04ca5d6 commit 2616531

4 files changed

Lines changed: 70 additions & 16 deletions

File tree

.githooks/pre-commit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
repo_root="$(git rev-parse --show-toplevel)"
5+
cd "$repo_root"
6+
7+
echo "pre-commit: running lint autofix"
8+
npm run lint:fix
9+
10+
echo "pre-commit: running formatter"
11+
npm run format
12+
13+
git add -A -- src

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"build": "npm run build:rust && npm run build:ts",
1919
"build:rust": "napi build --platform --release --cargo-cwd rust rust",
2020
"build:ts": "node ./scripts/generate-browser-profiles.mjs && tsc && node ./scripts/postbuild.mjs",
21+
"prepare": "node ./scripts/install-git-hooks.mjs",
2122
"prepare:publish:main": "node ./scripts/prepare-main-package.mjs",
2223
"prepare:publish:platform": "node ./scripts/prepare-platform-package.mjs",
2324
"artifacts": "napi artifacts",

scripts/install-git-hooks.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { existsSync } from 'node:fs';
2+
import { resolve } from 'node:path';
3+
import { execFileSync } from 'node:child_process';
4+
import { fileURLToPath } from 'node:url';
5+
6+
const repoRoot = resolve(fileURLToPath(new URL('..', import.meta.url)));
7+
const gitDir = resolve(repoRoot, '.git');
8+
const hooksPath = resolve(repoRoot, '.githooks');
9+
10+
if (!existsSync(gitDir) || !existsSync(hooksPath)) {
11+
process.exit(0);
12+
}
13+
14+
try {
15+
execFileSync('git', ['config', 'core.hooksPath', '.githooks'], {
16+
cwd: repoRoot,
17+
stdio: 'ignore',
18+
});
19+
} catch (error) {
20+
console.warn('[install-git-hooks] Failed to set core.hooksPath to .githooks');
21+
console.warn(error instanceof Error ? error.message : String(error));
22+
}

src/native/binding.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { execSync } from 'node:child_process';
2+
import { resolve } from 'node:path';
23
import type {
34
NativeRequestOptions,
45
NativeResponse,
@@ -33,6 +34,14 @@ type NativeTarget = {
3334

3435
let nativeBinding: NativeBinding | undefined;
3536

37+
function tryRequire<T>(id: string): T | undefined {
38+
try {
39+
return require(id) as T;
40+
} catch {
41+
return undefined;
42+
}
43+
}
44+
3645
function isMuslRuntime(): boolean {
3746
if (process.platform !== 'linux') {
3847
return false;
@@ -111,28 +120,37 @@ function loadNativeBinding(): NativeBinding {
111120
}
112121

113122
const attempted: string[] = [target.packageName];
123+
const packageBinding = tryRequire<NativeBinding>(target.packageName);
114124

115-
try {
116-
return require(target.packageName) as NativeBinding;
117-
} catch {
118-
attempted.push(`../rust/${target.binaryName}`);
125+
if (packageBinding) {
126+
return packageBinding;
119127
}
120128

121-
try {
122-
return require(`../rust/${target.binaryName}`) as NativeBinding;
123-
} catch {
124-
attempted.push('../rust/node-wreq.node');
129+
const localBinaryPath = resolve(__dirname, '../../rust', target.binaryName);
130+
131+
attempted.push(localBinaryPath);
132+
133+
const localPlatformBinding = tryRequire<NativeBinding>(localBinaryPath);
134+
135+
if (localPlatformBinding) {
136+
return localPlatformBinding;
125137
}
126138

127-
try {
128-
return require('../rust/node-wreq.node') as NativeBinding;
129-
} catch {
130-
throw new Error(
131-
`Failed to load native module for ${platform}-${arch}. ` +
132-
`Tried: ${attempted.join(', ')}. ` +
133-
`Make sure the matching @node-wreq platform package is installed or build the local native module.`
134-
);
139+
const localGenericBinaryPath = resolve(__dirname, '../../rust/node-wreq.node');
140+
141+
attempted.push(localGenericBinaryPath);
142+
143+
const localGenericBinding = tryRequire<NativeBinding>(localGenericBinaryPath);
144+
145+
if (localGenericBinding) {
146+
return localGenericBinding;
135147
}
148+
149+
throw new Error(
150+
`Failed to load native module for ${platform}-${arch}. ` +
151+
`Tried: ${attempted.join(', ')}. ` +
152+
`Make sure the matching @node-wreq platform package is installed or build the local native module.`
153+
);
136154
}
137155

138156
export function getBinding(): NativeBinding {

0 commit comments

Comments
 (0)