-
Notifications
You must be signed in to change notification settings - Fork 8
/
nodejs.go
79 lines (63 loc) · 1.76 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
// 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 source
import (
"context"
"os"
"path/filepath"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/nodejs"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/parsing/module"
"namespacelabs.dev/foundation/std/cfg"
)
func newNodejsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "node",
Short: "Run nodejs.",
}
var relPath string
yarn := fncobra.CmdWithEnv(&cobra.Command{
Use: "yarn",
Short: "Run Yarn.",
}, func(ctx context.Context, env cfg.Context, args []string) error {
root, err := module.FindRoot(ctx, ".")
if err != nil {
return err
}
if relPath == "" {
var err error
relPath, err = relCwd(ctx)
if err != nil {
return err
}
}
loc, err := parsing.NewPackageLoader(env).Resolve(ctx, root.RelPackage(relPath).AsPackageName())
if err != nil {
return err
}
return nodejs.RunYarn(ctx, env, loc, args)
})
yarn.Flags().StringVar(&relPath, "rel_path", "", "If not set, will be computed.")
cmd.AddCommand(yarn)
return fncobra.CmdWithEnv(cmd, func(ctx context.Context, env cfg.Context, args []string) error {
relPath, err := relCwd(ctx)
if err != nil {
return err
}
return nodejs.RunNodejs(ctx, env, relPath, "node", &nodejs.RunNodejsOpts{Args: args, IsInteractive: true})
})
}
func relCwd(ctx context.Context) (string, error) {
root, err := module.FindRoot(ctx, ".")
if err != nil {
return "", err
}
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Rel(root.Abs(), cwd)
}