This repository has been archived by the owner on Sep 5, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.go
358 lines (308 loc) · 9.2 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//go:generate go install github.com/kevinburke/go-bindata/go-bindata
//go:generate go-bindata -pkg bindata -o app/bindata/bindata.go nodejs-portable.conf
//go:generate go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo
//go:generate goversioninfo -icon=res/app.ico
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/akyoto/color"
"github.com/crazy-max/nodejs-portable/app/app"
"github.com/crazy-max/nodejs-portable/app/fs"
"github.com/crazy-max/nodejs-portable/app/log"
"github.com/crazy-max/nodejs-portable/app/menu"
"github.com/crazy-max/nodejs-portable/app/nodejs"
"github.com/crazy-max/nodejs-portable/app/pathu"
"github.com/crazy-max/nodejs-portable/app/util"
goversion "github.com/mcuadros/go-version"
)
var (
version = "dev"
module = "github.com/crazy-max/nodejs-portable"
)
func init() {
util.SetConsoleTitle(fmt.Sprintf("Node.js Portable %s", version))
log.Logger.Info("--------")
log.Logger.Infof("Starting Node.js Portable %s...", version)
log.Logger.Infof("Current path: %s", pathu.CurrentPath)
}
func main() {
// pass args directly to node.exe and exit
if len(os.Args[1:]) > 0 {
node(os.Args[1:]...)
return
}
color.New(color.FgHiWhite).Println("Node.js Portable " + version)
color.New(color.FgHiWhite).Println("https://" + module)
// check for update
latestVersion, err := util.GetLatestVersion()
if err != nil {
log.Logger.Error("Cannot contact the update server:", err.Error())
color.New(color.FgYellow).Printf("\nCan't contact the update server: %s\n", err.Error())
} else if goversion.Compare(version, latestVersion, "<") {
log.Logger.Info("New release available:", latestVersion)
color.New(color.FgHiGreen).Print("\nA new release is available : ")
color.New(color.FgHiGreen, color.Bold).Print(latestVersion)
color.New(color.FgHiGreen).Print("\nDownload : ")
color.New(color.FgHiGreen, color.Bold).Print("https://" + module + "/releases/latest\n")
}
// open shell on immediate mode
if app.Conf.ImmediateMode == true {
shell()
return
}
// build menu
menuCommands := []menu.CommandOption{
{
Description: "Install",
Function: install,
},
{
Description: "Shell",
Function: shell,
},
}
menuOptions := menu.NewOptions("Menu", "'menu' for help> ", 0, "")
menuN := menu.NewMenu(menuCommands, menuOptions)
menuN.Start()
}
func install(args ...string) error {
log.Logger.Info(">> INSTALL")
fmt.Println()
// check if already installed
if _, err := fs.Stat(fs.Join(pathu.AppPath, "node.exe")); err == nil {
util.PrintErrorStr("Node.js is already installed...")
return nil
}
// create tmp folder
os.MkdirAll(pathu.TmpPath, 777)
// seek latest node.js version
latestNodejs, err := nodejs.GetLatestVersion()
if err != nil {
util.PrintErrorStr(fmt.Sprintf("Seeking latest Node.js version: %s...", err.Error()))
return nil
}
// input version
nodejsVersion := util.ReadLine(fmt.Sprintf(" Version (default %s): ", latestNodejs))
if nodejsVersion == "" {
nodejsVersion = latestNodejs
}
log.Logger.Info("nodejsVersion:", nodejsVersion)
// input arch
nodejsArch := util.ReadLine(" Architecture (default x86): ")
if nodejsArch == "" {
nodejsArch = "x86"
}
log.Logger.Info("nodejsArch:", nodejsArch)
// check dist
fmt.Println()
util.Print(fmt.Sprintf("Checking Node.js version %s... ", nodejsVersion))
distURL, distFilename, err := nodejs.GetDistURL(nodejsVersion, nodejsArch)
if err != nil {
util.PrintError(err)
return nil
}
util.PrintOk()
// download dist
util.Print(fmt.Sprintf("Downloading %s...", distURL))
distPath := fs.Join(pathu.TmpPath, distFilename)
if err := util.DownloadFile(pathu.TmpPath, distURL); err != nil {
fmt.Print(" ")
util.PrintError(err)
return nil
}
fmt.Print(" ")
util.PrintOk()
// extract dist
util.Print(fmt.Sprintf("Extracting %s... ", distFilename))
extractPath, err := nodejs.ExtractDist(distPath)
if err != nil {
util.PrintError(err)
return nil
}
util.PrintOk()
// move nodejs folder
util.Print("Moving nodejs folder... ")
err = fs.CopyDir(extractPath, pathu.AppPath)
if err != nil {
util.PrintError(err)
return nil
}
util.PrintOk()
// create config
util.Print("Creating Node.js configuration... ")
if err := nodejs.CreateConfig(); err != nil {
util.PrintError(err)
return nil
}
util.PrintOk()
return nil
}
func shell(args ...string) error {
log.Logger.Info(">> SHELL")
fmt.Println()
// check if installed
util.Print("Checking if Node.js installed... ")
if _, err := fs.Stat(path.Join(pathu.AppPath, "node.exe")); err != nil {
util.PrintErrorStr("Not installed...")
return nil
}
util.PrintOk()
// create tmp folder
fs.CreateSubfolder(pathu.TmpPath)
// create config
util.Print("Creating Node.js configuration... ")
if err := nodejs.CreateConfig(); err != nil {
util.PrintError(err)
return nil
}
util.PrintOk()
// check custom paths
customPaths := ""
util.Println("Checking custom paths...")
for _, customPath := range app.Conf.CustomPaths {
if customPath == "" {
continue
}
tmpCustomPath, _ := filepath.Abs(customPath)
util.Print(fmt.Sprintf("# %s ", tmpCustomPath))
if _, err := os.Stat(tmpCustomPath); err == nil {
if customPaths != "" {
customPaths = customPaths + ";"
}
customPaths = customPaths + strings.TrimRight(fs.FormatWinPath(tmpCustomPath), `\`)
util.PrintOk()
} else {
util.PrintWarningStr("Not found...")
}
}
// add custom paths
if customPaths != "" {
util.Print("Adding customs path to PATH... ")
os.Setenv("PATH", fmt.Sprintf("%s;%s", customPaths, os.Getenv("PATH")))
util.PrintOk()
}
// add Node to path
util.Print("Adding node to PATH... ")
if err := os.Setenv("PATH", fmt.Sprintf("%s;%s", fs.RemoveUnc(pathu.AppPath), os.Getenv("PATH"))); err != nil {
util.PrintError(err)
}
util.PrintOk()
// set NODE_PATH
util.Print("Setting NODE_PATH... ")
if err := os.Setenv("NODE_PATH", fs.RemoveUnc(fs.FormatWinPath(path.Join(pathu.AppPath, "node_modules")))); err != nil {
util.PrintError(err)
}
util.PrintOk()
// create launch script
util.Print("Creating launch script... ")
launchScript := fs.Join(pathu.TmpPath, "launch.bat")
if err := util.CreateFile(launchScript, app.GetLaunchScriptContent()); err != nil {
util.PrintError(err)
return nil
}
util.PrintOk()
// check shell
var shellProc string
var shellCmdLine string
if app.Conf.Shell == "powershell" {
shellProc, _ = exec.LookPath("powershell.exe")
shellCmdLine = fmt.Sprintf("-? -NoExit -Command %s", fs.RemoveUnc(launchScript))
} else {
shellProc = os.Getenv("COMSPEC")
shellCmdLine = fmt.Sprintf(` /k "%s"`, fs.RemoveUnc(launchScript))
}
if app.Conf.ImmediateMode == false {
// wait user input to open the shell
fmt.Print("\nPress Enter to open the shell...")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
// clear screen
util.Println("Clearing screen...")
var clear *exec.Cmd
if app.Conf.Shell == "powershell" {
clear = exec.Command("powershell", "Clear-Host;")
} else {
clear = exec.Command("cmd", "/c", "cls")
}
clear.Stdout = os.Stdout
clear.Run()
// transfer stdin, stdout, and stderr to the new process
// and also set target directory for the shell to start in.
pa := os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
Dir: fs.RemoveUnc(app.Conf.WorkPath),
Sys: &syscall.SysProcAttr{
CmdLine: shellCmdLine,
},
}
// start up a new shell.
log.Logger.Info("Starting up the shell... ")
proc, err := os.StartProcess(shellProc, []string{}, &pa)
if err != nil {
util.PrintError(err)
return nil
}
// wait until user exits the shell
if _, err = proc.Wait(); err != nil {
util.PrintError(err)
return nil
}
return nil
}
func node(args ...string) {
log.Logger.Info(">> NODE")
// check if installed
if _, err := fs.Stat(path.Join(pathu.AppPath, "node.exe")); err != nil {
log.Logger.Error("node.exe not found...")
return
}
// create tmp folder
fs.CreateSubfolder(pathu.TmpPath)
// create config
if err := nodejs.CreateConfig(); err != nil {
log.Logger.Error(err.Error())
return
}
// check custom paths
customPaths := ""
for _, customPath := range app.Conf.CustomPaths {
if customPath == "" {
continue
}
tmpCustomPath, _ := filepath.Abs(customPath)
if _, err := os.Stat(tmpCustomPath); err == nil {
if customPaths != "" {
customPaths = customPaths + ";"
}
customPaths = customPaths + strings.TrimRight(fs.FormatWinPath(tmpCustomPath), `\`)
}
}
// add custom paths
if customPaths != "" {
os.Setenv("PATH", fmt.Sprintf("%s;%s", customPaths, os.Getenv("PATH")))
}
// add Node to path
if err := os.Setenv("PATH", fmt.Sprintf("%s;%s", fs.RemoveUnc(pathu.AppPath), os.Getenv("PATH"))); err != nil {
log.Logger.Error(err.Error())
}
// set NODE_PATH
if err := os.Setenv("NODE_PATH", fs.RemoveUnc(fs.FormatWinPath(path.Join(pathu.AppPath, "node_modules")))); err != nil {
log.Logger.Error(err.Error())
}
execute := exec.Command(fs.RemoveUnc(path.Join(pathu.AppPath, "node.exe")), args...)
execute.Stdout = os.Stdout
execute.Stderr = os.Stderr
log.Logger.Infof("Exec %s %s ", fs.RemoveUnc(fs.FormatWinPath(path.Join(pathu.AppPath, "node.exe"))), strings.Join(args, " "))
if err := execute.Start(); err != nil {
log.Logger.Fatalf("Command failed: %v", err)
}
execute.Wait()
}