underlay v0.3.0 · preview

The Underlay CLI

underlay is a command-line client for Underlay collections. It keeps collections on your machine, runs the scripts that write new versions of them, shows what changed, and pushes the versions to the server the collection lives on. Every action is a command, so scripts, cron jobs, and agents can use it as you do. Run plain underlay in a terminal to open its terminal UI.

It is a preview and changes often. Push to a scratch collection while you try it, and write to help@underlay.org about anything that gets in your way.

The terminal UI: a collection
The terminal UI, drawn by this version of the CLI when the site was built. The organizations in it are made up.

Servers

The CLI works with any server that speaks the Underlay protocol: underlay.org, a staging server, or a server on your own machine. You sign in to each one separately, and your home can hold collections from several. Each collection has an authority, the one server that accepts and numbers its versions, and the CLI pushes each collection to its own.

Pushing and pulling use the protocol's routes for commits built by their author and for following a collection (Push and pull). Listing your organizations' collections (underlay ls) uses underlay.org's account routes, which are not part of the protocol.

Install

You need Node.js 22.6 or later, on macOS or Linux.

curl -fsSL https://www.underlay.org/cli/install.sh | sh

The script downloads the CLI (one JavaScript file) to ~/.local/share/underlay-cli, checks it against its SHA-256, and puts an underlay command in ~/.local/bin. It tells you if that folder is not on your PATH. Read the script before you run it if you like. underlay update gets the newest build from the same server, and the CLI tells you when there is one. To remove the CLI, delete those two paths; your home and its records stay.

Getting started

From nothing to a pushed version. A collection starts on your machine, with no server or sign-in, and you publish it to a server when you want one. The examples use underlay.org; for another server, use its address in place of underlay.org (for example staging.underlay.org).

1. Make your home

A folder for your collections and your own scripts, at ~/Underlay.

underlay init

2. Start a collection

A name alone makes one on this machine only. Its versions are numbered here until you publish it.

underlay new scratch

3. Write a script and run it

Put a JSON Schema for each type in the collection's folder, as ~/Underlay/collections/local/_/scratch/schemas/Thing.json, and a script in ~/Underlay/scripts/:

// ~/Underlay/scripts/scratch.mjs
// underlay: writes scratch
import { writeFileSync } from 'node:fs'

const records = [
  { id: 'a', type: 'Thing', data: { name: 'First' } },
  { id: 'b', type: 'Thing', data: { name: 'Second' } },
]
writeFileSync(
  process.env.UNDERLAY_OUT + '/scratch.ndjson',
  records.map((r) => JSON.stringify(r)).join('\n'),
)
underlay run scratch
underlay diff scratch     # what the run saved

4. Sign in to a server

Make an API key on the server's settings page (on underlay.org, Settings › API keys), then paste it when asked. On a Mac the key is kept in your keychain; elsewhere in a file only you can read.

underlay login underlay.org
underlay ls               # your organizations' collections, to add with underlay add

5. Publish and push

Publishing gives the collection its server, org, and name there. Its first push creates it with the versions you made, unchanged. Push lists what it will send and asks first.

underlay publish scratch underlay.org/your-org/scratch
underlay push

A script names a published collection in full, so change its first line to // underlay: writes underlay.org/your-org/scratch and have it write $UNDERLAY_OUT/underlay.org/your-org/scratch.ndjson. Or run plain underlay and do all of this in the terminal UI, where + starts a collection and p on it publishes it, signing you in there if you aren't.