Skip to content

Commit 5e850c0

Browse files
fix(cli): add managed bin to hook path
1 parent 2732f23 commit 5e850c0

4 files changed

Lines changed: 89 additions & 1 deletion

File tree

packages/cli/snap-tests-global/migration-add-git-hooks/snap.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ i="${XDG_CONFIG_HOME:-$HOME/.config}/vite-plus/hooks-init.sh"
5959

6060
{ [ "${HUSKY-}" = "0" ] || [ "${VITE_GIT_HOOKS-}" = "0" ]; } && exit 0
6161

62+
if [ -n "${VP_HOME-}" ]; then
63+
__vp_bin="$VP_HOME/bin"
64+
elif [ -n "${HOME-}" ]; then
65+
__vp_bin="$HOME/.vite-plus/bin"
66+
else
67+
__vp_bin=""
68+
fi
69+
[ -n "$__vp_bin" ] && [ -d "$__vp_bin" ] && export PATH="$__vp_bin:$PATH"
70+
6271
d="$(dirname "$(dirname "$(dirname "$0")")")"
6372
export PATH="$d/node_modules/.bin:$PATH"
6473
sh -e "$s" "$@"

packages/cli/snap-tests-global/migration-composed-husky-custom-dir/snap.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ i="${XDG_CONFIG_HOME:-$HOME/.config}/vite-plus/hooks-init.sh"
5151

5252
{ [ "${HUSKY-}" = "0" ] || [ "${VITE_GIT_HOOKS-}" = "0" ]; } && exit 0
5353

54+
if [ -n "${VP_HOME-}" ]; then
55+
__vp_bin="$VP_HOME/bin"
56+
elif [ -n "${HOME-}" ]; then
57+
__vp_bin="$HOME/.vite-plus/bin"
58+
else
59+
__vp_bin=""
60+
fi
61+
[ -n "$__vp_bin" ] && [ -d "$__vp_bin" ] && export PATH="$__vp_bin:$PATH"
62+
5463
d="$(dirname "$(dirname "$(dirname "$(dirname "$0")")")")"
5564
export PATH="$d/node_modules/.bin:$PATH"
5665
sh -e "$s" "$@"

packages/cli/src/config/__tests__/hooks.spec.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { execSync } from 'node:child_process';
2-
import { existsSync, mkdtempSync, rmSync } from 'node:fs';
2+
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
33
import { tmpdir } from 'node:os';
44
import { join } from 'node:path';
55

@@ -73,4 +73,65 @@ describe('hookScript', () => {
7373
expect(countDirnameCalls(withDot)).toBe(countDirnameCalls(withoutDot));
7474
expect(countDirnameCalls(withDot)).toBe(3);
7575
});
76+
77+
it.skipIf(process.platform === 'win32')(
78+
'should add Vite+ managed bin to PATH before running user hook',
79+
() => {
80+
const tmp = mkdtempSync(join(tmpdir(), 'hooks-path-test-'));
81+
try {
82+
const hooksDir = join(tmp, '.vite-hooks');
83+
const internalHooksDir = join(hooksDir, '_');
84+
const nodeModulesBin = join(tmp, 'node_modules', '.bin');
85+
const vpHomeBin = join(tmp, 'vp-home', 'bin');
86+
const systemBin = join(tmp, 'system-bin');
87+
88+
mkdirSync(internalHooksDir, { recursive: true });
89+
mkdirSync(nodeModulesBin, { recursive: true });
90+
mkdirSync(vpHomeBin, { recursive: true });
91+
mkdirSync(systemBin, { recursive: true });
92+
93+
writeFileSync(join(internalHooksDir, 'h'), hookScript('.vite-hooks'), { mode: 0o755 });
94+
writeFileSync(
95+
join(internalHooksDir, 'pre-commit'),
96+
'#!/usr/bin/env sh\n. "$(dirname "$0")/h"',
97+
{ mode: 0o755 },
98+
);
99+
writeFileSync(join(hooksDir, 'pre-commit'), 'vp staged\n');
100+
101+
writeFileSync(
102+
join(nodeModulesBin, 'vp'),
103+
'#!/bin/sh\nbasedir=$(dirname "$0")\nexec node "$basedir/../vite-plus/bin/vp" "$@"\n',
104+
{ mode: 0o755 },
105+
);
106+
writeFileSync(
107+
join(vpHomeBin, 'node'),
108+
'#!/bin/sh\necho "fake-node $*" > "$VP_HOME/node-used"\n',
109+
{ mode: 0o755 },
110+
);
111+
112+
writeFileSync(join(systemBin, 'sh'), '#!/bin/sh\nexec /bin/sh "$@"\n', {
113+
mode: 0o755,
114+
});
115+
writeFileSync(join(systemBin, 'dirname'), '#!/bin/sh\nexec /usr/bin/dirname "$@"\n', {
116+
mode: 0o755,
117+
});
118+
writeFileSync(join(systemBin, 'basename'), '#!/bin/sh\nexec /usr/bin/basename "$@"\n', {
119+
mode: 0o755,
120+
});
121+
122+
execSync('sh .vite-hooks/_/pre-commit', {
123+
cwd: tmp,
124+
env: {
125+
HOME: join(tmp, 'home'),
126+
PATH: systemBin,
127+
VP_HOME: join(tmp, 'vp-home'),
128+
},
129+
});
130+
131+
expect(existsSync(join(tmp, 'vp-home', 'node-used'))).toBe(true);
132+
} finally {
133+
rmSync(tmp, { recursive: true, force: true });
134+
}
135+
},
136+
);
76137
});

packages/cli/src/config/hooks.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ i="\${XDG_CONFIG_HOME:-$HOME/.config}/vite-plus/hooks-init.sh"
4949
5050
{ [ "\${HUSKY-}" = "0" ] || [ "\${VITE_GIT_HOOKS-}" = "0" ]; } && exit 0
5151
52+
if [ -n "\${VP_HOME-}" ]; then
53+
__vp_bin="$VP_HOME/bin"
54+
elif [ -n "\${HOME-}" ]; then
55+
__vp_bin="$HOME/.vite-plus/bin"
56+
else
57+
__vp_bin=""
58+
fi
59+
[ -n "$__vp_bin" ] && [ -d "$__vp_bin" ] && export PATH="$__vp_bin:$PATH"
60+
5261
d=${rootExpr}
5362
export PATH="$d/node_modules/.bin:$PATH"
5463
sh -e "$s" "$@"

0 commit comments

Comments
 (0)