-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdetect.go
97 lines (84 loc) · 2.36 KB
/
detect.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
package yarnstart
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
// NoStartScriptError indicates that the targeted project does no have a start command in their package.json
const NoStartScriptError = "no start script in package.json"
func Detect() packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
projectPath, err := libnodejs.FindProjectPath(context.WorkingDir)
if err != nil {
return packit.DetectResult{}, err
}
exists, err := fs.Exists(filepath.Join(projectPath, "yarn.lock"))
if err != nil {
return packit.DetectResult{}, fmt.Errorf("failed to stat yarn.lock: %w", err)
}
if !exists {
return packit.DetectResult{}, packit.Fail.WithMessage("no 'yarn.lock' found in the project path %s", projectPath)
}
pkg, err := libnodejs.ParsePackageJSON(projectPath)
if err != nil {
if os.IsNotExist(err) {
return packit.DetectResult{}, packit.Fail.WithMessage("no 'package.json' found in project path %s", projectPath)
}
return packit.DetectResult{}, fmt.Errorf("failed to open package.json: %w", err)
}
if !pkg.HasStartScript() {
return packit.DetectResult{}, packit.Fail.WithMessage(NoStartScriptError)
}
requirements := []packit.BuildPlanRequirement{
{
Name: Node,
Metadata: map[string]interface{}{
"launch": true,
},
},
{
Name: Yarn,
Metadata: map[string]interface{}{
"launch": true,
},
},
{
Name: NodeModules,
Metadata: map[string]interface{}{
"launch": true,
},
},
}
shouldReload, err := checkLiveReloadEnabled()
if err != nil {
return packit.DetectResult{}, err
}
if shouldReload {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "watchexec",
Metadata: map[string]interface{}{
"launch": true,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Requires: requirements,
},
}, nil
}
}
func checkLiveReloadEnabled() (bool, error) {
if reload, ok := os.LookupEnv("BP_LIVE_RELOAD_ENABLED"); ok {
shouldEnableReload, err := strconv.ParseBool(reload)
if err != nil {
return false, fmt.Errorf("failed to parse BP_LIVE_RELOAD_ENABLED value %s: %w", reload, err)
}
return shouldEnableReload, nil
}
return false, nil
}