-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
58 lines (50 loc) · 1.2 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
package ngbuildcnb
import (
"path/filepath"
"fmt"
"os"
"encoding/json"
"github.com/paketo-buildpacks/packit"
)
func Detect() packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
dependencies, err := parseDependencies(filepath.Join(context.WorkingDir, "package.json"))
if err != nil {
return packit.DetectResult{}, err
}
if _, ok := dependencies["@angular/cli"]; !ok {
return packit.DetectResult{}, fmt.Errorf("Missing @angular/cli in package.json")
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: "ng_static"},
},
Requires: []packit.BuildPlanRequirement{
{Name: "ng_static"},
{
Name: "node_modules",
Metadata: map[string]interface{}{
"build": true,
},
},
},
},
}, nil
}
}
func parseDependencies(packageJsonPath string) (map[string]string, error) {
file, err := os.Open(packageJsonPath)
if err != nil {
return nil, err
}
defer file.Close()
var pkg struct {
Dependencies map[string]string `json:"dependencies"`
}
err = json.NewDecoder(file).Decode(&pkg)
if err != nil {
return nil, err
}
return pkg.Dependencies, nil
}