-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
109 lines (93 loc) · 2.45 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
package yarnstart
import (
"fmt"
"path/filepath"
"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
func Build(logger scribe.Emitter) 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 := "node"
arg := fmt.Sprintf("node %s", filepath.Join(context.WorkingDir, "server.js"))
if pkg.Scripts.Start != "" {
command = "bash"
arg = pkg.Scripts.Start
}
if pkg.Scripts.PreStart != "" {
command = "bash"
arg = fmt.Sprintf("%s && %s", pkg.Scripts.PreStart, arg)
}
if pkg.Scripts.PostStart != "" {
command = "bash"
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 {
command = "bash"
arg = fmt.Sprintf("cd %s && %s", projectPath, arg)
}
args := []string{arg}
switch command {
case "bash":
args = []string{"-c", arg}
case "node":
args = []string{filepath.Join(context.WorkingDir, "server.js")}
}
processes := []packit.Process{
{
Type: "web",
Command: command,
Args: args,
Default: true,
Direct: true,
},
}
shouldReload, err := checkLiveReloadEnabled()
if err != nil {
return packit.BuildResult{}, err
}
if shouldReload {
processes = []packit.Process{
{
Type: "web",
Command: "watchexec",
Args: append([]string{
"--restart",
"--shell", "none",
"--watch", projectPath,
"--ignore", filepath.Join(projectPath, "package.json"),
"--ignore", filepath.Join(projectPath, "yarn.lock"),
"--ignore", filepath.Join(projectPath, "node_modules"),
"--",
command,
}, args...),
Default: true,
Direct: true,
},
{
Type: "no-reload",
Command: command,
Args: args,
Direct: true,
},
}
}
logger.LaunchProcesses(processes)
return packit.BuildResult{
Launch: packit.LaunchMetadata{
Processes: processes,
},
}, nil
}
}