-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgit-hook-auto-npm-install.js
49 lines (48 loc) · 1.58 KB
/
git-hook-auto-npm-install.js
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
var cp = require("child_process")
var params = (process.env.GIT_PARAMS || "").split(" ")
if (params.length < 2) {
// it's a merge
var res = cp.execSync("git diff --name-only HEAD@{1} HEAD").toString()
cp.execSync("git diff -U0 HEAD@{1} HEAD package.json", { stdio: [0, 1, 2] })
cp.execSync("git diff -U0 HEAD@{1} HEAD frontend/package.json", {
stdio: [0, 1, 2],
})
cp.execSync("git diff -U0 HEAD@{1} HEAD backend/package.json", {
stdio: [0, 1, 2],
})
parseResult(res)
} else {
var res = cp
.execSync("git diff --name-only " + params[0] + " " + params[1])
.toString()
cp.execSync("git diff -U0 " + params[0] + " " + params[1] + " package.json", {
stdio: [0, 1, 2],
})
cp.execSync(
"git diff -U0 " + params[0] + " " + params[1] + " frontend/package.json",
{ stdio: [0, 1, 2] },
)
cp.execSync(
"git diff -U0 " + params[0] + " " + params[1] + " backend/package.json",
{ stdio: [0, 1, 2] },
)
parseResult(res)
}
function parseResult(res) {
if (res.match(/frontend\/package/)) {
console.log(
"frontend/package.json has been changed. Installing dependencies",
)
cp.execSync("npm i", { cwd: __dirname + "/frontend", stdio: [0, 1, 2] })
}
if (res.match(/backend\/package/)) {
console.log(
"backend/package.json has been changed. Installing dependencies",
)
cp.execSync("npm i", { cwd: __dirname + "/backend", stdio: [0, 1, 2] })
}
if (res.match(/(^|\n|\r)package/)) {
console.log("package.json has been changed. Installing dependencies")
cp.execSync("npm i", { cwd: __dirname + "/", stdio: [0, 1, 2] })
}
}