Skip to content

Commit

Permalink
Expose PrepareScript in WASM build (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik authored Oct 18, 2022
1 parent 59c2c9a commit 4c14d04
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
go.run(runme.instance);
console.log(GetSnippets(markdown));
console.log(GetDocument(markdown));
console.log(PrepareScript(["bash -c 'echo \"1\"'"]));
});
</script>
</head>
Expand Down
4 changes: 4 additions & 0 deletions internal/runner/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (s *Shell) Run(ctx context.Context) error {
return execSingle(ctx, sh, s.Dir, prepareScript(s.Cmds), s.Stdin, s.Stdout, s.Stderr)
}

func PrepareScript(cmds []string) string {
return prepareScript(cmds)
}

func prepareScript(cmds []string) string {
var b strings.Builder

Expand Down
12 changes: 12 additions & 0 deletions web/main_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stateful/runme/internal/document"
"github.com/stateful/runme/internal/renderer"
"github.com/stateful/runme/internal/runner"
)

// These are variables so that they can be set during the build time.
Expand All @@ -19,6 +20,7 @@ var (
func main() {
js.Global().Set("GetSnippets", js.FuncOf(GetBlocks))
js.Global().Set("GetDocument", js.FuncOf(GetDocument))
js.Global().Set("PrepareScript", js.FuncOf(PrepareScript))

select {}
}
Expand Down Expand Up @@ -59,3 +61,13 @@ func GetDocument(this js.Value, args []js.Value) interface{} {

return dynamic
}

func PrepareScript(this js.Value, args []js.Value) interface{} {
lines := args[0]
len := lines.Length()
scriptLines := make([]string, 0, len)
for i := 0; i < len; i++ {
scriptLines = append(scriptLines, lines.Index(i).String())
}
return runner.PrepareScript(scriptLines)
}

0 comments on commit 4c14d04

Please sign in to comment.