-
Notifications
You must be signed in to change notification settings - Fork 4
/
typescript_node_dockerfile.go
71 lines (54 loc) · 1.47 KB
/
typescript_node_dockerfile.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
package dockerfile
import (
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/FelipeMCassiano/gorvus/internal/builders"
"github.com/jedib0t/go-pretty/v6/text"
)
func TypescriptDockerFileBuilder(input DockerfileData) error {
if len(input.EntryFile) == 0 {
eF, err := setEntryfile()
if err != nil {
os.Exit(1)
}
input.EntryFile = eF
}
if strings.Contains(input.EntryFile, ".js") {
fmt.Println(text.FgYellow.Sprint("> Only allowed .ts files"))
os.Exit(1)
}
if len(input.ProjectName) >= 1 {
fmt.Println(text.FgYellow.Sprintf("This language doens't needs to specify the Project Name"))
os.Exit(1)
}
cmd := exec.Command("node", "-v")
nodeVersionOutput, err := cmd.Output()
if err != nil {
return err
}
versionPattern := `v(\d+\.\d+\.\d+)`
versionRegex := regexp.MustCompile(versionPattern)
matches := versionRegex.FindStringSubmatch(string(nodeVersionOutput))
if len(matches) < 2 {
return fmt.Errorf("failed to extract Node version number")
}
nodeVersion := matches[1]
datafile, err := templatesContent.ReadFile("templates/typescript_node_dockerfile.tmpl")
if err != nil {
return err
}
input.EntryFile = strings.TrimSuffix(input.EntryFile, filepath.Ext(input.EntryFile))
input.Version = nodeVersion
file, err := os.Create("Dockerfile")
if err != nil {
return fmt.Errorf("failed to creating Dockerfile: %s", err.Error())
}
defer file.Close()
builders.ApplyTemplate(file, string(datafile), input)
return nil
}