-
Notifications
You must be signed in to change notification settings - Fork 9
/
nodejs.go
92 lines (79 loc) · 2.61 KB
/
nodejs.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package nodejs
import (
"context"
"os"
"path/filepath"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/build/buildkit"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/dependencies/pins"
"namespacelabs.dev/foundation/internal/runtime/rtypes"
"namespacelabs.dev/foundation/internal/runtime/tools"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
)
const (
workspaceContainerDir = "/workspace"
)
type RunNodejsOpts struct {
Scope schema.PackageName
Args []string
EnvVars []*schema.BinaryConfig_EnvEntry
Mounts []*rtypes.LocalMapping
IsInteractive bool
}
func RunNodejs(ctx context.Context, env cfg.Context, relPath string, command string, opts *RunNodejsOpts) error {
p, err := tools.HostPlatform(ctx, env.Configuration())
if err != nil {
return err
}
nodeImageName, err := pins.CheckDefault("node")
if err != nil {
return err
}
// TODO: generate a prebuilt
nodeImageState, err := prepareNodejsBaseWithYarn(ctx, nodeImageName, p)
if err != nil {
return err
}
nodejsImage, err := buildkit.BuildImage(ctx, env, build.NewBuildTarget(&p).WithSourceLabel("nodejs+yarn: %s", nodeImageName), nodeImageState)
if err != nil {
return err
}
image, err := compute.GetValue(ctx, nodejsImage)
if err != nil {
return err
}
var io rtypes.IO
if opts.IsInteractive {
done := console.EnterInputMode(ctx)
defer done()
io = rtypes.IO{Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}
} else if opts.Scope != "" {
stdout := console.Output(ctx, console.MakeConsoleName(opts.Scope.String(), "yarn", ""))
io = rtypes.IO{Stdout: stdout, Stderr: stdout}
} else {
stdout := console.Output(ctx, "yarn")
io = rtypes.IO{Stdout: stdout, Stderr: stdout}
}
abs := env.Workspace().LoadedFrom().AbsPath
return tools.Run(ctx, env.Configuration(), rtypes.RunToolOpts{
IO: io,
AllocateTTY: opts.IsInteractive,
Mounts: append(opts.Mounts, &rtypes.LocalMapping{
HostPath: abs,
// The user's filesystem structure is replicated within the container.
ContainerPath: filepath.Join(workspaceContainerDir, abs),
}),
RunBinaryOpts: rtypes.RunBinaryOpts{
Image: image,
WorkingDir: filepath.Join(workspaceContainerDir, abs, relPath),
Command: []string{command},
Args: opts.Args,
Env: opts.EnvVars,
}})
}