-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
124 lines (104 loc) · 3.67 KB
/
build.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
package npmstart
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
libnodejs "github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/libreload-packit"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
func Build(logger scribe.Emitter, reloader Reloader) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
projectPath, err := libnodejs.FindProjectPath(context.WorkingDir)
if err != nil {
return packit.BuildResult{}, err
}
pkg, err := libnodejs.ParsePackageJSON(projectPath)
if err != nil {
return packit.BuildResult{}, err
}
command := "sh"
arg := pkg.Scripts.Start
if pkg.Scripts.PreStart != "" {
arg = fmt.Sprintf("%s && %s", pkg.Scripts.PreStart, arg)
}
if pkg.Scripts.PostStart != "" {
arg = fmt.Sprintf("%s && %s", arg, pkg.Scripts.PostStart)
}
// Ideally we would like the lifecycle to support setting a custom working
// directory to run the launch process. Until that happens we will cd in.
if projectPath != context.WorkingDir {
arg = fmt.Sprintf("cd %s && %s", projectPath, arg)
}
// ubuntu uses dash as the default shell while ubi uses bash as the default shell
// The version of bash on the current ubi images does not work properly with the
// signal handling added in the script. Running with bash -c and escaping quotes in
// the command changes the behavior so that it matches that when running with dash
// This is fixed in more recent versions of bash ( 5.x and greater) but it will be some
// time before ubi (and ubuntu it seems) will use that new a version of bash. This work
// around is needed until then.
etcOsReleaseFileContent, err := os.ReadFile(filepath.Join("/etc/os-release"))
if err == nil {
re := regexp.MustCompile(`ID=(rhel|"rhel")`)
match := re.FindStringSubmatch(string(etcOsReleaseFileContent))
if match != nil {
arg = fmt.Sprintf("bash -c \"%s\"", strings.Replace(arg, `"`, `\"`, -1))
}
}
script, err := createStartupScript(fmt.Sprintf(StartupScript, arg), projectPath, context.WorkingDir)
if err != nil {
return packit.BuildResult{}, err
}
args := []string{script}
originalProcess := packit.Process{
Type: "web",
Command: command,
Args: args,
Default: true,
Direct: true,
}
var processes []packit.Process
if shouldEnableReload, err := reloader.ShouldEnableLiveReload(); err != nil {
return packit.BuildResult{}, err
} else if shouldEnableReload {
nonReloadableProcess, reloadableProcess := reloader.TransformReloadableProcesses(originalProcess, libreload.ReloadableProcessSpec{
WatchPaths: []string{projectPath},
IgnorePaths: []string{
filepath.Join(projectPath, "package.json"),
filepath.Join(projectPath, "package-lock.json"),
filepath.Join(projectPath, "node_modules"),
},
})
nonReloadableProcess.Type = "no-reload"
reloadableProcess.Type = "web"
processes = append(processes, reloadableProcess, nonReloadableProcess)
} else {
processes = append(processes, originalProcess)
}
logger.LaunchProcesses(processes)
return packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{},
},
Launch: packit.LaunchMetadata{
Processes: processes,
},
}, nil
}
}
func createStartupScript(script, projectPath, workingDir string) (string, error) {
targetDir := workingDir
if projectPath != workingDir {
targetDir = projectPath
}
path := filepath.Join(targetDir, "start.sh")
err := os.WriteFile(path, []byte(script), 0644)
if err != nil {
return "", err
}
return path, nil
}