GitHub 0
Skip to content

Places

A Places object names your program and its folders. fleethosts writes and reads under that name only.

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

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"
ts

standardPlaces is also exported from fleethosts.

FieldWhat it is
programThe name your program writes under. It names environment variables and temporary folders.
statePer-machine state: caches, locks, logs, and the recorded fleet.
configPer-user config, where config.json lives.
formerProgramOptional. A name the program had before. fleethosts doesn't read it.
legacyHookMarkOptional. 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.

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

FolderFirst 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.

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

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

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

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.

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

VariableWhat it changes
<PROGRAM>_SSHThe ssh to run instead of ssh
<PROGRAM>_TAILSCALEThe 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.

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 keeps. The others name files a program built on fleethosts keeps, such as config(), state(), lock(), cache() and logs(). See the API reference for the full list.