scratch is a CLI-first tool that organizes agent knowledge into scratchpads: a folder + a scratchpad.json manifest, with a read-only visual viewer. Runs on the Bun runtime.
Core invariant (do not violate): the CLI never authors, copies, or moves file content. It only creates pad dirs, writes manifests, and tracks metadata. The user/agent writes files with their normal tools; scratch add just registers them. A pad is just a folder containing scratchpad.json — the folder path is its identity. There is no central store/index.
Three deliberate exceptions (don't "fix" them as violations), the first two in src/ui/launch.ts:
- Clicking a rendered GFM task checkbox in the viewer flips that single
[ ]/[x]marker in the source file (persistFileCheckbox). Line-addressed; verifies the line still IS a task marker before writing. - Saving from the viewer's in-place Excalidraw editor writes the edited scene JSON back to its
.excalidrawfile (persistExcalidrawScene). The user drew the content; the CLI only persists it — scoped to manifest-registered.excalidrawtargets and validated withparseScenefirst. scratch import(src/import.ts,cmdImportincommands.ts) rebuilds a pad from ascratch exportHTML page: embedded file contents are written back to disk andscratchpad.jsonis regenerated from the embedded metadata. The content is the user's own export and the restore is explicit; paths that are absolute or contain..are rejected, and the target must be empty unless--force.
bun test # run the suite (bun's built-in test runner)
bun test test/cli.test.ts # single file
bun test --test-name-pattern "slugify" # single test by name
bun run scratch -- <args> # run the CLI in dev (e.g. bun run scratch -- ls)
bun run build # compile standalone → dist/scratch(.exe) + stage native host
bun link # expose `scratch` globally from source
bun run docs:dev # vitepress docs (docs/)No separate lint/typecheck step is wired at the root; tsc runs via pi/ only (bun --cwd pi run typecheck). Tests are the gate (prepublishOnly: bun test).
Two independently-published npm packages plus a Claude Code plugin, all in one repo:
- root (
@nikiforovall/scratchpad) — the CLI. Entrysrc/cli.ts, bin namescratch. pi/(@nikiforovall/pi-scratchpad) — pi coding-agent package: skills +/scratch ui|export|stopcommands. Drives the samescratchCLI (does not reimplement it).plugins/scratchpad/— Claude Code plugin (skills);.claude-plugin/makes this repo a plugin marketplace.
Release with the release skill. A tag push publishes through .github/workflows/release.yml (npm trusted publishing); nobody publishes locally. Tags are prefixed: CLI vX.Y.Z, pi pkg pi-scratchpad-vX.Y.Z. See docs/PUBLISHING.md.
CLI is a thin layer over the filesystem. Data flows: cli.ts (parseArgs dispatch) → commands.ts (one fn per command, each returns an exit code) → discovery.ts + manifest.ts.
manifest.ts—scratchpad.jsonschema (version 1) types + read/write/validate. Unknown keys tolerated on read (forward-compatible).FileEntry.srcmarks a linked external file: content lives atsrc,pathis just its in-pad label.discovery.ts— pad discovery + name/slug/path resolution.findPadsscans a root for manifests (recursive, doesn't descend into a found pad orIGNORE_DIRS). Root resolution:--dir→$SCRATCH_DIR→ cwd.commands.ts— all command impls. Output goes through an injectedIOwriter (testable, no directconsole). Paths emitted to users are run throughtoPosix— always emit forward-slash paths; this repo is developed on Windows/Git Bash where backslashes + drive letters get mangled (especially by the fzf/JSON consumers).config.ts— machine-wide viewer prefs at~/.config/scratchpad/config.json(NOT%APPDATA%— see the comment; the path must not depend on inherited env). Load merges over defaults; malformed file is non-fatal.saveConfigsanitizes the patch field-by-field and preserves unknown keys.
The viewer is read-only and self-contained: render.ts does all file I/O and embeds pad data + file contents into one HTML string, so the native window and browser fallback render identically with no round-trips.
render.ts— builds the page. highlight.js/mermaid load from a pinned CDN (URL + SRI), added conditionally (hljs only if code present, mermaid only if a```mermaidblock exists). When bumping a pinned version, recompute the SRI from the exact CDN bytes. CDN vendoring (not inlining) is deliberate — inlined libs caused a Windows WebView2 blink.launch.ts— opens the viewer. Default = glimpse's native WebView2 host; falls back to serving the same HTML over a local server if native is unavailable (it prints the URL and opens no browser). Lots of hard-won Windows specifics live here (see comments before touching):NAV_LIMITsetHTML cap with file:// fallback, present-once guard against reload loops, graceful Ctrl+C teardown to avoid Chromium stderr spam,GLIMPSE_BINARY_PATHfor the compiled binary's staged host.reload.ts— on-demand reload shared by both transports. Re-reads manifests from disk; emits an in-place data patch unless a new vendor CDN tag became necessary (then a full page reload).- Settings written from the viewer page flow back via WebView2
postMessageorPOST /settings→saveConfig. Every UI setting must round-trip throughconfig.tsand persist across launches.
bunfig.toml preloads test/setup.ts, which forces NO_COLOR=1 so CLI string assertions are deterministic on a TTY or piped. src/colors.ts decides styling per-call (not at import), so color is controllable per test. UI DOM tests use @happy-dom/global-registrator.