This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeinstall.go
131 lines (108 loc) · 3.18 KB
/
nodeinstall.go
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
// This file is part of the ego-cli distribution.
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/)
//
// ego-cli is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3.
//
// ego-cli is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/briandowns/spinner"
"github.com/thatisuday/commando"
utils "github.com/egomobile/ego-cli/utils"
)
func nodeinstall_run(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
audit, err := flags["audit"].GetBool()
runAuditFix := err == nil && audit
update, err := flags["update"].GetBool()
runUpdate := err == nil && update
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
var package_json = filepath.Join(cwd, "/package.json")
if _, err := os.Stat(package_json); os.IsNotExist(err) {
log.Fatal("No package.json found")
}
var node_modules = filepath.Join(cwd, "/node_modules")
// remove node_modules?
if _, err := os.Stat(node_modules); !os.IsNotExist(err) {
utils.WithSpinner("Removing node_modules/ folder ...", func(s *spinner.Spinner) {
err = os.RemoveAll(node_modules)
if err != nil {
log.Fatal(err)
}
})
fmt.Println("")
}
// execute npm install
utils.WithSpinner("Executing npm install ...", func(s *spinner.Spinner) {
cmd := exec.Command("npm", "install")
cmd.Dir = cwd
err := cmd.Run()
if err != nil {
output, err2 := cmd.Output()
if err2 != nil {
log.Fatal(err)
} else {
log.Fatal(string(output))
}
}
fmt.Println("")
})
if runUpdate {
// run npm update
utils.WithSpinner("Executing npm update ...", func(s *spinner.Spinner) {
cmd := exec.Command("npm", "update")
cmd.Dir = cwd
err := cmd.Run()
if err != nil {
output, err2 := cmd.Output()
if err2 != nil {
log.Fatal(err)
} else {
log.Fatal(string(output))
}
}
fmt.Println("")
})
}
if runAuditFix {
// run npm audit fix
utils.WithSpinner("Executing npm audit fix ...", func(s *spinner.Spinner) {
cmd := exec.Command("npm", "audit", "fix")
cmd.Dir = cwd
err := cmd.Run()
if err != nil {
output, err2 := cmd.Output()
if err2 != nil {
log.Fatal(err)
} else {
log.Fatal(string(output))
}
}
fmt.Println("")
})
}
}
func Setup_nodeinstall_Command() {
commando.
Register("node-install").
SetShortDescription("runs \"npm install\"").
SetDescription("Deletes \"node_modules\" and executes \"npm install\" with optional \"npm update\" and \"npm audit fix\"").
AddFlag("audit,a", "run \"audit fix\" after \"install\" or \"update\"", commando.Bool, nil).
AddFlag("update,u", "run \"update\" after \"install\"", commando.Bool, nil).
SetAction(nodeinstall_run)
}