ai_old/js/file.go

44 lines
1.2 KiB
Go
Raw Normal View History

2024-09-17 18:44:21 +08:00
package js
import (
"apigo.cc/ai/ai/goja"
2024-09-17 18:44:21 +08:00
"github.com/ssgo/u"
)
2024-09-18 18:29:21 +08:00
func RequireFile() map[string]any {
2024-09-17 18:44:21 +08:00
return map[string]any{
"read": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
if r, err := u.ReadFile(findPath(vm, args.Str(0))); err == nil {
2024-09-17 18:44:21 +08:00
return vm.ToValue(r)
} else {
2024-09-23 18:15:02 +08:00
panic(vm.NewGoError(err))
2024-09-17 18:44:21 +08:00
}
},
"write": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(2)
if err := u.WriteFileBytes(findPath(vm, args.Str(0)), u.Bytes(args.Any(0))); err == nil {
2024-09-17 18:44:21 +08:00
return nil
} else {
panic(vm.NewGoError(err))
2024-09-17 18:44:21 +08:00
}
},
"dir": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
if r, err := u.ReadDir(findPath(vm, args.Str(0))); err == nil {
2024-09-17 18:44:21 +08:00
return vm.ToValue(r)
} else {
panic(vm.NewGoError(err))
2024-09-17 18:44:21 +08:00
}
},
"stat": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(u.GetFileInfo(findPath(vm, args.Str(0))))
2024-09-23 18:15:02 +08:00
},
"find": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(findPath(vm, args.Str(0)))
2024-09-17 18:44:21 +08:00
},
}
}