-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.go
66 lines (54 loc) · 1.7 KB
/
build.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
package noderunscript
import (
"fmt"
"time"
"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"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(execution pexec.Execution) error
}
func Build(npm Executable, yarn Executable, clock chronos.Clock, logger scribe.Logger, env Environment) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
projectDir, err := libnodejs.FindProjectPath(context.WorkingDir)
if err != nil {
return packit.BuildResult{}, err
}
scripts, packageManager, err := ScriptsToRun(projectDir, env.NodeRunScripts)
if err != nil {
return packit.BuildResult{}, fmt.Errorf("failed to find scripts to run: %w", err)
}
exec := npm
if packageManager == "yarn" {
exec = yarn
}
logger.Process("Executing build process")
duration, err := clock.Measure(func() error {
for _, script := range scripts {
logger.Subprocess("Running '%s %s %s'", packageManager, "run", script)
err := exec.Execute(pexec.Execution{
Dir: projectDir,
Args: []string{"run", script},
Stdout: logger.ActionWriter,
Stderr: logger.ActionWriter,
})
if err != nil {
return err
}
logger.Break()
}
return nil
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Subprocess("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
return packit.BuildResult{}, nil
}
}