Skip to content

Commit f313824

Browse files
chore: rename file and update ignorefile (#7)
1 parent a5a9cb6 commit f313824

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919

2020
# Go workspace file
2121
go.work
22+
23+
.idea/

render/chromedb.go renamed to render/chromedp.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7-
"github.com/chromedp/chromedp"
87
"log"
98
"os"
109
"path/filepath"
1110
"strings"
11+
12+
"github.com/chromedp/chromedp"
1213
)
1314

14-
const HTML = "html"
15-
const FileProtocol = "file://"
16-
const EchartsInstanceDom = "div[_echarts_instance_]"
17-
const CanvasJs = "echarts.getInstanceByDom(document.querySelector('div[_echarts_instance_]'))" +
18-
".getDataURL({type: '%s', pixelRatio: %d, excludeComponents: ['toolbox']})"
15+
const (
16+
HTML = "html"
17+
FileProtocol = "file://"
18+
EchartsInstanceDom = "div[_echarts_instance_]"
19+
CanvasJs = "echarts.getInstanceByDom(document.querySelector('div[_echarts_instance_]'))" +
20+
".getDataURL({type: '%s', pixelRatio: %d, excludeComponents: ['toolbox']})"
21+
)
1922

2023
type SnapshotConfig struct {
2124
// Renderer canvas or svg, not used for now
@@ -73,9 +76,7 @@ func MakeSnapshot(config *SnapshotConfig) error {
7376
htmlPath, _ = filepath.Abs(htmlPath)
7477
}
7578

76-
ctx, cancel := chromedp.NewContext(
77-
context.Background(),
78-
)
79+
ctx, cancel := chromedp.NewContext(context.Background())
7980
defer cancel()
8081

8182
htmlFullPath := filepath.Join(htmlPath, fileName+"."+HTML)
@@ -84,16 +85,12 @@ func MakeSnapshot(config *SnapshotConfig) error {
8485
defer func() {
8586
err := os.Remove(htmlFullPath)
8687
if err != nil {
87-
log.Printf("Failed to delete the file: %s\n", err)
88+
log.Printf("Failed to delete the file(%s), err: %s\n", htmlFullPath, err)
8889
}
8990
}()
9091
}
9192

92-
err := os.WriteFile(htmlFullPath, content, 0644)
93-
if err != nil {
94-
return err
95-
}
96-
93+
err := os.WriteFile(htmlFullPath, content, 0o644)
9794
if err != nil {
9895
return err
9996
}
@@ -102,15 +99,13 @@ func MakeSnapshot(config *SnapshotConfig) error {
10299
quality = 1
103100
}
104101

105-
executeJS := fmt.Sprintf(CanvasJs, suffix, quality)
106-
107102
var base64Data string
103+
executeJS := fmt.Sprintf(CanvasJs, suffix, quality)
108104
err = chromedp.Run(ctx,
109105
chromedp.Navigate(fmt.Sprintf("%s%s", FileProtocol, htmlFullPath)),
110106
chromedp.WaitVisible(EchartsInstanceDom, chromedp.ByQuery),
111107
chromedp.Evaluate(executeJS, &base64Data),
112108
)
113-
114109
if err != nil {
115110
return err
116111
}
@@ -120,10 +115,9 @@ func MakeSnapshot(config *SnapshotConfig) error {
120115
return err
121116
}
122117

123-
imageFullPath := fmt.Sprintf("%s/%s.%s", path, fileName, suffix)
124-
125-
if err := os.WriteFile(imageFullPath, imgContent, 0644); err != nil {
126-
log.Fatal(err)
118+
imageFullPath := filepath.Join(path, fmt.Sprintf("%s.%s", fileName, suffix))
119+
if err := os.WriteFile(imageFullPath, imgContent, 0o644); err != nil {
120+
return err
127121
}
128122

129123
log.Printf("Wrote %s.%s success", fileName, suffix)

render/chromedb_test.go renamed to render/chromedp_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@ package render
22

33
import (
44
_ "embed"
5-
"github.com/go-echarts/snapshot-chromedp/asset"
65
"os"
76
"testing"
7+
8+
"github.com/go-echarts/snapshot-chromedp/asset"
89
)
910

1011
// TODO: integration in ci
1112
func TestFileCreation(t *testing.T) {
12-
1313
fileName := "mock"
1414
fileImage := fileName + ".png"
1515
fileHtml := fileName + ".html"
1616

1717
err := MakeChartSnapshot(asset.RenderContent(), fileImage)
18-
1918
if err != nil {
2019
t.Fatalf("Failed to create file: %s", err)
2120
}
2221

23-
//defer os.Remove(fileImage)
22+
// defer os.Remove(fileImage)
2423

2524
_, err = os.Stat(fileImage)
2625
if os.IsNotExist(err) {

0 commit comments

Comments
 (0)