forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
77 lines (65 loc) · 1.75 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
package npminstall
import (
"errors"
"os"
"path/filepath"
"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)
}
//go:generate faux --interface PathParser --output fakes/path_parser.go
type PathParser interface {
Get(path string) (projectPath string, err error)
}
func Detect(projectPathParser PathParser, packageJSONParser VersionParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
projectPath, err := projectPathParser.Get(context.WorkingDir)
if err != nil {
return packit.DetectResult{}, err
}
version, err := packageJSONParser.ParseVersion(filepath.Join(context.WorkingDir, projectPath, "package.json"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return packit.DetectResult{}, packit.Fail
}
return packit.DetectResult{}, err
}
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
}
}