Skip to content
Merged
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
41 changes: 22 additions & 19 deletions packages/rstack/src/setup/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Keep this list aligned with the client-side hooks generated by Husky v9.
// `pre-auto-gc` is included even though it is not listed in Husky's documentation.
const hookNames = [
export const hookNames: string[] = [
'pre-commit',
'pre-merge-commit',
'prepare-commit-msg',
Expand Down Expand Up @@ -30,43 +30,46 @@ const quoteShellPath = (value: string): string => {
return `'${shellPath.replaceAll("'", `'"'"'`)}'`;
};

// Generated shims live in `<hooks-directory>/_`. When a shim sources this
// dispatcher, `$0` still points to the shim, so the user hook is one level up.
const createDispatcher = (nodeExecutable: string): string => `#!/usr/bin/env sh
# Generated by Rstack. Do not edit.

name=$(basename "$0")
dir=$(dirname "$(dirname "$0")")
hook="$dir/$name"
rs_name=\${0##*/}
rs_root=$PWD
rs_hook="\${rs_dir%/*}/$rs_name"
[ -f "$rs_hook" ] || exit 0

[ -f "$hook" ] || exit 0

init="\${XDG_CONFIG_HOME:-$HOME/.config}/rstack/hooks-init.sh"
[ -f "$init" ] && . "$init"
rs_init="\${XDG_CONFIG_HOME:-$HOME/.config}/rstack/hooks-init.sh"
[ -f "$rs_init" ] && . "$rs_init"

[ "\${RSTACK_HOOKS-}" = "0" ] && exit 0
[ "\${RSTACK_HOOKS-}" = "2" ] && set -x

IFS= read -r rs_project_path < "$rs_dir/.owner" || exit 1
[ -n "$rs_project_path" ] || exit 1

# Fall back to the Node.js executable that ran rs setup when GUI clients omit
# it from PATH. Keep an existing Node.js environment ahead of this fallback.
node_fallback=${quoteShellPath(nodeExecutable)}
if ! command -v node >/dev/null 2>&1 && [ -x "$node_fallback" ]; then
PATH="\${PATH:+$PATH:}\${node_fallback%/*}"
rs_node_fallback=${quoteShellPath(nodeExecutable)}
if ! command -v node >/dev/null 2>&1 && [ -x "$rs_node_fallback" ]; then
PATH="\${PATH:+$PATH:}\${rs_node_fallback%/*}"
fi

cd "$rs_root/$rs_project_path" || exit 1
Comment thread
chenjiahan marked this conversation as resolved.
Comment thread
chenjiahan marked this conversation as resolved.
export PATH="node_modules/.bin\${PATH:+:$PATH}"

code=0
sh -e "$hook" "$@" || code=$?
rs_code=0
sh -e "$rs_hook" "$@" || rs_code=$?

[ "$code" = "0" ] || echo "Rstack - $name hook failed (code $code)"
[ "$code" = "127" ] && echo "Rstack - command not found in PATH=$PATH"
exit "$code"
[ "$rs_code" = "0" ] || echo "Rstack - $rs_name hook failed (code $rs_code)"
[ "$rs_code" = "127" ] && echo "Rstack - command not found in PATH=$PATH"
exit "$rs_code"
`;

// Every generated Git hook sources the same dispatcher to keep runtime behavior
// consistent and make future initialization changes local to one file.
const shim = `#!/usr/bin/env sh
. "$(dirname "$0")/runner"
rs_dir=$(CDPATH= cd "$(dirname "$0")" && pwd) || exit 1
. "$rs_dir/runner"
`;

export const createHookFiles = (
Expand Down
7 changes: 6 additions & 1 deletion packages/rstack/src/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ${color.yellow(' $ rs setup [options]')}
Install Git hooks in the current repository.

${color.cyan('Options')}:
--hooks-dir <path> Specify hooks directory relative to the current directory
--hooks-dir <path> Specify hooks directory relative to the Git repository root
-h, --help Display this help message`;

export const runSetupCLI = (args: string[]): void => {
Expand Down Expand Up @@ -43,6 +43,11 @@ export const runSetupCLI = (args: string[]): void => {
}

if (result.status === 'skipped') {
if (result.message) {
logger.warn(`Git hooks setup skipped: ${color.yellow(result.message)}.`);
return;
}

const reason =
result.reason === 'disabled' ? 'disabled by RSTACK_HOOKS' : 'not a Git repository';
logger.info(`Git hooks setup skipped: ${color.yellow(reason)}.`);
Expand Down
Loading