Skip to content

Stevenmasley/wasm #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea
.idea

# build artifacts
build/
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
GO_SRC_FILES := $(shell find . $(FIND_EXCLUSIONS) -type f -name '*.go' -not -name '*_test.go')

.PHONY: gen
gen:
@echo "Generating code..."
go generate ./...

.PHONY: clean-testdata
clean-testdata:
git clean -xfd testdata
git clean -xfd testdata

.PHONY: build-wasm
build-wasm: site/public/build/preview.wasm
mkdir -p ./build

site/public/build/preview.wasm: $(GO_SRC_FILES)
GOOS=js GOARCH=wasm go build -o ./build/preview.wasm ./cmd/wasm
87 changes: 87 additions & 0 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//go:build js && wasm

package main

import (
"context"
"encoding/json"
"fmt"
"io/fs"
"path/filepath"
"syscall/js"

"github.com/spf13/afero"

"github.com/coder/preview"
"github.com/coder/preview/types"
)

func main() {
// Create a channel to keep the Go program alive
done := make(chan struct{})

// Expose the Go function `fibonacciSum` to JavaScript
js.Global().Set("go_preview", js.FuncOf(tfpreview))
js.Global()

// Block the program from exiting
<-done
}

func tfpreview(this js.Value, p []js.Value) any {
tf, err := fileTreeFS(p[0])
if err != nil {
return err
}

output, diags := preview.Preview(context.Background(), preview.Input{
PlanJSONPath: "",
PlanJSON: nil,
ParameterValues: nil,
Owner: types.WorkspaceOwner{},
}, tf)

data, _ := json.Marshal(map[string]any{
"output": output,
"diags": diags,
})
return js.ValueOf(string(data))
}

func fileTreeFS(value js.Value) (fs.FS, error) {
data := js.Global().Get("JSON").Call("stringify", value).String()
var filetree map[string]any
if err := json.Unmarshal([]byte(data), &filetree); err != nil {
return nil, err
}

mem := afero.NewMemMapFs()
loadTree(mem, filetree)

return afero.NewIOFS(mem), nil
}

func loadTree(mem afero.Fs, fileTree map[string]any, path ...string) {
dir := filepath.Join(path...)
err := mem.MkdirAll(dir, 0755)
if err != nil {
fmt.Printf("error creating directory %q: %v\n", dir, err)
}
for k, v := range fileTree {
switch vv := v.(type) {
case string:
fn := filepath.Join(dir, k)
f, err := mem.Create(fn)
if err != nil {
fmt.Printf("error creating file %q: %v\n", fn, err)
continue
}
_, _ = f.WriteString(vv)
f.Close()
case map[string]any:
loadTree(mem, vv, append(path, k)...)
default:
fmt.Printf("unknown type %T for %q\n", v, k)
}
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.36.1
github.com/jedib0t/go-pretty/v6 v6.6.7
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/spf13/afero v1.12.0
github.com/stretchr/testify v1.10.0
github.com/zclconf/go-cty v1.16.3
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,8 @@ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasO
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spiffe/go-spiffe/v2 v2.5.0 h1:N2I01KCUkv1FAjZXJMwh95KK1ZIQLYbPfhaxw8WS0hE=
Expand Down
24 changes: 0 additions & 24 deletions site/.gitignore

This file was deleted.

31 changes: 0 additions & 31 deletions site/.vite/deps/_metadata.json

This file was deleted.

Loading
Loading