This commit is contained in:
Star 2024-10-11 10:14:56 +08:00
commit 7f89e9bfdb
7 changed files with 350 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.

61
README.md Normal file
View File

@ -0,0 +1,61 @@
# util for GoJS
## usage
```go
import (
"apigo.cc/gojs"
_ "apigo.cc/gojs/util"
)
func main() {
r, err := gojs.Run(`
import util from 'apigo.cc/gojs/util'
function main(args){
return util.sha512('hello 123')
}
`, "test.js")
fmt.Println(r, err, r == "27d0d415a3b14bdeb5ab5b2298ed3aed272361688927250eaef201603b84dd8bebb57cae089d6b5f1e9aee122a67917ccdf8a9ba5e499c64b35e133c7f6b007a")
}
```
## module.exports
```ts
function json(data:any): string {return ''}
function jsonP(data:any): string {return ''}
function unJson(data:string): any {return null}
function yaml(data:any): string {return ''}
function unYaml(data:string): any {return null}
function load(filename:string): any {return null}
function save(filename:string, data:any) {}
function base64(data:any): string {return ''}
function unBase64(data:string): any {return null}
function urlBase64(data:any): string {return ''}
function unUrlBase64(data:string): any {return null}
function hex(data:any): string {return ''}
function unHex(data:string): any {return null}
function aes(data:any, key:string, iv:string): string {return ''}
function unAes(data:string, key:string, iv:string): any {return null}
function gzip(data:any): string {return ''}
function gunzip(data:string): any {return null}
function id(): string {return ''}
function uniqueId(): string {return ''}
function token(size:number): string {return ''}
function md5(data:any): string {return ''}
function sha1(data:any): string {return ''}
function sha256(data:any): string {return ''}
function sha512(data:any): string {return ''}
function tpl(text:string, data:any, functions?:Object): string {return ''}
function sleep(ms:number): void {}
function setTimeout(callback:()=>void, ms?:number, ...args:any): void {}
function shell(cmd:string, ...args:string[]): string[] {return []}
function toDatetime(timestamp:number): string {return ''}
function fromDatetime(datetimeStr:string): number {return 0}
function toDate(timestamp:number): string {return ''}
function fromDate(dateStr:string): number {return 0}
```
## full api see [util.ts](http://apigo.cc/gojs/util/util.ts)

182
console.go Normal file
View File

