-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
getScript.ts
142 lines (119 loc) · 3.22 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
import fs from 'fs'
import path from 'path'
import slash from 'slash'
interface Context {
createdAt: string
homepage: string
packageManager: string
pathToUserPkgDir: string
pkgDirectory?: string
pkgHomepage?: string
version: string
}
// Used to identify scripts created by Husky
export const huskyIdentifier = '# husky'
// Experimental
const huskyrc = '.huskyrc'
// Render script
const render = ({
createdAt,
homepage,
packageManager,
pathToUserPkgDir,
pkgDirectory,
pkgHomepage,
version
}: Context): string => `#!/bin/sh
${huskyIdentifier}
# Hook created by Husky v${version} (${homepage})
# At: ${createdAt}
# From: ${pkgDirectory} (${pkgHomepage})
# With: ${packageManager}
gitRoot="$(git rev-parse --show-toplevel)"
gitParams="$*"
hookName=\`basename "$0"\`
packageManager=${packageManager}
debug() {
if [ "$HUSKY_DEBUG" = "true" ] || [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky:debug $1"
fi
}
command_exists () {
command -v "$1" >/dev/null 2>&1
}
debug "husky v${version} (created at ${createdAt})"
debug "$hookName hook started"
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
}
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 "${pathToUserPkgDir}"
case $packageManager in
"npm") run_command npx --no-install;;
"pnpm") run_command pnpx --no-install;;
"yarn") run_command yarn;;
"*") echo "Unknow package manager: $packageManager"; exit 0;;
esac
`
/**
* @param {string} pathToUserPkgDir - relative path from git dir to dir containing package.json
* @param {string} packageManager - e.g. npm, pnpm or yarn
* @returns {string} script
*/
export default function(
pathToUserPkgDir: string,
packageManager: string
): string {
// Env variable
const pkgHomepage = process && process.env && process.env.npm_package_homepage
const pkgDirectory = process && process.env && process.env.PWD
// Husky package.json
const { homepage, version } = JSON.parse(
fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf-8')
)
// Created at
const createdAt = new Date().toLocaleString()
// Script runner command
if (!['npm', 'pnpm', 'yarn'].includes(packageManager)) {
throw new Error(
`Unknown package manager: ${packageManager} (npm_config_user_agent: ${process.env.npm_config_user_agent})`
)
}
// Render script
return render({
createdAt,
homepage,
packageManager,
// Normalize path
pathToUserPkgDir: slash(pathToUserPkgDir),
pkgDirectory,
pkgHomepage,
version
})
}