update gojs, support userPath
This commit is contained in:
parent
2a7e21be35
commit
3a760442a7
27
file.go
27
file.go
@ -49,7 +49,8 @@ func init() {
|
||||
},
|
||||
"read": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||
if r, err := u.ReadFile(args.Path(0)); err == nil {
|
||||
filename := args.Path(0)
|
||||
if r, err := u.ReadFile(filename); err == nil {
|
||||
return vm.ToValue(r)
|
||||
} else {
|
||||
panic(vm.NewGoError(err))
|
||||
@ -65,7 +66,8 @@ func init() {
|
||||
},
|
||||
"write": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
||||
if err := u.WriteFileBytes(args.Path(0), args.Bytes(1)); err == nil {
|
||||
filename := args.Path(0)
|
||||
if err := u.WriteFileBytes(filename, args.Bytes(1)); err == nil {
|
||||
return nil
|
||||
} else {
|
||||
panic(vm.NewGoError(err))
|
||||
@ -131,7 +133,7 @@ func init() {
|
||||
searchPath := args.Str(1)
|
||||
findFile := ""
|
||||
if searchPath != "" {
|
||||
findFile = searchFile(searchPath, filename)
|
||||
findFile = searchFile(vm, searchPath, filename)
|
||||
} else {
|
||||
findFile = searchFileFromCurrent(vm, filename)
|
||||
}
|
||||
@ -145,7 +147,7 @@ func init() {
|
||||
envFile = searchFileFromCurrent(vm, "env.json")
|
||||
}
|
||||
if envFile != "" {
|
||||
u.LoadX(envFile, &envConfigs)
|
||||
u.LoadX(gojs.FixPath(vm, envFile), &envConfigs)
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,7 +159,7 @@ func init() {
|
||||
confFile = searchFileFromCurrent(vm, confName+".json")
|
||||
}
|
||||
if confFile != "" {
|
||||
u.LoadX(confFile, &conf)
|
||||
u.LoadX(gojs.FixPath(vm, confFile), &conf)
|
||||
}
|
||||
if envConf, ok := envConfigs[confName]; ok {
|
||||
u.Convert(envConf, &conf)
|
||||
@ -190,7 +192,7 @@ func init() {
|
||||
data, err = json.Marshal(args.Any(1))
|
||||
}
|
||||
if err == nil {
|
||||
if err = u.WriteFileBytes(gojs.FindPath(vm, filename), data); err != nil {
|
||||
if err = u.WriteFileBytes(gojs.FixPath(vm, filename), data); err != nil {
|
||||
panic(vm.NewGoError(err))
|
||||
}
|
||||
return nil
|
||||
@ -201,7 +203,7 @@ func init() {
|
||||
"load": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||
filename := args.Str(0)
|
||||
if data, err := u.ReadFileBytes(gojs.FindPath(vm, filename)); err == nil {
|
||||
if data, err := u.ReadFileBytes(gojs.FixPath(vm, filename)); err == nil {
|
||||
var r any
|
||||
if strings.HasSuffix(filename, ".yml") || strings.HasSuffix(filename, ".yaml") {
|
||||
err = yaml.Unmarshal(data, &r)
|
||||
@ -238,13 +240,22 @@ func makeFileInfo(info *u.FileInfo) map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
func searchFile(searchPath, name string) string {
|
||||
func searchFile(vm *goja.Runtime, searchPath, name string) string {
|
||||
searchPath = gojs.FixPath(vm, searchPath)
|
||||
|
||||
searched := map[string]bool{}
|
||||
return _searchFile(searchPath, name, &searched)
|
||||
}
|
||||
|
||||
func searchFileFromCurrent(vm *goja.Runtime, name string) string {
|
||||
searched := map[string]bool{}
|
||||
|
||||
// 使用 userPath 作为文件系统沙盒
|
||||
rootPath := u.String(vm.GetData("userPath"))
|
||||
if rootPath != "" {
|
||||
return _searchFile(rootPath, name, &searched)
|
||||
}
|
||||
|
||||
startPath := u.String(vm.GetData("startPath"))
|
||||
filename := _searchFile(startPath, name, &searched)
|
||||
if filename == "" {
|
||||
|
||||
43
file_test.go
43
file_test.go
@ -3,14 +3,16 @@ package file_test
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"apigo.cc/gojs"
|
||||
_ "apigo.cc/gojs/console"
|
||||
_ "apigo.cc/gojs/file"
|
||||
"github.com/ssgo/u"
|
||||
)
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
func TestWrite(t *testing.T) {
|
||||
defer os.Remove("test.txt")
|
||||
defer os.Remove("test2.txt")
|
||||
r, err := gojs.Run(`
|
||||
@ -24,7 +26,7 @@ func TestHash(t *testing.T) {
|
||||
} else if r != "hello world" {
|
||||
t.Fatal("read file error", r)
|
||||
} else {
|
||||
fmt.Println(u.BGreen("test succeess"))
|
||||
fmt.Println(u.BGreen("write test succeess"))
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,5 +45,40 @@ func TestConfig(t *testing.T) {
|
||||
if r != true {
|
||||
t.Fatal(r)
|
||||
}
|
||||
fmt.Println(u.Green("ecdsa test passed"))
|
||||
fmt.Println(u.BGreen("loadConfig test passed"))
|
||||
}
|
||||
|
||||
func TestUserPath(t *testing.T) {
|
||||
defer os.RemoveAll("aaa")
|
||||
defer os.RemoveAll("bbb")
|
||||
pwd, _ := os.Getwd()
|
||||
vm := gojs.New()
|
||||
vm.SetUserPath("bbb")
|
||||
_, err := vm.RunCode(`
|
||||
import file from 'apigo.cc/gojs/file'
|
||||
import co from 'apigo.cc/gojs/console'
|
||||
let aaa = ''
|
||||
let bbb = ''
|
||||
try {
|
||||
file.write('aaa/test.txt', 'hello world1')
|
||||
aaa = file.read('aaa/test.txt')
|
||||
} catch (e) {
|
||||
co.error(e)
|
||||
}
|
||||
try {
|
||||
file.write('bbb/test.txt', 'hello world2')
|
||||
bbb = file.read('bbb/test.txt')
|
||||
} catch (e) {
|
||||
co.error(e)
|
||||
}
|
||||
`)
|
||||
aaa := u.ReadFileN(filepath.Join(pwd, "aaa", "test.txt"))
|
||||
bbb := u.ReadFileN(filepath.Join(pwd, "bbb", "test.txt"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if aaa+bbb != "hello world2" {
|
||||
t.Fatal("read file error", aaa+bbb)
|
||||
} else {
|
||||
fmt.Println(u.BGreen("userPath test succeess"))
|
||||
}
|
||||
}
|
||||
|
||||
15
go.mod
15
go.mod
@ -1,10 +1,11 @@
|
||||
module apigo.cc/gojs/file
|
||||
|
||||
go 1.24
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
apigo.cc/gojs v0.0.25
|
||||
github.com/ssgo/u v1.7.21
|
||||
apigo.cc/gojs v0.0.28
|
||||
apigo.cc/gojs/console v0.0.2
|
||||
github.com/ssgo/u v1.7.23
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@ -12,11 +13,11 @@ require (
|
||||
github.com/dlclark/regexp2 v1.11.5 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
||||
github.com/google/pprof v0.0.0-20250630185457-6e76a2b096b5 // indirect
|
||||
github.com/ssgo/config v1.7.9 // indirect
|
||||
github.com/google/pprof v0.0.0-20250903194437-c28834ac2320 // indirect
|
||||
github.com/ssgo/config v1.7.10 // indirect
|
||||
github.com/ssgo/log v1.7.9 // indirect
|
||||
github.com/ssgo/standard v1.7.7 // indirect
|
||||
github.com/ssgo/tool v0.4.29 // indirect
|
||||
golang.org/x/sys v0.34.0 // indirect
|
||||
golang.org/x/text v0.27.0 // indirect
|
||||
golang.org/x/sys v0.38.0 // indirect
|
||||
golang.org/x/text v0.31.0 // indirect
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user