Skip to content

Commit df87970

Browse files
committed
Add support for yarn commands
1 parent 5afd899 commit df87970

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

cmd/npm_clean.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ const NodeDir = "node"
1616

1717
// npmCmd represents the npm command
1818
var npmCleanCmd = &cobra.Command{
19-
Use: "npm-clean",
20-
Short: "Cleanup npm related state",
19+
Use: "npm-clean",
20+
Aliases: []string{"yarn-clean"},
21+
Short: "Cleanup npm/yarn related state",
2122
Long: `
2223
Removes several npm state folders. (e.g. node_modules)
2324
`,

cmd/yarn.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/Graylog2/graylog-project-cli/config"
10+
"github.com/Graylog2/graylog-project-cli/logger"
11+
"github.com/Graylog2/graylog-project-cli/manifest"
12+
p "github.com/Graylog2/graylog-project-cli/project"
13+
"github.com/Graylog2/graylog-project-cli/utils"
14+
"github.com/fatih/color"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
var yarnCmd = &cobra.Command{
19+
Use: "yarn",
20+
Short: "Run yarn commands",
21+
Long: `
22+
Runs yarn commands in javascript projects. It checks for the presence of a package.json file.
23+
24+
Example:
25+
26+
# Run "yarn install" in every module
27+
$ graylog-project yarn install
28+
`,
29+
Run: yarnCommand,
30+
}
31+
32+
func init() {
33+
RootCmd.AddCommand(yarnCmd)
34+
}
35+
36+
func yarnCommand(cmd *cobra.Command, args []string) {
37+
if len(args) < 1 {
38+
logger.Info("Missing yarn command")
39+
cmd.UsageFunc()(cmd)
40+
os.Exit(1)
41+
}
42+
43+
manifestFiles := manifest.ReadState().Files()
44+
project := p.New(config.Get(), manifestFiles)
45+
46+
logger.Info("Current manifests: %v", manifestFiles)
47+
logger.Info("Executing `yarn %v` for every selected javascript module", strings.Join(args, " "))
48+
p.ForEachSelectedModuleOrSubmodules(project, func(module p.Module) {
49+
if module.IsNpmModule() {
50+
yarnExecForPath(module, args)
51+
}
52+
})
53+
}
54+
55+
func yarnExecForPath(module p.Module, args []string) {
56+
defer utils.Chdir(utils.GetCwd())
57+
58+
logger.ColorPrintln(color.FgMagenta, "[command output: %v]", filepath.Base(module.Path))
59+
60+
utils.Chdir(module.Path)
61+
62+
command := exec.Command("yarn", args...)
63+
64+
command.Stdout = os.Stdout
65+
command.Stderr = os.Stderr
66+
67+
if err := command.Run(); err != nil {
68+
logger.Fatal("Command failed: %v", err)
69+
}
70+
logger.Println("")
71+
}

0 commit comments

Comments
 (0)