Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Replace hard-coded webpack with node script call (#1718)
Browse files Browse the repository at this point in the history
* Replace hard-coded webpack with node script call

* Fix assets test

* Fix some issues with the fix command

* Rewrite package.json only if necessary

* Fix WithNodeJs opt

Currently, the only way to generate a project with node is to use
Webpack. If the --skip-webpack flag is used, WithNodeJs must be false
then.
  • Loading branch information
stanislas-m authored Jul 3, 2019
1 parent e77d2e6 commit f3351e3
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 251 deletions.
5 changes: 4 additions & 1 deletion buffalo/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ var xbuildCmd = &cobra.Command{
ctx, cancel := sigtx.WithCancel(context.Background(), os.Interrupt)
defer cancel()

pwd, _ := os.Getwd()
pwd, err := os.Getwd()
if err != nil {
return err
}

buildOptions.App = meta.New(pwd)
if len(buildOptions.bin) > 0 {
Expand Down
41 changes: 16 additions & 25 deletions buffalo/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/fatih/color"
"github.com/gobuffalo/buffalo/genny/assets/webpack"
rg "github.com/gobuffalo/buffalo/genny/refresh"
"github.com/gobuffalo/events"
"github.com/gobuffalo/genny"
Expand Down Expand Up @@ -67,7 +65,15 @@ This behavior can be changed in .buffalo.dev.yml file.`,
})

wg.Go(func() error {
return startWebpack(ctx)
app := meta.New(".")
if !app.WithNodeJs {
// No need to run dev script
return nil
}
if _, err := app.NodeScript("dev"); err != nil {
return err
}
return runDevScript(ctx, app)
})

err := wg.Wait()
Expand All @@ -78,30 +84,15 @@ This behavior can be changed in .buffalo.dev.yml file.`,
},
}

func startWebpack(ctx context.Context) error {
app := meta.New(".")
if !app.WithWebpack {
// there's no webpack, so don't do anything
return nil
func runDevScript(ctx context.Context, app meta.App) error {
tool := "yarnpkg"
if !app.WithYarn {
tool = "npm"
}

if _, err := os.Stat(filepath.Join(app.Root, "node_modules")); err != nil {
tool := "yarnpkg"
if !app.WithYarn {
tool = "npm"
}
if _, err := exec.LookPath(tool); err != nil {
return fmt.Errorf("no node_modules directory found, and couldn't find %s to install it with", tool)
}
cmd := exec.CommandContext(ctx, tool, "install")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return err
}
if _, err := exec.LookPath(tool); err != nil {
return fmt.Errorf("could not find %s tool", tool)
}

cmd := exec.CommandContext(ctx, webpack.BinPath, "--watch")
cmd := exec.CommandContext(ctx, tool, "run", "dev")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
Expand Down
1 change: 1 addition & 0 deletions buffalo/cmd/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var checks = []Check{
mr.transformPackages,
WebpackCheck,
PackageJSONCheck,
AddPackageJSONScripts,
DepEnsure,
installTools,
DeprecrationsCheck,
Expand Down
58 changes: 58 additions & 0 deletions buffalo/cmd/fix/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fix

import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
Expand All @@ -12,6 +13,63 @@ import (
"github.com/gobuffalo/buffalo/genny/assets/webpack"
)

// AddPackageJSONScripts rewrites the package.json file
// to add dev and build scripts if there are missing.
func AddPackageJSONScripts(r *Runner) error {
if !r.App.WithWebpack {
return nil
}
fmt.Println("~~~ Patching package.json to add dev and build scripts ~~~")

b, err := ioutil.ReadFile("package.json")
if err != nil {
return err
}

needRewrite := false
packageJSON := map[string]interface{}{}
if err := json.Unmarshal(b, &packageJSON); err != nil {
return fmt.Errorf("could not rewrite package.json: %s", err.Error())
}

if _, ok := packageJSON["scripts"]; !ok {
needRewrite = true
// Add scripts
packageJSON["scripts"] = map[string]string{
"dev": "webpack --watch",
"build": "webpack -p --progress",
}
} else {
// Add missing scripts
scripts, ok := packageJSON["scripts"].(map[string]interface{})
if !ok {
return fmt.Errorf("could not rewrite package.json: invalid scripts section")
}
if _, ok := scripts["dev"]; !ok {
needRewrite = true
scripts["dev"] = "webpack --watch"
}
if _, ok := scripts["build"]; !ok {
needRewrite = true
scripts["build"] = "webpack -p --progress"
}
packageJSON["scripts"] = scripts
}

if needRewrite {
b, err = json.MarshalIndent(packageJSON, "", " ")
if err != nil {
return fmt.Errorf("could not rewrite package.json: %s", err.Error())
}

ioutil.WriteFile("package.json", b, 644)
} else {
fmt.Println("~~~ package.json doesn't need to be patched, skipping. ~~~")
}

return nil
}

// PackageJSONCheck will compare the current default Buffalo
// package.json against the applications package.json. If they are
// different you have the option to overwrite the existing package.json
Expand Down
4 changes: 2 additions & 2 deletions buffalo/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"path/filepath"
"strings"

"github.com/gobuffalo/buffalo/genny/docker"
pop "github.com/gobuffalo/buffalo-pop/genny/newapp"
"github.com/gobuffalo/buffalo/genny/assets/standard"
"github.com/gobuffalo/buffalo/genny/assets/webpack"
"github.com/gobuffalo/buffalo/genny/ci"
"github.com/gobuffalo/buffalo/genny/docker"
"github.com/gobuffalo/buffalo/genny/newapp/api"
"github.com/gobuffalo/buffalo/genny/newapp/core"
"github.com/gobuffalo/buffalo/genny/newapp/web"
Expand Down Expand Up @@ -89,7 +89,7 @@ func parseNewOptions(args []string) (newAppOptions, error) {
app.WithPop = !viper.GetBool("skip-pop")
app.WithWebpack = !viper.GetBool("skip-webpack")
app.WithYarn = !viper.GetBool("skip-yarn")
app.WithNodeJs = app.WithYarn || app.WithWebpack
app.WithNodeJs = app.WithWebpack
app.AsWeb = !app.AsAPI

if app.AsAPI {
Expand Down
4 changes: 4 additions & 0 deletions genny/assets/webpack/templates/package.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "webpack --watch",
"build": "webpack -p --progress"
},
"repository": "github.com/gobuffalo/buffalo",
"dependencies": {
"bootstrap": "4.3.1",
Expand Down
12 changes: 9 additions & 3 deletions genny/build/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os/exec"
"path/filepath"

"github.com/gobuffalo/buffalo/genny/assets/webpack"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/genny"

Expand All @@ -20,7 +19,10 @@ func assets(opts *Options) (*genny.Generator, error) {
return g, err
}

if opts.App.WithWebpack {
if opts.App.WithNodeJs {
if _, err := opts.App.NodeScript("build"); err != nil {
return g, err
}
if opts.CleanAssets {
g.RunFn(func(r *genny.Runner) error {
r.Delete(filepath.Join(opts.App.Root, "public", "assets"))
Expand All @@ -32,8 +34,12 @@ func assets(opts *Options) (*genny.Generator, error) {
return envy.MustSet("NODE_ENV", opts.Environment)
})
g.RunFn(func(r *genny.Runner) error {
tool := "yarnpkg"
if !opts.App.WithYarn {
tool = "npm"
}
bb := &bytes.Buffer{}
c := exec.Command(webpack.BinPath)
c := exec.CommandContext(r.Context, tool, "run", "build")
c.Stdout = bb
c.Stderr = bb
if err := r.Exec(c); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions genny/build/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"
"testing"

"github.com/gobuffalo/buffalo/genny/assets/webpack"
"github.com/gobuffalo/envy"
"github.com/stretchr/testify/require"
)
Expand All @@ -16,7 +15,10 @@ func Test_assets(t *testing.T) {
WithAssets: true,
}
r.NoError(opts.Validate())
opts.App.WithWebpack = true
opts.App.WithNodeJs = true
opts.App.PackageJSON.Scripts = map[string]string{
"build": "webpack -p --progress",
}

run := cokeRunner()
run.WithNew(assets(opts))
Expand All @@ -32,7 +34,7 @@ func Test_assets(t *testing.T) {

res := run.Results()

cmds := []string{webpack.BinPath}
cmds := []string{"npm run build"}
r.Len(res.Commands, len(cmds))
for i, c := range res.Commands {
r.Equal(cmds[i], strings.Join(c.Args, " "))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/gobuffalo/buffalo
go 1.12

require (
cloud.google.com/go v0.36.0 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.7.0
Expand Down
412 changes: 195 additions & 217 deletions packrd/packed-packr.go

Large diffs are not rendered by default.

0 comments on commit f3351e3

Please sign in to comment.