-
Notifications
You must be signed in to change notification settings - Fork 8
/
detect.go
77 lines (66 loc) · 1.81 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 yarninstall
import (
"errors"
"os"
"path/filepath"
"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
type BuildPlanMetadata struct {
Version string `toml:"version"`
VersionSource string `toml:"version-source"`
Build bool `toml:"build"`
}
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
}
exists, err := fs.Exists(filepath.Join(projectPath, "yarn.lock"))
if err != nil {
return packit.DetectResult{}, err
}
if !exists {
return packit.DetectResult{}, packit.Fail.WithMessage("no 'yarn.lock' file found in the project path %s", projectPath)
}
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
}
nodeVersion := pkg.GetVersion()
nodeRequirement := packit.BuildPlanRequirement{
Name: PlanDependencyNode,
Metadata: BuildPlanMetadata{
Build: true,
},
}
if nodeVersion != "" {
nodeRequirement.Metadata = BuildPlanMetadata{
Version: nodeVersion,
VersionSource: "package.json",
Build: true,
}
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: PlanDependencyNodeModules},
},
Requires: []packit.BuildPlanRequirement{
nodeRequirement,
{
Name: PlanDependencyYarn,
Metadata: BuildPlanMetadata{
Build: true,
},
},
},
},
}, nil
}
}