-
Notifications
You must be signed in to change notification settings - Fork 9
/
yarn.go
103 lines (87 loc) · 2.87 KB
/
yarn.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
// 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"
"fmt"
"os"
"path/filepath"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/localexec"
"namespacelabs.dev/foundation/internal/runtime/rtypes"
"namespacelabs.dev/foundation/internal/sdk/yarn"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/pkggraph"
)
const (
yarnBinaryPath = "/yarn.cjs"
fnYarnLockEnvVar = "FN_YARN_LOCK_FILENAME"
pluginFn = "plugin-foundation.cjs"
yarnRcFn = ".yarnrc.yml"
)
var UseNativeNode = false
func RunYarn(ctx context.Context, env cfg.Context, loc pkggraph.Location, args []string) error {
lockFileStruct, err := generateLockFileStruct(loc.Module.Workspace, loc.Module.Abs(), loc.Rel())
if err != nil {
return err
}
dir, err := os.MkdirTemp("", "ns-yarn")
if err != nil {
return err
}
if err := writeLockFileToTemp(filepath.Join(dir, lockFn), lockFileStruct); err != nil {
return err
}
yarnFilesDir := "/"
targetLockDirFn := "/ns-yarn-lock/"
if UseNativeNode {
yarnFilesDir = dir
targetLockDirFn = dir
}
envArgs := []*schema.BinaryConfig_EnvEntry{}
for k, v := range yarnEnvArgs(yarnFilesDir) {
envArgs = append(envArgs, &schema.BinaryConfig_EnvEntry{Name: k, Value: v})
}
envArgs = append(envArgs, &schema.BinaryConfig_EnvEntry{Name: fnYarnLockEnvVar, Value: filepath.Join(targetLockDirFn, lockFn)})
if UseNativeNode {
yarnBin, err := yarn.EnsureSDK(ctx)
if err != nil {
return err
}
if err := writeYarnAuxFiles(ctx, fnfs.ReadWriteLocalFS(dir)); err != nil {
return err
}
var cmd localexec.Command
cmd.Command = "node"
for _, kv := range envArgs {
cmd.AdditionalEnv = append(cmd.AdditionalEnv, fmt.Sprintf("%s=%s", kv.Name, kv.Value))
}
cmd.Args = append([]string{string(yarnBin)}, args...)
cmd.Dir = filepath.Join(env.Workspace().LoadedFrom().AbsPath, loc.Rel())
return cmd.Run(ctx)
}
mounts := []*rtypes.LocalMapping{{HostPath: dir, ContainerPath: targetLockDirFn}}
for moduleName, module := range lockFileStruct.Modules {
if moduleName != loc.Module.ModuleName() {
path := filepath.Join(loc.Module.Abs(), loc.Rel(), module.Path)
mounts = append(mounts, &rtypes.LocalMapping{
HostPath: path,
ContainerPath: filepath.Join(workspaceContainerDir, path),
})
}
}
return RunNodejs(ctx, env, loc.Rel(), "node", &RunNodejsOpts{
Scope: loc.PackageName,
Args: append([]string{string(yarnBinaryPath)}, args...),
EnvVars: envArgs,
Mounts: mounts,
})
}
func yarnEnvArgs(root string) map[string]string {
return map[string]string{
"YARN_PLUGINS": filepath.Join(root, pluginFn),
"YARN_RC_FILENAME": filepath.Join(root, yarnRcFn),
}
}