Skip to content

Commit df4a934

Browse files
committed
refactor: only fetch library when needed
#932 WIP
1 parent 324a0ff commit df4a934

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

cmd/vale/library.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import "encoding/json"
4+
5+
var library = "https://raw.githubusercontent.com/errata-ai/styles/master/library.json"
6+
7+
func getLibrary(_ string) ([]Style, error) {
8+
styles := []Style{}
9+
10+
resp, err := fetchJSON(library)
11+
if err != nil {
12+
return styles, err
13+
} else if err = json.Unmarshal(resp, &styles); err != nil {
14+
return styles, err
15+
}
16+
17+
return styles, err
18+
}
19+
20+
func inLibrary(name, path string) string {
21+
lookup, err := getLibrary(path)
22+
if err != nil {
23+
return ""
24+
}
25+
26+
for _, entry := range lookup {
27+
if name == entry.Name {
28+
return entry.URL
29+
}
30+
}
31+
32+
return ""
33+
}

cmd/vale/sync.go

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"os"
76
"path/filepath"
@@ -13,8 +12,6 @@ import (
1312
"github.com/errata-ai/vale/v3/internal/core"
1413
)
1514

16-
var library = "https://raw.githubusercontent.com/errata-ai/styles/master/library.json"
17-
1815
func initPath(cfg *core.Config) error {
1916
// The first entry is always the default `StylesPath`.
2017
stylesPath := cfg.StylesPath()
@@ -36,29 +33,13 @@ func initPath(cfg *core.Config) error {
3633
}
3734

3835
func readPkg(pkg, path string, idx int) error {
39-
lookup, err := getLibrary(path)
40-
if err != nil {
41-
return err
42-
}
43-
44-
found := false
45-
for _, entry := range lookup {
46-
if pkg == entry.Name {
47-
found = true
48-
if err = download(pkg, entry.URL, path, idx); err != nil {
49-
return err
50-
}
36+
if core.IsPhrase(pkg) && !core.IsDir(pkg) {
37+
entry := inLibrary(pkg, path)
38+
if entry != "" {
39+
return download(pkg, entry, path, idx)
5140
}
5241
}
53-
54-
if !found {
55-
name := fileNameWithoutExt(pkg)
56-
if err = loadPkg(name, pkg, path, idx); err != nil {
57-
return err
58-
}
59-
}
60-
61-
return nil
42+
return loadPkg(fileNameWithoutExt(pkg), pkg, path, idx)
6243
}
6344

6445
func loadPkg(name, urlOrPath, styles string, index int) error {
@@ -96,7 +77,7 @@ func download(name, url, styles string, index int) error {
9677

9778
if err = fetch(url, dir); err != nil {
9879
if strings.Contains(err.Error(), "unsupported protocol scheme") {
99-
err = fmt.Errorf("'%s' is not a valid URL or the local file doesn't exist", url)
80+
err = fmt.Errorf("'%s' is not a valid URL or the directory doesn't exist", url)
10081
}
10182
return core.NewE100("download", err)
10283
}
@@ -194,16 +175,3 @@ func moveAsset(name, old, new string) error { //nolint:predeclared
194175

195176
return cp.Copy(src, dst)
196177
}
197-
198-
func getLibrary(_ string) ([]Style, error) {
199-
styles := []Style{}
200-
201-
resp, err := fetchJSON(library)
202-
if err != nil {
203-
return styles, err
204-
} else if err = json.Unmarshal(resp, &styles); err != nil {
205-
return styles, err
206-
}
207-
208-
return styles, err
209-
}

internal/core/util_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ func TestPrepText(t *testing.T) {
3838

3939
func TestPhrase(t *testing.T) {
4040
rawToPrepped := map[string]bool{
41-
"test suite": true,
42-
"test[ ]?suite": false,
41+
"test suite": true,
42+
"test[ ]?suite": false,
43+
"Google": true,
44+
"write-good": true,
45+
"https://vale.sh/explorer": false,
46+
"Google.zip": false,
4347
}
4448
for input, output := range rawToPrepped {
4549
result := IsPhrase(input)

0 commit comments

Comments
 (0)