# Places

Every function that reads or writes your program's files takes a `Places`: your program's name, its state folder and its config folder.

```ts
import { envName, placePaths, standardPlaces } from "fleethosts/places";

const places = standardPlaces("mytool");
// { program: "mytool", state: "/home/me/.local/state/mytool", config: "/home/me/.config/mytool" }

placePaths(places).hosts(); // "/home/me/.local/state/mytool/hosts.json"
envName(places, "SSH"); // "MYTOOL_SSH"
```

`standardPlaces` is also exported from `fleethosts`.

## The fields

| Field | What it is |
| --- | --- |
| `program` | The name your program writes under. It names environment variables and temporary folders. |
| `state` | Per-machine state: caches, locks, logs, and the recorded fleet. |
| `config` | Per-user config, where `config.json` lives. |
| `formerProgram` | Optional. A name the program had before. fleethosts doesn't read it. |
| `legacyHookMark` | Optional. A hook ownership key to keep from an earlier version. fleethosts doesn't read it. |

A `Places` is a plain object, so tests can build one with folders in a temporary directory.

## Where the folders go

`standardPlaces(program)` follows the XDG base directory spec, and the platform's convention where it is unset:

| Folder | First match |
| --- | --- |
| config | `$XDG_CONFIG_HOME/<program>`, then `%APPDATA%\<program>` on Windows, then `~/.config/<program>` |
| state | `$XDG_STATE_HOME/<program>`, then `%LOCALAPPDATA%\<program>` on Windows, then `~/.local/state/<program>` |

A relative `XDG_*` path is ignored, as the spec says. The object comes back frozen.

### An installed copy

A program that installs itself as `<state>/app/cli.mjs` can pass that folder as the second argument:

```ts
import { installedStateDir, standardPlaces } from "fleethosts/places";

const places = standardPlaces("mytool", installedStateDir());
```

`installedStateDir()` returns the state folder when the running script is `<folder>/app/cli.mjs`, and `undefined` otherwise. `XDG_STATE_HOME` still wins over it. On Windows, an existing `state.json` in `%LOCALAPPDATA%\<program>` keeps that folder; the choice stays fixed for the process.

## Environment variables

`envName(places, name)` gives `<PROGRAM>_<name>`, upper case, with any character outside `A-Z` and `0-9` turned into `_`. fleethosts reads two:

| Variable | What it changes |
| --- | --- |
| `<PROGRAM>_SSH` | The ssh to run instead of `ssh` |
| `<PROGRAM>_TAILSCALE` | The Tailscale CLI to run instead of `tailscale` |

A value ending in `.js` or `.mjs` runs with the current Node, which is how the tests stand in a fake ssh. For `desk+tools`, the variable is `DESK_TOOLS_SSH`.

## File paths

`placePaths(places)` returns functions that build file paths in the folders. It also takes a function that returns the places, read on every call.

fleethosts itself writes one of them: `hosts()`, the fleet [`recordHosts`](https://fleethosts.loadsheet.dev/guide/hosts#record-the-fleet) keeps. The others name files a program built on fleethosts keeps, such as `config()`, `state()`, `lock()`, `cache()` and `logs()`. See the [API reference](https://fleethosts.loadsheet.dev/reference/api#placepaths) for the full list.

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://fleethosts.loadsheet.dev/raw/guide.md)
- [Installation](https://fleethosts.loadsheet.dev/raw/guide/installation.md)
- [Places](https://fleethosts.loadsheet.dev/raw/guide/places.md) (this page)
- [Hosts](https://fleethosts.loadsheet.dev/raw/guide/hosts.md)
- [Exec](https://fleethosts.loadsheet.dev/raw/guide/exec.md)
- [Coding agents](https://fleethosts.loadsheet.dev/raw/guide/agents.md)

Next: [Hosts](https://fleethosts.loadsheet.dev/raw/guide/hosts.md)

All pages: [llms.txt](https://fleethosts.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/fleethosts/issues). Report a security vulnerability privately, never in a public issue.
