-
Notifications
You must be signed in to change notification settings - Fork 6
/
detect.go
75 lines (64 loc) · 1.72 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
package npmstart
import (
"fmt"
"os"
"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/libreload-packit"
"github.com/paketo-buildpacks/packit/v2"
)
type Reloader libreload.Reloader
//go:generate faux --interface Reloader --output fakes/reloader.go
const NoStartScriptError = "no start script in package.json"
func Detect(reloader Reloader) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
projectPath, err := libnodejs.FindProjectPath(context.WorkingDir)
if err != nil {
return packit.DetectResult{}, err
}
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: Npm,
Metadata: map[string]interface{}{
"launch": true,
},
},
{
Name: NodeModules,
Metadata: map[string]interface{}{
"launch": true,
},
},
}
if shouldReload, err := reloader.ShouldEnableLiveReload(); err != nil {
return packit.DetectResult{}, err
} else if shouldReload {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "watchexec",
Metadata: map[string]interface{}{
"launch": true,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Requires: requirements,
},
}, nil
}
}