forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.go
More file actions
379 lines (336 loc) · 9.58 KB
/
js.go
File metadata and controls
379 lines (336 loc) · 9.58 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ttnmage
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
)
func nodeBin(cmd string) string { return filepath.Join("node_modules", ".bin", cmd) }
// DevDeps installs the javascript development dependencies.
func devDeps() error {
_, err := yarn()
if err != nil {
return err
}
mg.Deps(Js.Deps)
return nil
}
// Js namespace.
type Js mg.Namespace
func installYarn() error {
packageJSONBytes, err := ioutil.ReadFile("package.json")
if err != nil {
return err
}
var packageJSON struct {
DevDependencies map[string]string `json:"devDependencies"`
}
if err = json.Unmarshal(packageJSONBytes, &packageJSON); err != nil {
return err
}
yarn, ok := packageJSON.DevDependencies["yarn"]
if ok {
yarn = "yarn@" + yarn
} else {
yarn = "yarn"
}
if mg.Verbose() {
fmt.Printf("Installing Yarn %s\n", yarn)
}
return sh.RunV("npm", "install", "--no-package-lock", "--no-save", "--production=false", yarn)
}
func yarn() (func(args ...string) error, error) {
if _, err := os.Stat(nodeBin("yarn")); os.IsNotExist(err) {
if err = installYarn(); err != nil {
return nil, err
}
}
return func(args ...string) error {
return sh.RunV(nodeBin("yarn"), args...)
}, nil
}
func (js Js) node() (func(args ...string) error, error) {
return func(args ...string) error {
return sh.Run("node", args...)
}, nil
}
func (js Js) execFromNodeBin(cmd string, verbose bool) (func(args ...string) error, error) {
if _, err := os.Stat(nodeBin(cmd)); os.IsNotExist(err) {
if err = js.DevDeps(); err != nil {
return nil, err
}
}
out := os.Stdout
if !mg.Verbose() && !verbose {
out = nil
}
return func(args ...string) error {
_, err := sh.Exec(nil, out, os.Stderr, nodeBin(cmd), args...)
return err
}, nil
}
func (js Js) webpack() (func(args ...string) error, error) {
return js.execFromNodeBin("webpack", false)
}
func (js Js) webpackServe() (func(args ...string) error, error) {
return js.execFromNodeBin("webpack-dev-server", true)
}
func (js Js) babel() (func(args ...string) error, error) {
if _, err := os.Stat(nodeBin("babel")); os.IsNotExist(err) {
if err = js.DevDeps(); err != nil {
return nil, err
}
}
return func(args ...string) error {
_, err := sh.Exec(nil, nil, os.Stderr, nodeBin("babel"), args...)
return err
}, nil
}
func (js Js) jest() (func(args ...string) error, error) {
return js.execFromNodeBin("jest", false)
}
func (js Js) prettier() (func(args ...string) error, error) {
return js.execFromNodeBin("prettier", false)
}
func (js Js) eslint() (func(args ...string) error, error) {
return js.execFromNodeBin("eslint", false)
}
func (js Js) stylint() (func(args ...string) error, error) {
return js.execFromNodeBin("stylint", false)
}
func (js Js) storybook() (func(args ...string) error, error) {
return js.execFromNodeBin("start-storybook", true)
}
// DevDeps installs the javascript development dependencies.
func (js Js) DevDeps() error {
return devDeps()
}
// Deps installs the javascript dependencies.
func (js Js) Deps() error {
files, readErr := ioutil.ReadDir("node_modules")
changed, targetErr := target.Path("node_modules", "./package.json", "./yarn.lock")
// Check whether package.json/yarn.lock are newer than node_modules
// and whether it is not only yarn that is installed via DevDeps()
if readErr != nil || os.IsNotExist(targetErr) || (targetErr == nil && changed) || len(files) <= 4 {
if mg.Verbose() {
fmt.Println("Installing JS dependencies")
}
yarn, err := yarn()
if err != nil {
return err
}
err = yarn("install", "--no-progress", "--production=false")
if err != nil {
return err
}
mg.Deps(JsSDK.Link)
return nil
}
return nil
}
// Build runs all necessary commands to build the console bundles and files.
func (js Js) Build() {
mg.SerialDeps(js.Deps, JsSDK.Build, js.BuildDll, js.BuildMain)
}
// BuildMain runs the webpack command with the project config.
func (js Js) BuildMain() error {
mg.Deps(js.Translations, js.BackendTranslations, js.BuildDll)
if mg.Verbose() {
fmt.Println("Running Webpack")
}
webpack, err := js.webpack()
if err != nil {
return err
}
return webpack("--config", "config/webpack.config.babel.js")
}
// BuildDll runs the webpack command to build the DLL bundle
func (js Js) BuildDll() error {
mg.Deps(js.Deps)
if nodeEnv := os.Getenv("NODE_ENV"); nodeEnv == "development" {
changed, err := target.Path("./public/libs.bundle.js", "./yarn.lock")
if changed || os.IsNotExist(err) {
if mg.Verbose() {
fmt.Println("Running Webpack for DLL...")
}
webpack, err := js.webpack()
if err != nil {
return err
}
return webpack("--config", "config/webpack.dll.babel.js")
}
return nil
}
if mg.Verbose() {
fmt.Println("Skipping DLL module bundling (production mode)")
}
return nil
}
// Serve builds necessary bundles and serves the console for development.
func (js Js) Serve() {
mg.Deps(js.ServeMain)
}
// ServeMain runs webpack-dev-server
func (js Js) ServeMain() error {
mg.Deps(js.Translations, js.BackendTranslations, js.BuildDll)
if mg.Verbose() {
fmt.Println("Running Webpack for Main Bundle in watch mode...")
}
webpackServe, err := js.webpackServe()
if err != nil {
return err
}
os.Setenv("DEV_SERVER_BUILD", "true")
return webpackServe("--config", "config/webpack.config.babel.js", "-w")
}
// Messages extracts the frontend messages via babel.
func (js Js) Messages() error {
changed, err := target.Dir("./.cache/messages", "./pkg/webui/console")
if os.IsNotExist(err) || (err == nil && changed) {
if mg.Verbose() {
fmt.Println("Extracting frontend messages...")
}
babel, err := js.babel()
if err != nil {
return err
}
if err = sh.Rm(".cache/messages"); err != nil {
return err
}
if err = os.MkdirAll("pkg/webui/locales", 0755); err != nil {
return err
}
return babel("pkg/webui")
}
return nil
}
// Translations builds the frontend locale files.
func (js Js) Translations() error {
changed, err := target.Dir("./pkg/webui/locales/en.json", "./.cache/messages")
if os.IsNotExist(err) || (err == nil && changed) {
mg.Deps(js.Messages)
if mg.Verbose() {
fmt.Println("Building frontend locale files...")
}
node, err := js.node()
if err != nil {
return err
}
return node(".mage/translations.js")
}
return nil
}
// BackendTranslations builds the backend locale files.
func (js Js) BackendTranslations() error {
changed, err := target.Path("./pkg/webui/locales/.backend/en.json", "./config/messages.json")
if os.IsNotExist(err) || (err == nil && changed) {
if mg.Verbose() {
fmt.Println("Building backend locale files...")
}
node, err := js.node()
if err != nil {
return err
}
return node(".mage/translations.js", "--backend-messages", "config/messages.json", "--locales", "pkg/webui/locales/.backend", "--backend-only")
}
return nil
}
// Clean clears all generated files.
func (js Js) Clean() {
sh.Rm(".cache")
sh.Rm("public")
sh.Rm(filepath.Join("pkg", "webui", "locales", ".backend"))
}
// CleanDeps removes all installed node packages (rm -rf node_modules).
func (js Js) CleanDeps() {
sh.Rm("node_modules")
}
// Test runs frontend jest tests.
func (js Js) Test() error {
if mg.Verbose() {
fmt.Println("Running Tests")
}
jest, err := js.jest()
if err != nil {
return err
}
return jest("./pkg/webui")
}
// Fmt formats all js files.
func (js Js) Fmt() error {
if mg.Verbose() {
fmt.Println("Running prettier on .js files")
}
prettier, err := js.prettier()
if err != nil {
return err
}
return prettier("--config", "./config/.prettierrc.js", "./pkg/webui/**/*.js", "./config/**/*.js", "--write")
}
// Lint runs eslint over frontend js files.
func (js Js) Lint() error {
if mg.Verbose() {
fmt.Println("Running eslint on .js files")
}
eslint, err := js.eslint()
if err != nil {
return err
}
// TODO: Remove the `--quiet` flag after all component prop-types are defined
// (https://github.com/TheThingsNetwork/lorawan-stack/issues/1086)
return eslint("./pkg/webui/**/*.js", "./config/**/*.js", "--no-ignore", "--color", "--quiet")
}
// LintSnap runs eslint over frontend snap files.
func (js Js) LintSnap() error {
if mg.Verbose() {
fmt.Println("Running eslint on .snap files")
}
eslint, err := js.eslint()
if err != nil {
return err
}
return eslint("./pkg/webui/**/*.snap", "--no-ignore", "--color")
}
// LintAll runs linters over js and snap files.
func (js Js) LintAll() {
mg.Deps(js.Lint, js.LintSnap)
}
// Storybook runs a local server with storybook.
func (js Js) Storybook() error {
if mg.Verbose() {
fmt.Println("Serving storybook...")
}
storybook, err := js.storybook()
if err != nil {
return err
}
return storybook("--config-dir", "./config/storybook", "--static-dir", "public", "--port", "9001")
}
// Vulnerabilities runs yarn audit to check for vulnerable node packages.
func (js Js) Vulnerabilities() error {
if mg.Verbose() {
fmt.Println("Checking for vulnerabilities")
}
yarn, err := yarn()
if err != nil {
return err
}
return yarn("audit")
}