forked from paketo-buildpacks/node-run-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.go
90 lines (77 loc) · 2.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
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
package noderunscript
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/paketo-buildpacks/packit"
)
type BuildPlanMetadata struct {
Build bool `toml:"build"`
}
func Detect(scriptManager PackageInterface) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
envRunScripts, exists := os.LookupEnv("BP_NODE_RUN_SCRIPTS")
// If no node scripts, run build
if !exists {
envRunScripts = "build"
}
projectDir := context.WorkingDir
envProjectPath, exists := os.LookupEnv("BP_NODE_PROJECT_PATH")
if exists {
projectDir = filepath.Join(context.WorkingDir, envProjectPath)
}
projectDir = filepath.Clean(projectDir)
_, err := os.Stat(projectDir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return packit.DetectResult{},
fmt.Errorf("expected value from $BP_NODE_PROJECT_PATH [%s] to be an existing directory",
projectDir)
}
return packit.DetectResult{}, err
}
_, err = os.Stat(filepath.Join(projectDir, "package.json"))
if err != nil {
return packit.DetectResult{},
packit.Fail.WithMessage("expected file package.json to exist")
}
packageScripts, err := scriptManager.GetPackageScripts(projectDir)
if err != nil {
return packit.DetectResult{}, err
}
envScripts := strings.Split(envRunScripts, ",")
var missingScripts []string
for _, envScript := range envScripts {
envScript = strings.TrimSpace(envScript)
if _, exists := packageScripts[envScript]; !exists {
missingScripts = append(missingScripts, envScript)
}
}
if len(missingScripts) > 0 {
return packit.DetectResult{},
fmt.Errorf("expected script(s) %s from $BP_NODE_RUN_SCRIPTS to exist in package.json",
strings.Join(missingScripts, ", "))
}
packageManager := scriptManager.GetPackageManager(projectDir)
return packit.DetectResult{
Plan: packit.BuildPlan{
Requires: []packit.BuildPlanRequirement{
{
Name: "node",
Metadata: BuildPlanMetadata{Build: true},
},
{
Name: packageManager,
Metadata: BuildPlanMetadata{Build: true},
},
{
Name: "node_modules",
Metadata: BuildPlanMetadata{Build: true},
},
},
},
}, nil
}
}