# First app

> Create a project, run it, call Rust from the page and bundle it.

This page takes a new project from `kurogane new` to a bundled app. You need the [CLI and the build tools](https://kurogane-rs.org/docs/start/install.md) installed.

## Create a project

Create a project from the minimal starter:

```bash
kurogane new minimal
```

The CLI asks for a project name and a language (TypeScript or JavaScript). Then it generates the project in a directory of that name.

`kurogane new` without a starter asks which one to use. The official starters are `minimal`, `react`, `svelte` and `vue`. Each uses a Vite frontend.

## Run it

From the new project:

```bash
npm --prefix frontend install     # install frontend dependencies once
npm --prefix frontend run dev     # start the dev server
kurogane dev                      # launch the app at the dev server
```

Run the dev server and `kurogane dev` in two terminals. A window opens with "Hello from Kurogane!".

`kurogane dev` builds your Rust code in debug mode and runs it. It installs the Chromium runtime first when it is missing. Changes to the frontend reload in the window. Changes to Rust need `kurogane dev` again.

## The Rust side

`src/main.rs` holds the whole app:

```rust
#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]

use kurogane::App;

fn main() {
    #[cfg(debug_assertions)]
    App::url("http://localhost:5173").run_or_exit();

    #[cfg(not(debug_assertions))]
    App::new("content").run_or_exit();
}
```

A debug build points the window at the dev server with [`App::url`](https://kurogane-rs.org/docs/api/app/app.md#method.url). A release build loads the built frontend from the `content` directory with [`App::new`](https://kurogane-rs.org/docs/api/app/app.md#method.new). A bundle carries that directory inside it.

[`App::run_or_exit`](https://kurogane-rs.org/docs/api/app/app.md#method.run_or_exit) starts Chromium and runs until the last window closes. It prints the error and exits when the app can't start.

The first line keeps a console window from opening next to your app on Windows.

## Call Rust from the page

Register a command in `src/main.rs`:

```rust
#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]

use kurogane::{App, AppHandle, IpcError};

fn greet(name: String, _: &AppHandle) -> Result<String, IpcError> {
    Ok(format!("Hello, {name}!"))
}

fn main() {
    #[cfg(debug_assertions)]
    let app = App::url("http://localhost:5173");

    #[cfg(not(debug_assertions))]
    let app = App::new("content");

    app.command("greet", greet).run_or_exit();
}
```

Call it from the page in `frontend/main.ts` or `frontend/main.js`:

```javascript
window.kurogane.invoke("greet", "Kurogane").then((reply) => {
  document.querySelector("#app").textContent = reply;
});
```

Run `kurogane dev` again. The window now shows "Hello, Kurogane!".

The page sends `"Kurogane"` as JSON. Kurogane decodes it into the handler's `String` and sends the reply back the same way. Any type serde can read or write works on either side. See [IPC](https://kurogane-rs.org/docs/concepts/ipc.md) for async commands, events and streams.

> [!NOTE]
> Kurogane doesn't ship TypeScript declarations for `window.kurogane` yet. Your editor reports it as unknown in `main.ts`. Vite runs the page all the same.

## Bundle it

Package the app for your platform:

```bash
kurogane bundle
```

It runs the frontend build from `kurogane.toml` and builds your Rust code in release mode. Then it packages both with the Chromium runtime. The finished app lands in `dist/`.

Bundles are platform-specific. Build each platform's bundle on that platform.

## Already have a frontend?

`kurogane init` adds Kurogane to an existing frontend project without touching your files:

```bash
kurogane init
```

It asks for the frontend's build output directory and its dev server URL. Pass both to skip the questions:

```bash
kurogane init --assets dist --dev-url http://localhost:5173
```

It writes `Cargo.toml`, `build.rs`, `kurogane.toml` and `src/main.rs`. It refuses to run when any of them already exists.
