-
Notifications
You must be signed in to change notification settings - Fork 17
/
build_process_resolver.go
138 lines (117 loc) · 3.62 KB
/
build_process_resolver.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package npminstall
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2/fs"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface Executable --output fakes/executable.go
type Executable interface {
Execute(pexec.Execution) (err error)
}
//go:generate faux --interface BuildProcess --output fakes/build_process.go
type BuildProcess interface {
ShouldRun(workingDir string, metadata map[string]interface{}, npmrcPath string) (run bool, sha string, err error)
Run(modulesDir, cacheDir, workingDir, npmrcPath string, launch bool) error
}
//go:generate faux --interface Summer --output fakes/summer.go
type Summer interface {
Sum(paths ...string) (string, error)
}
//go:generate faux --interface EnvironmentConfig --output fakes/environment_config.go
type EnvironmentConfig interface {
Lookup(key string) (value string, found bool)
LookupBool(key string) (bool, error)
}
type BuildProcessResolver struct {
logger scribe.Logger
rebuild BuildProcess
install BuildProcess
ci BuildProcess
}
func NewBuildProcessResolver(logger scribe.Logger, rebuild, install, ci BuildProcess) BuildProcessResolver {
return BuildProcessResolver{
logger: logger,
rebuild: rebuild,
install: install,
ci: ci,
}
}
func (r BuildProcessResolver) Resolve(workingDir string) (BuildProcess, bool, error) {
nodeModulesPath := filepath.Join(workingDir, "node_modules")
vendored, err := fs.Exists(nodeModulesPath)
if err != nil {
return nil, false, err
}
packageLockPath := filepath.Join(workingDir, "package-lock.json")
locked, err := fs.Exists(packageLockPath)
if err != nil {
return nil, false, err
}
npmCachePath := filepath.Join(workingDir, "npm-cache")
cached, err := fs.Exists(npmCachePath)
if err != nil {
return nil, false, err
}
wasItFound := map[bool]string{
true: "Found",
false: "Not found",
}
inputsMap := scribe.FormattedMap{
"package-lock.json": wasItFound[locked],
"node_modules": wasItFound[vendored],
"npm-cache": wasItFound[cached],
}
r.logger.Subprocess("Process inputs:")
r.logger.Action("%s", inputsMap)
r.logger.Break()
switch {
case !locked && vendored, locked && vendored && !cached:
r.logger.Subprocess("Selected NPM build process: 'npm rebuild'")
r.logger.Break()
return r.rebuild, cached, nil
case !locked && !vendored:
r.logger.Subprocess("Selected NPM build process: 'npm install'")
r.logger.Break()
return r.install, cached, nil
default:
r.logger.Subprocess("Selected NPM build process: 'npm ci'")
r.logger.Break()
return r.ci, cached, nil
}
}
// cacheExecutableResponse writes the output of a successfully executed command
// to a tmp file and returns the file location and possibly and error
func cacheExecutableResponse(executable Executable, args []string, workingDir string, npmrcPath string, logger scribe.Logger) (string, error) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
var environment []string
if npmrcPath != "" {
environment = append(os.Environ(), fmt.Sprintf("NPM_CONFIG_GLOBALCONFIG=%s", npmrcPath))
}
err := executable.Execute(pexec.Execution{
Args: args,
Dir: workingDir,
Env: environment,
Stdout: stdout,
Stderr: stderr,
})
if err != nil {
logger.Subprocess("error: %s", stderr.String())
return "", err
}
tmpFile, err := os.CreateTemp(workingDir, "executable_response")
if err != nil {
logger.Subprocess("error: %s", err)
return "", err
}
err = os.WriteFile(tmpFile.Name(), stdout.Bytes(), 0644)
if err != nil {
logger.Subprocess("error: %s", err)
return "", err
}
return tmpFile.Name(), nil
}