This commit is contained in:
2026-01-27 10:08:07 -05:00
parent c2f08e9140
commit de1dcaceef
5 changed files with 348 additions and 0 deletions

47
main.go
View File

@@ -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>")