/
getScript.ts
144 lines (119 loc) · 3.21 KB
/
getScript.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import path from 'path'
import slash from 'slash'
import { readPkg } from '../read-pkg'
interface Context {
createdAt: string
huskyHomepage?: string
huskyVersion?: string
pkgDirectory?: string
pkgHomepage?: string
pmName: string
relativeUserPkgDir: string
}
// Used to identify scripts created by Husky
export const huskyIdentifier = '# husky'
// Experimental
const huskyrc = '.huskyrc'
// Render script
const render = ({
createdAt,
huskyHomepage,
huskyVersion,
pkgDirectory,
pkgHomepage,
pmName,
relativeUserPkgDir
}: Context): string => `#!/bin/sh
${huskyIdentifier}
# Hook created by Husky v${huskyVersion} (${huskyHomepage})
# At: ${createdAt}
# From: ${pkgDirectory} (${pkgHomepage})
# With: ${pmName}
gitRoot="$(git rev-parse --show-toplevel)"
gitParams="$*"
hookName=\`basename "$0"\`
packageManager=${pmName}
debug() {
if [ "$HUSKY_DEBUG" = "true" ] || [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky:debug $1"
fi
}
command_exists () {
command -v "$1" >/dev/null 2>&1
}
run_command () {
if command_exists "$1"; then
"$@" husky-run $hookName "$gitParams"
exitCode="$?"
debug "$* husky-run exited with $exitCode exit code"
if [ $exitCode -eq 127 ]; then
echo "Can't find Husky, skipping $hookName hook"
echo "You can reinstall it using 'npm install husky --save-dev' or delete this hook"
else
exit $exitCode
fi
else
echo "Can't find $1 in PATH: $PATH"
echo "Skipping $hookName hook"
exit 0
fi
}
debug "husky v${huskyVersion} (created at ${createdAt})"
debug "$hookName hook started"
debug "Current working directory is \`pwd\`"
if [ -f ~/${huskyrc} ]; then
debug "source ~/${huskyrc}"
. ~/${huskyrc}
fi
if [ -f "$gitRoot"/${huskyrc}.local ]; then
debug "source $gitRoot/${huskyrc}.local"
. "$gitRoot"/${huskyrc}.local
fi
debug "$hookName hook started"
if [ "$HUSKY_SKIP_HOOKS" = "true" ] || [ "$HUSKY_SKIP_HOOKS" = "1" ]; then
debug "HUSKY_SKIP_HOOKS is set to $HUSKY_SKIP_HOOKS, skipping hook"
exit 0
fi
cd "${relativeUserPkgDir}"
case $packageManager in
"npm") run_command npx --no-install;;
"pnpm") run_command pnpx --no-install;;
"yarn") run_command yarn run --silent;;
"*") echo "Unknow package manager: $packageManager"; exit 0;;
esac
`
/**
* @param {string} relativeUserPkgDir - relative path from git dir to dir containing user package.json
* @param {string} packageManager - e.g. npm, pnpm or yarn
* @returns {string} script
*/
export default function({
relativeUserPkgDir,
pmName
}: {
relativeUserPkgDir: string
pmName: string
}): string {
const pkgHomepage = process.env.npm_package_homepage
const pkgDirectory = process.env.PWD
const { homepage: huskyHomepage, version: huskyVersion } = readPkg(
path.join(__dirname, '../..')
)
const createdAt = new Date().toLocaleString()
if (!['npm', 'pnpm', 'yarn'].includes(pmName)) {
throw new Error(
`Unknown package manager: ${pmName} (npm_config_user_agent: ${process.env.npm_config_user_agent})`
)
}
const normalizedPath = slash(relativeUserPkgDir)
// Render script
return render({
createdAt,
huskyHomepage,
huskyVersion,
relativeUserPkgDir: normalizedPath,
pkgDirectory,
pkgHomepage,
pmName
})
}