This commit is contained in:
Star 2024-10-11 12:02:48 +08:00
commit 6a20519361
7 changed files with 259 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.*
!.gitignore
go.sum

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2024 apigo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

47
README.md Normal file
View File

@ -0,0 +1,47 @@
# file for GoJS
## usage
```go
import (
"apigo.cc/gojs"
_ "apigo.cc/gojs/file"
)
func main() {
r, err := gojs.Run(`
import file from 'apigo.cc/gojs/file'
function main(args){
return file.read('test.txt')
}
`, "test.js")
fmt.Println(r, err)
}
```
## cache file for proformance
```javascript
import file from 'apigo.cc/gojs/file'
file.cache('test.txt', true)
file.cache('images/test.png')
```
## module.exports
```ts
function read(filename: string): string {return ''}
function readBytes(filename: string): any {return null}
function write(filename: string, data: any): void {}
function dir(filename: string): Array<FileInfo> {return null as any}
function stat(filename: string): FileInfo {return null as any}
function find(filename: string): string {return ''}
function remove(filename: string): void {}
function rename(from: string, to: string): void {}
function copy(from: string, to: string): void {}
function cache(filename: string, isCompress: boolean): void {}
```
## full api see [file.ts](https://apigo.cc/gojs/file/file.ts)

116
file.go Normal file
View File

@ -0,0 +1,116 @@
package file
import (
_ "embed"
"os"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
"github.com/ssgo/u"
)
//go:embed file.ts
var fileTS string
//go:embed README.md
var fileMD string
func init() {
obj := map[string]any{
"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 {
return vm.ToValue(r)
} else {
panic(vm.NewGoError(err))
}
},
"readBytes": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
if r, err := u.ReadFileBytes(args.Path(0)); err == nil {
return vm.ToValue(r)
} else {
panic(vm.NewGoError(err))
}
},
"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 {
return nil
} else {
panic(vm.NewGoError(err))
}
},
"dir": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
if r, err := u.ReadDir(args.Path(0)); err == nil {
list := make([]map[string]any, len(r))
for i, info := range r {
list[i] = makeFileInfo(&info)
}
return vm.ToValue(list)
} else {
panic(vm.NewGoError(err))
}
},
"stat": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(makeFileInfo(u.GetFileInfo(args.Path(0))))
},
"find": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(args.Path(0))
},
"remove": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
if err := os.RemoveAll(args.Path(0)); err == nil {
return nil
} else {
panic(vm.NewGoError(err))
}
},
"rename": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(2)
if err := os.Rename(args.Path(0), args.Path(1)); err == nil {
return nil
} else {
panic(vm.NewGoError(err))
}
},
"copy": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(2)
if err := u.CopyFile(args.Path(0), args.Path(1)); err == nil {
return nil
} else {
panic(vm.NewGoError(err))
}
},
"cache": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
compress := args.Bool(1)
if compress {
u.LoadFileToMemoryWithCompress(args.Path(0))
} else {
u.LoadFileToMemory(args.Path(0))
}
return nil
},
}
gojs.Register("apigo.cc/gojs/file", gojs.Module{
Object: obj,
Desc: "file api by https://github.com/ssgo/u",
TsCode: fileTS,
Example: fileMD,
})
}
func makeFileInfo(info *u.FileInfo) map[string]any {
return map[string]any{
"name": info.Name,
"size": info.Size,
"fullName": info.FullName,
"isDir": info.IsDir,
"modTime": info.ModTime.UnixMilli(),
}
}

33
file.ts Normal file
View File

@ -0,0 +1,33 @@
// just for develop
export default {
read,
readBytes,
write,
dir,
stat,
find,
remove,
rename,
copy,
cache
}
function read(filename: string): string {return ''}
function readBytes(filename: string): any {return null}
function write(filename: string, data: any): void {}
function dir(filename: string): Array<FileInfo> {return null as any}
function stat(filename: string): FileInfo {return null as any}
function find(filename: string): string {return ''}
function remove(filename: string): void {}
function rename(from: string, to: string): void {}
function copy(from: string, to: string): void {}
function cache(filename: string, isCompress: boolean): void {}
interface FileInfo {
name: string
fullName: string
isDir: boolean
size: number
modTime: number
}

29
file_test.go Normal file
View File

@ -0,0 +1,29 @@
package file_test
import (
"fmt"
"os"
"testing"
"apigo.cc/gojs"
_ "apigo.cc/gojs/file"
"github.com/ssgo/u"
)
func TestHash(t *testing.T) {
defer os.Remove("test.txt")
defer os.Remove("test2.txt")
r, err := gojs.Run(`
import file from 'apigo.cc/gojs/file'
file.write('test.txt', 'hello world')
file.rename('test.txt', 'test2.txt')
return file.read('test2.txt')
`, "")
if err != nil {
t.Fatal(err)
} else if r != "hello world" {
t.Fatal("read file error", r)
} else {
fmt.Println(u.BGreen("test succeess"))
}
}

22
go.mod Normal file
View File

@ -0,0 +1,22 @@
module apigo.cc/gojs/file
go 1.18
require (
apigo.cc/gojs v0.0.1
github.com/ssgo/u v1.7.9
)
require (
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/ssgo/config v1.7.7 // indirect
github.com/ssgo/log v1.7.7 // indirect
github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/tool v0.4.27 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)