This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodejs.go
99 lines (78 loc) · 2.47 KB
/
nodejs.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
package templates
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/abdfnx/botway/constants"
"github.com/abdfnx/looker"
)
func isTypescript(platform string, isTs bool) (string, string, string) {
if isTs {
return "nodejs-ts", "ts", MainTSContent(platform)
} else {
return "nodejs", "js", MainJSContent(platform)
}
}
func MainJSContent(platform string) string {
return Content("main.js", platform+"-nodejs", "", "")
}
func MainTSContent(platform string) string {
return Content("main.ts", platform+"-nodejs-ts", "", "")
}
func NodejsTemplate(botName, pm, platform string, isTs bool) {
_, nerr := looker.LookPath("npm")
pmPath, err := looker.LookPath(pm)
if err != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" " + pm + " is not installed"))
} else {
if nerr != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" npm is not installed"))
} else {
tmpName, ext, content := isTypescript(platform, isTs)
mainFile := os.WriteFile(filepath.Join(botName, "src", "main."+ext), []byte(content), 0644)
dockerFile := os.WriteFile(filepath.Join(botName, "Dockerfile"), []byte(DockerfileContent(botName, pm+".dockerfile", platform)), 0644)
resourcesFile := os.WriteFile(filepath.Join(botName, "resources.md"), []byte(Resources(platform, "nodejs.md")), 0644)
packageFile := os.WriteFile(filepath.Join(botName, "package.json"), []byte(Content("package.json", platform+"-"+tmpName, "", "")), 0644)
if mainFile != nil {
log.Fatal(mainFile)
}
if resourcesFile != nil {
log.Fatal(resourcesFile)
}
if dockerFile != nil {
log.Fatal(dockerFile)
}
if packageFile != nil {
log.Fatal(packageFile)
}
if isTs {
tsConfigFile := os.WriteFile(filepath.Join(botName, "tsconfig.json"), []byte(Content("tsconfig.json", platform+"-nodejs-ts", "", "")), 0644)
if tsConfigFile != nil {
log.Fatal(tsConfigFile)
}
}
pmInstall := pmPath + " i"
if pm == "yarn" {
pmInstall = pmPath
}
installCmd := exec.Command("bash", "-c", pmInstall)
if runtime.GOOS == "windows" {
installCmd = exec.Command("powershell.exe", pmInstall)
}
installCmd.Dir = botName
installCmd.Stdin = os.Stdin
installCmd.Stdout = os.Stdout
installCmd.Stderr = os.Stderr
err = installCmd.Run()
if err != nil {
log.Printf("error: %v\n", err)
}
CheckProject(botName, platform)
}
}
}