@ -0,0 +1,182 @@
package console
import (
_ "embed"
"fmt"
"reflect"
"strings"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
"github.com/ssgo/u"
)
//go:embed console.ts
var consoleTS string
func init() {
obj := map[string]any{
"print": func(args goja.FunctionCall) goja.Value {
consolePrint(args, "print", nil)
return nil
},
"println": func(args goja.FunctionCall) goja.Value {
consolePrint(args, "log", nil)
return nil
},
"log": func(args goja.FunctionCall) goja.Value {
consolePrint(args, "log", nil)
return nil
},
"info": func(args goja.FunctionCall) goja.Value {
consolePrint(args, "info", nil)
return nil
},
"warn": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
consolePrint(args, "warn", vm)
return nil
},
"error": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
consolePrint(args, "error", vm)
return nil
},
"debug": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
consolePrint(args, "debug", vm)
return nil
},
"input": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
consolePrint(args, "print", nil)
line := ""
_, _ = fmt.Scanln(&line)
return vm.ToValue(line)
},
"black": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Black(fmt.Sprint(args.Array(0)...)))
},
"red": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Red(fmt.Sprint(args.Array(0)...)))
},
"green": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Green(fmt.Sprint(args.Array(0)...)))
},
"yellow": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Yellow(fmt.Sprint(args.Array(0)...)))
},
"blue": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Blue(fmt.Sprint(args.Array(0)...)))
},
"magenta": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Magenta(fmt.Sprint(args.Array(0)...)))
},
"cyan": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Cyan(fmt.Sprint(args.Array(0)...)))
},
"white": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.White(fmt.Sprint(args.Array(0)...)))
},
"dim": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Dim(fmt.Sprint(args.Array(0)...)))
},
"italic": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Italic(fmt.Sprint(args.Array(0)...)))
},
"bBlack": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BBlack(fmt.Sprint(args.Array(0)...)))
},
"bRed": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BRed(fmt.Sprint(args.Array(0)...)))
},
"bGreen": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BGreen(fmt.Sprint(args.Array(0)...)))
},
"bYellow": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BYellow(fmt.Sprint(args.Array(0)...)))
},
"bBlue": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BBlue(fmt.Sprint(args.Array(0)...)))
},
"bMagenta": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BMagenta(fmt.Sprint(args.Array(0)...)))
},
"bCyan": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BCyan(fmt.Sprint(args.Array(0)...)))
},
"bWhite": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BWhite(fmt.Sprint(args.Array(0)...)))
},
}
gojs.Register("apigo.cc/gojs/console", gojs.Module{
Object: obj,
Desc: "console api referer by nodejs console",
TsCode: consoleTS,
Example: "",
})
}
func consolePrint(args goja.FunctionCall, typ string, vm *goja.Runtime) {
arr := make([]any, len(args.Arguments))
textColor := u.TextNone
bgColor := u.BgNone
switch typ {
case "info":
textColor = u.TextCyan
bgColor = u.BgCyan
case "warn":
textColor = u.TextYellow
bgColor = u.BgYellow
case "error":
textColor = u.TextRed
bgColor = u.BgRed
}
if len(args.Arguments) == 1 && args.Argument(0).ExportType().Kind() == reflect.Map {
ex := args.Argument(0).ToObject(vm)
message := ex.Get("message")
stack := ex.Get("stack")
if message != nil && stack != nil {
fmt.Println(u.Color(u.String(message.Export()), textColor, u.BgNone))
fmt.Println(u.Color(u.String(stack.Export()), u.TextWhite, bgColor))
return
}
}
for i, arg := range args.Arguments {
if textColor != u.TextNone {
arr[i] = u.Color(u.StringP(arg.Export()), textColor, u.BgNone)
} else {
arr[i] = u.StringP(arg.Export())
}
}
if (typ == "warn" || typ == "error" || typ == "debug") && vm != nil {
callStacks := make([]string, 0)
for _, stack := range vm.CaptureCallStack(0, nil) {
callStacks = append(callStacks, u.Color(" "+stack.Position().String(), u.TextWhite, bgColor))
}
fmt.Println(arr...)
fmt.Println(strings.Join(callStacks, "\n"))
} else if typ == "print" {
fmt.Print(arr...)
} else {
fmt.Println(arr...)
}
}

58
console.ts Normal file
View File

@ -0,0 +1,58 @@
// just for develop
export default {
print,
println,
log,
debug,
info,
warn,
error,
input,
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
dim,
italic,
bBlack,
bRed,
bGreen,
bYellow,
bBlue,
bMagenta,
bCyan,
bWhite,
}
function print(...data: any[]): void {}
function println(...data: any[]): void {}
function log(...data: any[]): void {}
function debug(...data: any[]): void {}
function info(...data: any[]): void {}
function warn(...data: any[]): void {}
function error(...data: any[]): void {}
function input(...data: any[]): string {return ''}
function black(...data: any[]): string {return ''}
function red(...data: any[]): string {return ''}
function green(...data: any[]): string {return ''}
function yellow(...data: any[]): string {return ''}
function blue(...data: any[]): string {return ''}
function magenta(...data: any[]): string {return ''}
function cyan(...data: any[]): string {return ''}
function white(...data: any[]): string {return ''}
function dim(...data: any[]): string {return ''}
function italic(...data: any[]): string {return ''}
function bBlack(...data: any[]): string {return ''}
function bRed(...data: any[]): string {return ''}
function bGreen(...data: any[]): string {return ''}
function bYellow(...data: any[]): string {return ''}
function bBlue(...data: any[]): string {return ''}
function bMagenta(...data: any[]): string {return ''}
function bCyan(...data: any[]): string {return ''}
function bWhite(...data: any[]): string {return ''}

15
console_test.go Normal file
View File

@ -0,0 +1,15 @@
package console_test
import (
"testing"
"apigo.cc/gojs"
_ "apigo.cc/gojs/console"
)
func TestPrint(t *testing.T) {
gojs.Run(`
import console from 'apigo.cc/gojs/console'
console.println(console.bCyan('Hello World!'))
`, "")
}

22
go.mod Normal file
View File

@ -0,0 +1,22 @@
module apigo.cc/gojs/console
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
)