x
This commit is contained in:
+21
-4
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user