forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild_build_process.go
144 lines (124 loc) · 3.74 KB
/
rebuild_build_process.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
139
140
141
142
143
144
package npminstall
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/paketo-buildpacks/packit/v2/fs"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
type RebuildBuildProcess struct {
executable Executable
summer Summer
environment EnvironmentConfig
logger scribe.Logger
}
func NewRebuildBuildProcess(executable Executable, summer Summer, environment EnvironmentConfig, logger scribe.Logger) RebuildBuildProcess {
return RebuildBuildProcess{
executable: executable,
summer: summer,
environment: environment,
logger: logger,
}
}
func (r RebuildBuildProcess) ShouldRun(workingDir string, metadata map[string]interface{}, npmrcPath string) (bool, string, error) {
cachedNodeVersion, err := cacheExecutableResponse(
r.executable,
[]string{"get", "user-agent"},
workingDir,
npmrcPath,
r.logger)
if err != nil {
return false, "", fmt.Errorf("failed to execute npm get user-agent: %w", err)
}
defer os.Remove(cachedNodeVersion)
sum, err := r.summer.Sum(filepath.Join(workingDir, "node_modules"), cachedNodeVersion)
if err != nil {
return false, "", err
}
cacheSha, ok := metadata["cache_sha"].(string)
if !ok || sum != cacheSha {
return true, sum, nil
}
return false, "", nil
}
func (r RebuildBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath string, launch bool) error {
buffer := bytes.NewBuffer(nil)
environment := os.Environ()
if npmrcPath != "" {
environment = append(environment, fmt.Sprintf("NPM_CONFIG_GLOBALCONFIG=%s", npmrcPath))
}
err := r.executable.Execute(pexec.Execution{
Args: []string{"list"},
Dir: workingDir,
Env: environment,
Stdout: buffer,
Stderr: buffer,
})
if err != nil {
r.logger.Subprocess("%s", buffer.String())
return fmt.Errorf("vendored node_modules have unmet dependencies: npm list failed: %w", err)
}
args := []string{"run-script", "preinstall", "--if-present"}
r.logger.Subprocess("Running 'npm %s'", strings.Join(args, " "))
err = r.executable.Execute(pexec.Execution{
Args: args,
Dir: workingDir,
Env: environment,
Stdout: buffer,
Stderr: buffer,
})
if err != nil {
r.logger.Subprocess("%s", buffer.String())
return fmt.Errorf("preinstall script failed on rebuild: %s", err)
}
env := append(environment, fmt.Sprintf("NPM_CONFIG_LOGLEVEL=%s", r.environment.GetValue("NPM_CONFIG_LOGLEVEL")))
if !launch {
env = append(env, "NODE_ENV=development")
}
args = []string{"rebuild", fmt.Sprintf("--nodedir=%s", os.Getenv("NODE_HOME"))}
r.logger.Subprocess("Running 'npm %s'", strings.Join(args, " "))
err = r.executable.Execute(pexec.Execution{
Args: args,
Dir: workingDir,
Stdout: buffer,
Stderr: buffer,
Env: env,
})
if err != nil {
r.logger.Subprocess("%s", buffer.String())
return fmt.Errorf("npm rebuild failed: %s", err)
}
args = []string{"run-script", "postinstall", "--if-present"}
r.logger.Subprocess("Running 'npm %s'", strings.Join(args, " "))
err = r.executable.Execute(pexec.Execution{
Args: args,
Dir: workingDir,
Env: environment,
Stdout: buffer,
Stderr: buffer,
})
if err != nil {
r.logger.Subprocess("%s", buffer.String())
return fmt.Errorf("postinstall script failed on rebuild: %s", err)
}
_, err = os.Stat(filepath.Join(workingDir, "node_modules"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return fmt.Errorf("unable to stat node_modules in working directory: %w", err)
}
err = fs.Move(filepath.Join(workingDir, "node_modules"), filepath.Join(modulesDir, "node_modules"))
if err != nil {
return err
}
err = os.Symlink(filepath.Join(modulesDir, "node_modules"), filepath.Join(workingDir, "node_modules"))
if err != nil {
return err
}
return nil
}