-
Notifications
You must be signed in to change notification settings - Fork 17
/
detect.go
74 lines (63 loc) · 1.64 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
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) {
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,
}
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: NodeModules},
},
Requires: []packit.BuildPlanRequirement{
nodeDependency,
{
Name: Npm,
Metadata: BuildPlanMetadata{
Build: true,
},
},
},
},
}, nil
}
}