From 4c14d0484cf1eaa0246d84887d92c0fa39e43d1d Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Tue, 18 Oct 2022 15:29:47 +0200 Subject: [PATCH] Expose PrepareScript in WASM build (#48) --- examples/web/index.html | 1 + internal/runner/shell.go | 4 ++++ web/main_js.go | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/examples/web/index.html b/examples/web/index.html index 9e834a970..abd0bc1ba 100644 --- a/examples/web/index.html +++ b/examples/web/index.html @@ -22,6 +22,7 @@ go.run(runme.instance); console.log(GetSnippets(markdown)); console.log(GetDocument(markdown)); + console.log(PrepareScript(["bash -c 'echo \"1\"'"])); }); diff --git a/internal/runner/shell.go b/internal/runner/shell.go index 01123e5d1..e05b7b2cf 100644 --- a/internal/runner/shell.go +++ b/internal/runner/shell.go @@ -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 diff --git a/web/main_js.go b/web/main_js.go index 0fbe85d2f..e67e1b12a 100644 --- a/web/main_js.go +++ b/web/main_js.go @@ -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. @@ -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 {} } @@ -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) +}