bi test
This commit is contained in:
47
main.go
47
main.go
@@ -34,6 +34,8 @@ func executeTypeScript(filePath string, stdout, stderr io.Writer) error {
|
||||
|
||||
vm := goja.New()
|
||||
|
||||
RegisterBuiltins(vm)
|
||||
|
||||
console := vm.NewObject()
|
||||
_ = vm.Set("console", console)
|
||||
|
||||
@@ -58,6 +60,51 @@ func executeTypeScript(filePath string, stdout, stderr io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func executeTypeScriptContent(tsContent string, stdout, stderr io.Writer) error {
|
||||
result := api.Transform(tsContent, api.TransformOptions{
|
||||
Loader: api.LoaderTS,
|
||||
Target: api.ES2020,
|
||||
Format: api.FormatIIFE,
|
||||
Sourcemap: api.SourceMapNone,
|
||||
TreeShaking: api.TreeShakingFalse,
|
||||
})
|
||||
|
||||
if len(result.Errors) > 0 {
|
||||
_, _ = fmt.Fprintf(stderr, "Transpilation errors:\n")
|
||||
for _, err := range result.Errors {
|
||||
_, _ = fmt.Fprintf(stderr, " %s\n", err.Text)
|
||||
}
|
||||
return fmt.Errorf("transpilation failed")
|
||||
}
|
||||
|
||||
vm := goja.New()
|
||||
|
||||
RegisterBuiltins(vm)
|
||||
|
||||
console := vm.NewObject()
|
||||
_ = vm.Set("console", console)
|
||||
|
||||
_ = console.Set("log", func(call goja.FunctionCall) goja.Value {
|
||||
args := call.Arguments
|
||||
for i, arg := range args {
|
||||
if i > 0 {
|
||||
_, _ = fmt.Fprint(stdout, " ")
|
||||
}
|
||||
_, _ = fmt.Fprint(stdout, arg.String())
|
||||
}
|
||||
_, _ = fmt.Fprintln(stdout)
|
||||
return goja.Undefined()
|
||||
})
|
||||
|
||||
_, err := vm.RunString(string(result.Code))
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(stderr, "Execution error: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: program <typescript-file>")
|
||||
|
||||
Reference in New Issue
Block a user