// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * Path builders for Mailwoman's platform-native application directories. */ import { PathBuilder, type PathBuilderLike, createPathBuilderResolver, resolvePath } from "path-ts" import { $public } from "#env" /** * The per-user configuration root is `$MAILWOMAN_CONFIG_ROOT`. * * It defaults to the platform config directory and stores the license signing * key plus other operator-owned files. */ export const configRootPath = createPathBuilderResolver<"~env/config-root">(() => $public.MAILWOMAN_CONFIG_ROOT) /** * Build a path under the data root, e.g. `dataRootPath("geonames", "US.txt")`; * the env is read on each call, so a late environment change or a test stub is honored * rather than frozen at the first importer's environment. */ export const dataRootPath = createPathBuilderResolver<"~env/data-root">(() => $public.MAILWOMAN_DATA_ROOT) /** * A path under the data root's database group, `$MAILWOMAN_DATA_ROOT/db/`, where every built * or published SQLite artifact lives one subdirectory per layer. * * Source downloads, corpora, weights and caches stay at the data root's top level * and do not pass through here. * * The group name is a bare string in every path a caller composes, * so the compiler reports no error when a call site keeps a pre-grouping prefix: * such a call site resolves to a directory that doesn't exist. * Every reader of a layer database treats an absent file as an absent layer. * * Compose a database path through this function — via the owning package's `paths` export, for example * `wofDatabasePath` from `@mailwoman/resolver-wof-sqlite/paths` — so one edit moves all of them. */ export function databaseRootPath(source: T) { return PathBuilder.from(source)("db") } /** * A vintage identifies one dated pull of a source: a year, month or day * (`2026`, `2026-08`, `2026-08-31`), or the publisher's release id (`2026-05-20.0`, `r24`, `fy23`). * It is one path segment. */ const VINTAGE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u /** * Throws unless `vintage` is one path segment a vintage can take. */ export function assertVintage(vintage: string): void { if (!VINTAGE.test(vintage) || vintage.includes("..")) { throw new Error(`"${vintage}" is not a vintage: use a date such as 2026-08-31 or a release id such as r24`) } } /** * A path under a current source's download directory, `$MAILWOMAN_CACHE_ROOT/sources//`. * * A current source is mutable, and fetching it again brings it up to date: a git clone, * or a download the publisher serves under an undated name. * A customer who clears the cache root fetches the source again and loses no data. */ export function currentSourcePath(source: string, ...segments: string[]): PathBuilder { return currentSourcesRoot()(source, ...segments) } /** * The directory that holds every current source, `$MAILWOMAN_CACHE_ROOT/sources/`. * * A fetch command that writes each source under `//` takes this as its root, * so `currentSourcesRoot()(source)` is {@link currentSourcePath}`(source)`. */ export function currentSourcesRoot(): PathBuilder { return cacheRootPathBuilder("sources") } /** * The directory that holds every snapshot source, `$MAILWOMAN_DATA_ROOT/snapshots/`. * * A fetch command that writes each vintage under `///` takes this as its root. */ export function snapshotSourcesRoot(): PathBuilder { return dataRootPath("snapshots") } /** * The directory that holds every vintage of a snapshot source, `$MAILWOMAN_DATA_ROOT/snapshots//`. * * A reader that lists the vintages on disk, such as one that picks the newest Overture release, starts here. * A path inside one vintage goes through {@link snapshotSourcePath}. */ export function snapshotSourceDirectory(source: string): PathBuilder { return snapshotSourcesRoot()(source) } /** * A path under one vintage of a snapshot source, `$MAILWOMAN_DATA_ROOT/snapshots///`. * * A snapshot is a dated pull that's read-only once written. * A newer vintage sits beside it and supersedes it. * * Write one through `writeSnapshot` from `@mailwoman/core/fs/snapshot`. * * @throws When `vintage` isn't one path segment a vintage can take. */ export function snapshotSourcePath(source: string, vintage: string, ...segments: string[]): PathBuilder { assertVintage(vintage) return snapshotSourceDirectory(source)(vintage, ...segments) } /** * The dev-weights overlay root, `$MAILWOMAN_WEIGHTS_OVERLAY_ROOT` when set * and `$MAILWOMAN_DATA_ROOT/weights/` otherwise. * * The data root is shared by every checkout on a host, so a CI job points this variable * at a per-job directory and keeps reading the shared databases. */ export function weightsOverlayRootPath(): PathBuilder { const root = $public.MAILWOMAN_WEIGHTS_OVERLAY_ROOT return root ? PathBuilder.from(resolvePath(root)) : dataRootPath("weights") } /** * The dev-weights overlay for a locale, `//` (see {@link weightsOverlayRootPath}). * * Each `link-dev-weights.ts` script writes its own locale's directory. * `@mailwoman/neural` (`resolveWeights`) reads from this directory. * * It lives outside git because the data root is shared across local checkouts and never packaged. * Weight binaries stay outside the repository. */ export function weightsOverlayPath(locale: string, ...segments: string[]) { return weightsOverlayRootPath()(locale.toLowerCase(), ...segments) } /** * Path builder under Mailwoman's temporary-file root, e.g. `tempRootPathBuilder("reg", "fr-communes.tsv")`; * the env is read on each call, so a late environment change or a test stub is honored. */ export const tempRootPathBuilder = createPathBuilderResolver<"~env/temp-root">(() => $public.MAILWOMAN_TEMP_ROOT) /** * The Mailwoman temporary-file root. */ export function mailwomanTempRoot(): PathBuilder { return tempRootPathBuilder() } /** * Absolute-path-string resolver under the temporary-file root — the string-returning sibling of * {@link tempRootPathBuilder}, for handing paths straight to `node:fs` and other string APIs. */ export function tempRootPath(...segments: string[]): string { return resolvePath($public.MAILWOMAN_TEMP_ROOT, ...segments) } /** * The Mailwoman cache root from the typed public environment. */ export const cacheRootPathBuilder = createPathBuilderResolver<"~env/cache-root">(() => $public.MAILWOMAN_CACHE_ROOT) /** * The Mailwoman cache root. */ export function mailwomanCacheRoot(): PathBuilder { return cacheRootPathBuilder() } /** * Absolute path string under the Mailwoman cache root. */ export function cacheRootPath(...segments: string[]): string { return cacheRootPathBuilder(...segments).toString() } /** * Path builder for the Who's On First git checkouts: `$MAILWOMAN_CACHE_ROOT/sources/wof-repos/`. * * They're current sources: `git pull` brings each one up to date, and a cleared cache root is cloned again. * * `mailwoman gazetteer inspect sync` clones each repository here from its `mailwoman` fork. * Admin builds read here. * Postcode builds and polygon builds also read from here. * * The constant follows `cacheRootPathBuilder` because it is evaluated when this module loads. */ export const wofReposPath = currentSourcePath("wof-repos")