/** * Where things live on each system: the release target of this machine, the * install directory, the directory for the command shim and the data * directory the sidecar uses. */ import os from 'node:os'; import path from 'node:path'; /** The command the installer puts on the PATH. A working name until the product name is decided. */ export const COMMAND_NAME = 'waggle-server'; export interface Host { platform: NodeJS.Platform; arch: string; homedir: string; env: NodeJS.ProcessEnv; } export function currentHost(): Host { return { platform: process.platform, arch: process.arch, homedir: os.homedir(), env: process.env }; } /** Release targets of v1: Windows x64, macOS on Apple and Intel, Linux x64. */ const TARGETS: Record = { 'win32-x64': 'windows-x64', 'darwin-arm64': 'macos-arm64', 'darwin-x64': 'macos-x64', 'linux-x64': 'linux-x64', }; export function releaseTarget(host: Pick): string | null { return TARGETS[`${host.platform}-${host.arch}`] ?? null; } export function assetName(version: string, target: string): string { return `waggle-server-${version}-${target}.tar.gz`; } export function pathApi(platform: NodeJS.Platform): path.PlatformPath { return platform === 'win32' ? path.win32 : path.posix; } /** Program files, not user data: uninstall removes this directory whole. */ export function defaultInstallDir(host: Host): string { const p = pathApi(host.platform); if (host.platform === 'win32') { const localAppData = host.env.LOCALAPPDATA || p.join(host.homedir, 'AppData', 'Local'); return p.join(localAppData, 'Waggle', 'server'); } if (host.platform === 'darwin') { return p.join(host.homedir, 'Library', 'Application Support', 'Waggle', 'server'); } const dataHome = host.env.XDG_DATA_HOME || p.join(host.homedir, '.local', 'share'); return p.join(dataHome, 'waggle', 'server'); } /** * Where the command shim goes. On Windows it stays inside the install * directory: the installer never edits the user's PATH, it prints how to. */ export function defaultBinDir(host: Host, installDir: string): string { const p = pathApi(host.platform); return host.platform === 'win32' ? p.join(installDir, 'bin') : p.join(host.homedir, '.local', 'bin'); } /** Same rule as the sidecar's resolveDataDir: WAGGLE_DATA_DIR, else ~/.waggle. */ export function defaultDataDir(host: Host): string { return host.env.WAGGLE_DATA_DIR || pathApi(host.platform).join(host.homedir, '.waggle'); } export function isOnPath(host: Host, dir: string): boolean { const p = pathApi(host.platform); const entries = (host.env.PATH ?? host.env.Path ?? '').split(p.delimiter).filter(Boolean); const normalize = (value: string) => { const resolved = p.resolve(value); return host.platform === 'win32' ? resolved.toLowerCase() : resolved; }; return entries.some((entry) => normalize(entry) === normalize(dir)); }