-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdetect.go
More file actions
95 lines (79 loc) · 2.31 KB
/
detect.go
File metadata and controls
95 lines (79 loc) · 2.31 KB
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
package npminstall
import (
"errors"
"os"
"path/filepath"
"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2"
)
type BuildPlanMetadata struct {
Version string `toml:"version"`
VersionSource string `toml:"version-source"`
Build bool `toml:"build"`
Launch bool `toml:"launch"`
}
//go:generate faux --interface VersionParser --output fakes/version_parser.go
type VersionParser interface {
ParseVersion(path string) (version string, err error)
}
func Detect() packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
var requirements []packit.BuildPlanRequirement
projectPath, err := libnodejs.FindProjectPath(context.WorkingDir)
if err != nil {
return packit.DetectResult{}, err
}
pkg, err := libnodejs.ParsePackageJSON(projectPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return packit.DetectResult{}, packit.Fail.WithMessage("no 'package.json' found in project path %s", filepath.Join(projectPath))
}
return packit.DetectResult{}, err
}
version := pkg.GetVersion()
nodeDependency := packit.BuildPlanRequirement{
Name: Node,
Metadata: BuildPlanMetadata{
Build: true,
},
}
if version != "" {
nodeDependency.Metadata = BuildPlanMetadata{
Version: version,
VersionSource: "package.json",
Build: true,
}
}
requirements = append(requirements, nodeDependency)
bpNpmIncludeBuildPython, bpNpmIncludeBuildPythonExists := os.LookupEnv("BP_NPM_INCLUDE_BUILD_PYTHON")
installPython := false
if bpNpmIncludeBuildPythonExists && (bpNpmIncludeBuildPython == "" || bpNpmIncludeBuildPython == "true") {
installPython = true
} else if bpNpmIncludeBuildPythonExists && bpNpmIncludeBuildPython == "false" {
installPython = false
}
if installPython {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Cpython,
Metadata: BuildPlanMetadata{
Build: true,
Launch: false,
},
})
}
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Npm,
Metadata: BuildPlanMetadata{
Build: true,
},
})
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: NodeModules},
},
Requires: requirements,
},
}, nil
}
}