forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci_build_process.go
108 lines (93 loc) · 2.74 KB
/
ci_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
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 CIBuildProcess struct {
executable Executable
summer Summer
environment EnvironmentConfig
logger scribe.Logger
}
func NewCIBuildProcess(executable Executable, summer Summer, environment EnvironmentConfig, logger scribe.Logger) CIBuildProcess {
return CIBuildProcess{
executable: executable,
summer: summer,
environment: environment,
logger: logger,
}
}
func (r CIBuildProcess) ShouldRun(workingDir string, metadata map[string]interface{}, npmrcConfig string) (bool, string, error) {
cachedNodeVersion, err := cacheExecutableResponse(
r.executable,
[]string{"get", "user-agent"},
workingDir,
npmrcConfig,
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, "package.json"),
filepath.Join(workingDir, "package-lock.json"),
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 CIBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath string, launch bool) error {
err := os.MkdirAll(filepath.Join(workingDir, "node_modules"), os.ModePerm)
if err != nil {
return err
}
buffer := bytes.NewBuffer(nil)
args := []string{"ci", "--unsafe-perm", "--cache", cacheDir}
environment := append(os.Environ(), fmt.Sprintf("NPM_CONFIG_LOGLEVEL=%s", r.environment.GetValue("NPM_CONFIG_LOGLEVEL")))
if npmrcPath != "" {
environment = append(environment, fmt.Sprintf("NPM_CONFIG_GLOBALCONFIG=%s", npmrcPath))
}
if !launch {
environment = append(environment, "NODE_ENV=development")
}
r.logger.Subprocess("Running 'npm %s'", strings.Join(args, " "))
err = r.executable.Execute(pexec.Execution{
Args: args,
Dir: workingDir,
Stdout: buffer,
Stderr: buffer,
Env: environment,
})
if err != nil {
r.logger.Subprocess("%s", buffer.String())
return fmt.Errorf("npm ci failed: %w", 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
}