This commit is contained in:
cdle
2023-06-08 09:15:35 +08:00
parent b20bbe0f7d
commit 6bb0027b22
3 changed files with 25 additions and 8 deletions
+21 -4
View File
@@ -2,10 +2,12 @@ package core
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"runtime"
iconv "github.com/djimenez/iconv-go"
"github.com/dop251/goja"
)
@@ -19,14 +21,29 @@ func getJsOs(vm *goja.Runtime, running func() bool) *goja.Object {
return data
})
jsos.Set("readFileSync", func(path string) string {
data, err := os.ReadFile(path)
content, err := ioutil.ReadFile(path)
if err != nil {
panic(Error(vm, err))
}
return string(data)
// 自动转换编码为UTF-8
utf8Content, err := iconv.ConvertString(string(content), "auto", "utf-8")
if err != nil {
panic(Error(vm, err))
}
return utf8Content
})
jsos.Set("writeFileSync", func(path, content string) {
err := os.WriteFile(path, []byte(content), 0644)
jsos.Set("writeFileSync", func(path, content string, encode string) {
// 将文本内容转换为GBK编码
var err error
if !(encode == "" || encode == "utf-8") {
content, err = iconv.ConvertString(content, "utf-8", encode)
if err != nil {
panic(Error(vm, err))
}
}
// 将文本内容以GBK编码写入文件
err = ioutil.WriteFile(path, []byte(content), 0644)
if err != nil {
panic(Error(vm, err))
}
+2 -2
View File
@@ -11,16 +11,17 @@ require (
github.com/boltdb/bolt v1.3.1
github.com/buger/jsonparser v1.1.1
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58
github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da
github.com/dop251/goja v0.0.0-20230427124612-428fc442ff5f
github.com/dop251/goja_nodejs v0.0.0-20230322100729-2550c7b6c124
github.com/elastic/go-elasticsearch/v6 v6.8.5
github.com/forPelevin/gomoji v1.1.8
github.com/gin-gonic/gin v1.8.1
github.com/go-redis/redis/v8 v8.11.5
github.com/goccy/go-json v0.9.7
github.com/gogo/protobuf v1.3.1
github.com/google/uuid v1.1.1
github.com/gorilla/websocket v1.5.0
github.com/rivo/uniseg v0.4.3
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.4.0
@@ -31,7 +32,6 @@ require (
github.com/google/pprof v0.0.0-20230510103437-eeec1cb781c3 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
+2 -2
View File
@@ -44,6 +44,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da h1:0qwwqQCLOOXPl58ljnq3sTJR7yRuMolM02vjxDh4ZVE=
github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da/go.mod h1:ns+zIWBBchgfRdxNgIJWn2x6U95LQchxeqiN5Cgdgts=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
@@ -62,8 +64,6 @@ github.com/elastic/go-elasticsearch/v6 v6.8.5/go.mod h1:UwaDJsD3rWLM5rKNFzv9hgox
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/forPelevin/gomoji v1.1.8 h1:JElzDdt0TyiUlecy6PfITDL6eGvIaxqYH1V52zrd0qQ=
github.com/forPelevin/gomoji v1.1.8/go.mod h1:8+Z3KNGkdslmeGZBC3tCrwMrcPy5GRzAD+gL9NAwMXg=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=