diff --git a/README.md b/README.md index 14aafb4..7f9fd92 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ One parser. One IR. Zero lock-in. [![Terraform](https://img.shields.io/badge/Terraform-1.0_%E2%80%93_1.17-7B42BC?style=flat-square&logo=terraform&logoColor=white)](docs/TERRAFORM-VERSIONS.md) [![AWS CDK](https://img.shields.io/badge/AWS_CDK-TypeScript-FF9900?style=flat-square&logo=amazonaws&logoColor=white)](https://aws.amazon.com/cdk/) [![Pulumi](https://img.shields.io/badge/Pulumi-TypeScript-8A3391?style=flat-square&logo=pulumi&logoColor=white)](https://www.pulumi.com/) -[![Cloudflare Workers](https://img.shields.io/badge/Cloudflare-Workers-F38020?style=flat-square&logo=cloudflare&logoColor=white)](https://openiac.cwz.workers.dev) +[![Cloudflare Workers](https://img.shields.io/badge/Cloudflare-Workers-F38020?style=flat-square&logo=cloudflare&logoColor=white)](https://openiac.codewithzeal.com) [![IR](https://img.shields.io/badge/IR-schema_0.2-818cf8?style=flat-square)](docs/IR.md) [![Made with Go](https://img.shields.io/badge/made_with-Go-00ADD8?style=flat-square&logo=go&logoColor=white)](https://go.dev) @@ -28,7 +28,25 @@ One parser. One IR. Zero lock-in.
-[🚀 Playground](https://openiac.cwz.workers.dev) · [📦 Quick Start](#-quick-start) · [🗺️ Roadmap](docs/ROADMAP.md) · [🧠 How It Works](#-how-it-works) · [🧱 Architecture](docs/ARCHITECTURE.md) · [📄 IR Reference](docs/IR.md) · [🧭 Version Support](docs/TERRAFORM-VERSIONS.md) · [🧪 Conformance](docs/CONFORMANCE.md) · [🤝 Contribute](CONTRIBUTING.md) +[🚀 Live demo · open beta](https://openiac.codewithzeal.com) · [📦 Quick Start](#-quick-start) · [🗺️ Roadmap](docs/ROADMAP.md) · [🧠 How It Works](#-how-it-works) · [🧱 Architecture](docs/ARCHITECTURE.md) · [📄 IR Reference](docs/IR.md) · [🧭 Version Support](docs/TERRAFORM-VERSIONS.md) · [🧪 Conformance](docs/CONFORMANCE.md) · [🤝 Contribute](CONTRIBUTING.md) + +
+ +### ⚡ Open beta — try it right now + +[![Try the live playground](https://img.shields.io/badge/%E2%96%B6%20TRY%20THE%20LIVE%20PLAYGROUND-OPEN%20BETA-22d3ee?style=for-the-badge&logo=cloudflare&logoColor=0b0f17)](https://openiac.codewithzeal.com) +[![Runs in your browser](https://img.shields.io/badge/WASM-runs_in_your_browser-654FF0?style=for-the-badge&logo=webassembly&logoColor=white)](https://openiac.codewithzeal.com) +[![No install](https://img.shields.io/badge/no_install-no_signup-34d399?style=for-the-badge)](https://openiac.codewithzeal.com) + + + OpenIaC playground converting Terraform HCL to AWS CDK TypeScript in the browser + + +No install, no account, nothing leaves your tab. + +

+ +▶ Click here to get open beta access @@ -150,12 +168,17 @@ const mainVpc = new aws.ec2.Vpc("mainVpc", { ### 🖥️ Browser playground +Live at **[openiac.codewithzeal.com](https://openiac.codewithzeal.com)** — open +beta, no account. To run it yourself: + ```bash make wasm # builds main.wasm + wasm_exec.js python3 -m http.server -d playground 8080 # serve it ``` Open `http://localhost:8080`, paste Terraform, get TypeScript. **No backend.** +Press **View IR** in the output pane to inspect the intermediate representation +both emitters share; **Hide IR** returns to the generated TypeScript. --- diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 45e8245..6b42bbc 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -31,7 +31,7 @@ writing one package, not a new frontend. | [`pkg/emitters/awscdk`](../pkg/emitters/awscdk/) | `aws-cdk-lib` TypeScript, L1 and L2 constructs. | | [`pkg/emitters/pulumi`](../pkg/emitters/pulumi/) | `@pulumi/aws` TypeScript. | | [`pkg/engine`](../pkg/engine/) | Public API: parse, convert, marshal, target parsing. | -| [`wasm`](../wasm/) + [`playground`](../playground/) | Browser build and UI. | +| [`wasm`](../wasm/) + [`playground`](../playground/) | Browser build and UI. The WebAssembly module exposes `openiacTranspile` and `openiacIR`; the UI renders either the generated TypeScript or the IR JSON from the same graph. | ## The IR contract diff --git a/docs/assets/playground.png b/docs/assets/playground.png new file mode 100644 index 0000000..d3597b5 Binary files /dev/null and b/docs/assets/playground.png differ diff --git a/playground/app.js b/playground/app.js index 5b8b6e5..e7750dd 100644 --- a/playground/app.js +++ b/playground/app.js @@ -6,6 +6,8 @@ const state = { target: 'cdk', + view: 'code', + monaco: null, input: null, output: null, debounce: 0, @@ -53,6 +55,11 @@ if (Date.now() > deadline) throw new Error('WebAssembly runtime did not start.'); await new Promise((resolve) => setTimeout(resolve, 25)); } + // The converter and the IR toggle read separate exports, so a build with + // only the first would leave the pane showing the other view's result. + if (typeof window.openiacIR !== 'function') { + throw new Error('This WebAssembly build predates the IR view. Run `make wasm` and reload the page.'); + } } /* ---------------- monaco setup ---------------- */ @@ -140,6 +147,7 @@ } function createEditors(monaco) { + state.monaco = monaco; const common = { theme: 'openiac', automaticLayout: true, @@ -182,8 +190,42 @@ /* ---------------- conversion ---------------- */ + function targetLabel() { + return state.target === 'cdk' ? 'AWS CDK' : 'Pulumi'; + } + + // Shared by both views so the numbers describe the same graph. Data sources + // only appear when the input has them, which keeps the common case short. + function statsLine(result) { + const parts = [`${result.resources || 0} resources`]; + if (result.data) parts.push(`${result.data} data`); + parts.push(`${result.variables || 0} vars`, `${result.outputs || 0} outputs`, `${result.edges || 0} edges`); + return parts.join(' · '); + } + + // The output pane shows either generated TypeScript or the IR, so the editor + // language, file name and download name follow the active view. + function updateOutputLanguage() { + const model = state.output && state.output.getModel(); + if (!model) return; + state.monaco.editor.setModelLanguage(model, state.view === 'ir' ? 'json' : 'typescript'); + } + + function setView(view) { + state.view = view; + const irButton = $('ir-button'); + irButton.classList.toggle('is-active', view === 'ir'); + irButton.setAttribute('aria-pressed', view === 'ir' ? 'true' : 'false'); + irButton.title = view === 'ir' ? 'Back to the generated TypeScript' : 'Show the universal IR (JSON) both emitters share'; + $('ir-button-label').textContent = view === 'ir' ? 'Hide IR' : 'View IR'; + $('output-file').textContent = view === 'ir' ? 'ir.json' : 'index.ts'; + $('target-badge').textContent = view === 'ir' ? 'IR' : targetLabel(); + updateOutputLanguage(); + convert(); + } + function convert() { - if (!state.input || typeof window.openiacTranspile !== 'function') return; + if (!state.input) return; const source = state.input.getValue(); if (!source.trim()) { @@ -193,6 +235,13 @@ return; } + if (state.view === 'ir') { + convertIR(source); + return; + } + + if (typeof window.openiacTranspile !== 'function') return; + let result; try { result = JSON.parse(window.openiacTranspile(source, state.target)); @@ -213,20 +262,50 @@ const warnings = result.warnings || []; renderWarnings(warnings); - const stats = [ - `${result.resources || 0} resources`, - `${result.variables || 0} vars`, - `${result.outputs || 0} outputs`, - `${result.edges || 0} edges`, - ].join(' · '); - setStatus('ok', `Converted to ${state.target === 'cdk' ? 'AWS CDK' : 'Pulumi'}.`, `${stats} · ${warnings.length} warnings`); + const stats = statsLine(result); + setStatus('ok', `Converted to ${targetLabel()}.`, `${stats} · ${warnings.length} warnings`); + } + + function convertIR(source) { + if (typeof window.openiacIR !== 'function') { + fail(new Error('This WebAssembly build has no IR export. Run `make wasm` and reload the page.')); + return; + } + + let result; + try { + result = JSON.parse(window.openiacIR(source)); + } catch (error) { + fail(error); + return; + } + if (!result.ok) { + fail(new Error(result.error || 'IR generation failed.')); + return; + } + + let schema = ''; + try { + schema = JSON.parse(result.ir).version || ''; + } catch (_) { + schema = ''; + } + + state.output.setValue(result.ir || ''); + renderWarnings([]); + + const stats = statsLine(result); + setStatus('ok', 'Generated the universal IR.', schema ? `${stats} · schema ${schema}` : stats); } function fail(error) { console.error(error); - state.output.setValue(`// OpenIaC could not convert this input.\n// ${String(error.message || error).replace(/\n/g, '\n// ')}`); + const message = String(error.message || error); + state.output.setValue(state.view === 'ir' + ? JSON.stringify({ error: message }, null, 2) + : `// OpenIaC could not convert this input.\n// ${message.replace(/\n/g, '\n// ')}`); renderWarnings([]); - setStatus('error', String(error.message || error), ''); + setStatus('error', message, ''); } function setStatus(kind, text, stats) { @@ -262,11 +341,16 @@ document.querySelectorAll('.segment').forEach((other) => { other.classList.toggle('is-active', other === button); }); - $('target-badge').textContent = state.target === 'cdk' ? 'AWS CDK' : 'Pulumi'; - convert(); + setView('code'); }); }); + // The IR stays hidden until this button is pressed, then toggles back to + // the generated TypeScript. + $('ir-button').addEventListener('click', () => { + setView(state.view === 'ir' ? 'code' : 'ir'); + }); + $('run-button').addEventListener('click', convert); $('copy-button').addEventListener('click', async () => { @@ -283,11 +367,12 @@ $('download-button').addEventListener('click', () => { const text = state.output.getValue(); if (!text) return; - const blob = new Blob([text], { type: 'text/typescript' }); + const ir = state.view === 'ir'; + const blob = new Blob([text], { type: ir ? 'application/json' : 'text/typescript' }); const url = URL.createObjectURL(blob); const anchor = document.createElement('a'); anchor.href = url; - anchor.download = `openiac-${state.target}.ts`; + anchor.download = ir ? 'openiac.ir.json' : `openiac-${state.target}.ts`; anchor.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); }); diff --git a/playground/index.html b/playground/index.html index c006b0a..4a7ff58 100644 --- a/playground/index.html +++ b/playground/index.html @@ -60,11 +60,15 @@

Escape the HCL lock-in.

- index.ts + index.ts AWS CDK - - + + +
diff --git a/playground/styles.css b/playground/styles.css index 4322c5a..d7ddbea 100644 --- a/playground/styles.css +++ b/playground/styles.css @@ -279,6 +279,58 @@ a { color: inherit; } border-color: rgba(34, 211, 238, 0.45); } +/* The IR toggle is the one thing in this bar that changes what the pane + shows, so it gets the accent treatment instead of the muted icon-button. */ +.ir-button { + display: inline-flex; + align-items: center; + gap: 7px; + margin-left: 4px; + padding: 6px 13px; + border: 1px solid rgba(129, 140, 248, 0.5); + border-radius: 8px; + background: linear-gradient(135deg, rgba(34, 211, 238, 0.18), rgba(129, 140, 248, 0.24)); + color: #d6fbff; + font: 700 12px var(--sans); + letter-spacing: 0.02em; + cursor: pointer; + animation: ir-glow 2.8s ease-in-out infinite; + transition: color 160ms ease, border-color 160ms ease, box-shadow 160ms ease, background 160ms ease; +} + +.ir-button:hover { + color: #ecfeff; + border-color: rgba(34, 211, 238, 0.8); + box-shadow: 0 0 18px rgba(34, 211, 238, 0.3); +} + +.ir-button:focus-visible { + outline: 2px solid var(--accent); + outline-offset: 2px; +} + +.ir-button.is-active { + color: #06121a; + border-color: transparent; + background: linear-gradient(135deg, var(--accent), var(--accent-2)); + box-shadow: 0 2px 18px rgba(34, 211, 238, 0.4); + animation: none; +} + +.ir-button-icon { + font: 700 11px var(--mono); + opacity: 0.85; +} + +@keyframes ir-glow { + 0%, 100% { box-shadow: 0 0 0 0 rgba(34, 211, 238, 0.26); } + 50% { box-shadow: 0 0 16px 2px rgba(34, 211, 238, 0.26); } +} + +@media (prefers-reduced-motion: reduce) { + .ir-button { animation: none; } +} + .editor { flex: 1; min-height: 0; @@ -413,3 +465,10 @@ a { color: inherit; } .hero { margin-top: 0; } .status-stats { display: none; } } + +@media (max-width: 640px) { + /* Keep the pane header on one line: the glyph still marks the toggle. */ + #ir-button-label { display: none; } + .ir-button { padding: 5px 9px; } + .icon-button { padding: 4px 7px; } +} diff --git a/wasm/main.go b/wasm/main.go index 7550e87..3300ca2 100644 --- a/wasm/main.go +++ b/wasm/main.go @@ -29,8 +29,22 @@ type file struct { Content string `json:"content"` } +// irResponse carries the parsed graph as JSON text, so the playground can show +// the IR without importing a TypeScript emitter. +type irResponse struct { + OK bool `json:"ok"` + Error string `json:"error,omitempty"` + IR string `json:"ir,omitempty"` + Resources int `json:"resources,omitempty"` + Data int `json:"data,omitempty"` + Variables int `json:"variables,omitempty"` + Outputs int `json:"outputs,omitempty"` + Edges int `json:"edges,omitempty"` +} + func main() { js.Global().Set("openiacTranspile", js.FuncOf(transpile)) + js.Global().Set("openiacIR", js.FuncOf(parseIR)) <-make(chan struct{}) } @@ -80,7 +94,37 @@ func transpile(_ js.Value, args []js.Value) any { return encode(out) } -func encode(value response) string { +// parseIR(source) returns the JSON IR for source as a string field, using the +// same parser and schema version the CLI prints with `openiac ir`. +func parseIR(_ js.Value, args []js.Value) any { + if len(args) == 0 || args[0].Type() != js.TypeString { + return encode(irResponse{Error: "missing HCL source"}) + } + graph, err := engine.ParseSource(args[0].String(), "playground.tf") + if err != nil { + return encode(irResponse{Error: err.Error()}) + } + if graph == nil { + return encode(irResponse{Error: "no graph produced"}) + } + data, err := engine.MarshalIR(graph) + if err != nil { + return encode(irResponse{Error: err.Error()}) + } + + resources, dataSources, variables, outputs, edges := graph.Stats() + return encode(irResponse{ + OK: true, + IR: string(data), + Resources: resources, + Data: dataSources, + Variables: variables, + Outputs: outputs, + Edges: edges, + }) +} + +func encode(value any) string { data, err := json.Marshal(value) if err != nil { return `{"ok":false,"error":"failed to encode response"}`