forked from paketo-buildpacks/node-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
101 lines (87 loc) · 2.45 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
98
99
100
101
package nodeengine
import (
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
//go:generate faux --interface VersionParser --output fakes/version_parser.go
type VersionParser interface {
ParseVersion(path string) (version string, err error)
}
type BuildPlanMetadata struct {
Version string `toml:"version"`
VersionSource string `toml:"version-source"`
}
func Detect(nvmrcParser, nodeVersionParser VersionParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
var requirements []packit.BuildPlanRequirement
projectPath := context.WorkingDir
customProjPath := os.Getenv("BP_NODE_PROJECT_PATH")
if customProjPath != "" {
customProjPath = filepath.Clean(customProjPath)
projectPath = filepath.Join(projectPath, customProjPath)
exists, err := fs.Exists(projectPath)
if err != nil {
return packit.DetectResult{}, err
}
if !exists {
return packit.DetectResult{},
packit.Fail.WithMessage("expected value derived from BP_NODE_PROJECT_PATH [%s] to be an existing directory", projectPath)
}
}
version, err := nvmrcParser.ParseVersion(filepath.Join(projectPath, NvmrcSource))
if err != nil {
return packit.DetectResult{}, err
}
if version != "" {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Node,
Metadata: BuildPlanMetadata{
Version: version,
VersionSource: NvmrcSource,
},
})
}
version = os.Getenv("BP_NODE_VERSION")
if version != "" {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Node,
Metadata: BuildPlanMetadata{
Version: version,
VersionSource: "BP_NODE_VERSION",
},
})
}
version, err = nodeVersionParser.ParseVersion(filepath.Join(projectPath, NodeVersionSource))
if err != nil {
return packit.DetectResult{}, err
}
if version != "" {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Node,
Metadata: BuildPlanMetadata{
Version: version,
VersionSource: NodeVersionSource,
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: Node},
},
Requires: requirements,
Or: []packit.BuildPlan{
{
Provides: []packit.BuildPlanProvision{
{Name: Node},
{Name: Npm},
},
Requires: requirements,
},
},
},
}, nil
}
}