forked from paketo-buildpacks/yarn-install
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetect.go
More file actions
115 lines (102 loc) · 3.66 KB
/
detect.go
File metadata and controls
115 lines (102 loc) · 3.66 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package yarninstall
import (
"encoding/json"
"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()
// Determine yarn version from package.json (engines.yarn or packageManager)
yarnVersion := ""
yarnVersionSource := ""
// Read raw package.json to inspect engines and packageManager
pkgJSONPath := filepath.Join(projectPath, "package.json")
if data, readErr := os.ReadFile(pkgJSONPath); readErr == nil {
var raw struct {
Engines map[string]string `json:"engines"`
PackageManager string `json:"packageManager"`
}
if jsonErr := json.Unmarshal(data, &raw); jsonErr == nil {
if raw.Engines != nil {
if v, ok := raw.Engines["yarn"]; ok && v != "" {
yarnVersion = v
yarnVersionSource = "package.json#engines.yarn"
}
}
if yarnVersion == "" && len(raw.PackageManager) > 0 {
// Expect format like "yarn@4.2.2"; extract substring after '@'
if idx := len("yarn@"); len(raw.PackageManager) > idx && raw.PackageManager[:idx] == "yarn@" {
yarnVersion = raw.PackageManager[idx:]
yarnVersionSource = "package.json#packageManager"
}
}
}
}
nodeRequirement := packit.BuildPlanRequirement{
Name: PlanDependencyNode,
Metadata: BuildPlanMetadata{
Build: true,
},
}
if nodeVersion != "" {
nodeRequirement.Metadata = BuildPlanMetadata{
Version: nodeVersion,
VersionSource: "package.json",
Build: true,
}
}
// Compose Yarn requirement, optionally with version metadata
yarnRequirement := packit.BuildPlanRequirement{
Name: PlanDependencyYarn,
Metadata: BuildPlanMetadata{
Build: true,
},
}
if yarnVersion != "" {
yarnRequirement.Metadata = BuildPlanMetadata{
Version: yarnVersion,
VersionSource: yarnVersionSource,
Build: true,
}
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: PlanDependencyNodeModules},
},
Requires: []packit.BuildPlanRequirement{
nodeRequirement,
yarnRequirement,
},
},
}, nil
}
}