forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.go
80 lines (65 loc) · 1.82 KB
/
webpack.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
package webpack
import (
"os/exec"
"path/filepath"
"github.com/gobuffalo/buffalo/generators"
"github.com/gobuffalo/buffalo/generators/assets"
"github.com/gobuffalo/buffalo/generators/assets/standard"
"github.com/gobuffalo/makr"
"github.com/gobuffalo/packr"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// TemplateBox contains all templates needed for the webpack generator
var TemplateBox = packr.NewBox("../webpack/templates")
var logo = &makr.RemoteFile{
File: makr.NewFile("assets/images/logo.svg", ""),
RemotePath: assets.LogoURL,
}
// BinPath is the path to the local install of webpack
var BinPath = filepath.Join("node_modules", ".bin", "webpack")
// Run webpack generator
func (w Generator) Run(root string, data makr.Data) error {
g := makr.New()
// if there's no npm, return!
if _, err := exec.LookPath("npm"); err != nil {
logrus.Info("Could not find npm. Skipping webpack generation.")
return standard.Run(root, data)
}
command := "yarn"
if !w.WithYarn {
command = "npm"
} else {
err := installYarn(data)
if err != nil {
return errors.WithStack(err)
}
}
g.Add(logo)
files, err := generators.FindByBox(TemplateBox)
if err != nil {
return errors.WithStack(err)
}
for _, f := range files {
g.Add(makr.NewFile(f.WritePath, f.Body))
}
args := []string{"install", "--no-progress", "--save"}
g.Add(makr.NewCommand(exec.Command(command, args...)))
data["opts"] = w
return g.Run(root, data)
}
func installYarn(data makr.Data) error {
// if there's no yarn, install it!
_, err := exec.LookPath("yarn")
// A new makr is necessary to have yarn available in path
if err != nil {
yg := makr.New()
yargs := []string{"install", "-g", "yarn"}
yg.Add(makr.NewCommand(exec.Command("npm", yargs...)))
err = yg.Run(".", data)
if err != nil {
return err
}
}
return nil
}