Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

snelusha/balrun

Open more actions menu

Repository files navigation

banner

Warning

This project is actively evolving, and APIs may change frequently.

Installation

npm install @snelusha/balrun

CLI

npx @snelusha/balrun ./main.bal

Accepts a .bal file, a package directory, or . for the current package.

Usage

import { Ballerina } from "@snelusha/balrun";

const ballerina = new Ballerina({ colors: false });

// Returns null on success, or { error: "..." } on failure
const result = await ballerina.run("./main.bal", { colors: true });

Options passed to run() override the constructor defaults for that call only.

React

import { useBallerina } from "@snelusha/balrun/react";

function RunButton() {
	const { isReady, error, run } = useBallerina({
		fs: new SomeFS(), // Required in browser environments
	});

	return (
		<button disabled={!isReady} onClick={() => run("./main.bal")}>
			{error ? error.message : "Run Ballerina"}
		</button>
	);
}

useBallerina() accepts the same options as Ballerina and initializes the runtime.

See examples/vite for a Vite + React browser example.

Options

colors

Diagnostics use ANSI colors by default. Pass colors: false to disable. The CLI auto-detects based on stderr.isTTY.

stdout / stderr

Redirect runtime output by passing any object that implements StreamWriter:

import type { StreamWriter } from "@snelusha/balrun";

const writer: StreamWriter = { write(chunk: string) {} };

Example:

import { Ballerina, type StreamWriter } from "@snelusha/balrun";

const buffer: string[] = [];
const writer: StreamWriter = {
	write(chunk) {
		buffer.push(chunk);
	},
};

await new Ballerina({ stdout: writer, stderr: writer }).run("./main.bal");

fs

Ballerina reads files through the FS interface. In Node.js environments, this defaults to the built-in Node adapter. In browsers, pass an fs implementation explicitly.

import { Ballerina, type FS } from "@snelusha/balrun";

class MemFS implements FS {
	// When running a single file, only `open` and `stat` are required.
	// When running a package, `readDir` is also required.
}

await new Ballerina({ fs: new MemFS() }).run("main.bal");

To use the Node adapter explicitly:

import { Ballerina } from "@snelusha/balrun";
import { NodeFS } from "@snelusha/balrun/fs/node";

await new Ballerina({ fs: new NodeFS() }).run("./main.bal");

See examples/memfs for a full implementation.

wasmSource / core

By default, Ballerina loads the bundled ballerina.wasm. Pass wasmSource to load a different local path or HTTP(S) URL:

await new Ballerina({ wasmSource: "https://example.com/ballerina.wasm" }).run("main.bal");

For custom loading, pass a BallerinaCore directly. WasmBridge.load() accepts a local path, URL, Response, or Promise<Response>:

import { Ballerina, WasmBridge } from "@snelusha/balrun";

const core = await WasmBridge.load(fetch("/ballerina.wasm"));
await new Ballerina({ core }).run("main.bal");

Bundler note

ballerina.wasm must be available in the final build output at runtime.

Vite handles this automatically: it detects the default WASM URL and emits ballerina.wasm into dist/assets during vite build. Other bundlers may not copy the file automatically. If your built app cannot find ballerina.wasm, copy it from node_modules/@snelusha/balrun/dist/ballerina.wasm into your app's output directory, or use wasmSource to point to where you serve it.

Acknowledgements

Built on ballerina.

Releases

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.