This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
/
npm.go
145 lines (124 loc) · 3.12 KB
/
npm.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package fix
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/gobuffalo/buffalo-cli/internal/v1/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
// file with the new one.
func PackageJSONCheck(r *Runner) error {
fmt.Println("~~~ Checking package.json ~~~")
if !r.App.WithWebpack {
return nil
}
box := webpack.Templates
f, err := box.FindString("package.json.tmpl")
if err != nil {
return err
}
tmpl, err := template.New("package.json").Parse(f)
if err != nil {
return err
}
bb := &bytes.Buffer{}
err = tmpl.Execute(bb, map[string]interface{}{
"opts": &webpack.Options{
App: r.App,
},
})
if err != nil {
return err
}
b, err := ioutil.ReadFile("package.json")
if err != nil {
return err
}
if string(b) == bb.String() {
return nil
}
if !ask("Your package.json file is different from the latest Buffalo template.\nWould you like to REPLACE yours with the latest template?") {
fmt.Println("\tskipping package.json")
return nil
}
pf, err := os.Create("package.json")
if err != nil {
return err
}
_, err = pf.Write(bb.Bytes())
if err != nil {
return err
}
err = pf.Close()
if err != nil {
return err
}
os.RemoveAll(filepath.Join(r.App.Root, "node_modules"))
var cmd *exec.Cmd
if r.App.WithYarn {
cmd = exec.Command("yarnpkg", "install")
} else {
cmd = exec.Command("npm", "install")
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}