From bb115a36f384d6d694622b0f0a80580d464bacfc Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Tue, 9 Jan 2018 15:36:40 -0500 Subject: [PATCH] Make sure node deps are installed when running in dev fixes #839 --- buffalo/cmd/dev.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/buffalo/cmd/dev.go b/buffalo/cmd/dev.go index e50f97ba7..3d51f3c59 100644 --- a/buffalo/cmd/dev.go +++ b/buffalo/cmd/dev.go @@ -4,11 +4,13 @@ import ( "context" "os" "os/exec" + "path/filepath" "runtime" "github.com/fatih/color" "github.com/gobuffalo/buffalo/generators/assets/webpack" rg "github.com/gobuffalo/buffalo/generators/refresh" + "github.com/gobuffalo/buffalo/meta" "github.com/markbates/refresh/refresh" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -65,12 +67,29 @@ This behavior can be changed in your .buffalo.dev.yml file.`, } func startWebpack(ctx context.Context) error { - cfgFile := "./webpack.config.js" - _, err := os.Stat(cfgFile) - if err != nil { + app := meta.New(".") + if !app.WithWebpack { // there's no webpack, so don't do anything return nil } + + if _, err := os.Stat(filepath.Join(app.Root, "node_modules")); err != nil { + tool := "yarn" + if !app.WithYarn { + tool = "npm" + } + if _, err := exec.LookPath(tool); err != nil { + return errors.Errorf("no node_modules directory found, and couldn't find %s to install it with", tool) + } + cmd := exec.CommandContext(ctx, tool, "install") + cmd.Stdin = os.Stdin + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + if err := cmd.Run(); err != nil { + return errors.WithStack(err) + } + } + cmd := exec.CommandContext(ctx, webpack.BinPath, "--watch") cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr