import path from '#host-path' import { statSync } from '#host-fs' import { fileURLToPath } from 'url' import os from 'bare-os' function cwd() { if (typeof globalThis.process?.cwd === 'function') { try { const p = globalThis.process.cwd() if (p) return p } catch { /* Pear/Bare sometimes exposes cwd() but returns "" */ } } return os.cwd() } function env(name) { return globalThis.process?.env?.[name] } /** Host-side state root (Corestore, etc.): `BARE_OS_HOST_DATA` or `~/.bare-os`. */ function hostDataRoot() { const override = env('BARE_OS_HOST_DATA') if (override) return path.resolve(override) return path.join(os.homedir(), '.bare-os') } function kernelDirPresent(root) { try { return statSync(path.join(root, 'kernel')).isDirectory() } catch { return false } } function dirnameOf(p) { return path.dirname(path.resolve(String(p))) } /** * When running as a Bare standalone binary, prefer the directory containing * the executable (tarball layout ships kernel/ next to bare-os-seeder). */ function bundledExecCandidates() { const out = [] try { if (typeof os.execPath === 'function') { const ep = os.execPath() if (ep) out.push(dirnameOf(ep)) } } catch { // ignore } const argv0 = globalThis.Bare?.argv?.[0] || globalThis.process?.argv?.[0] if (argv0 && pathBasename(argv0) !== 'bare' && !String(argv0).includes('node')) { out.push(dirnameOf(argv0)) } return out } function pathBasename(p) { const s = String(p || '') const i = Math.max(s.lastIndexOf('/'), s.lastIndexOf('\\')) return i >= 0 ? s.slice(i + 1) : s } /** * Directory containing this package (index.js, kernel/, …). * Under `pear run`, `import.meta.url` is pear: — Pear may set `RTI.mount` and/or `swapDir` * to different paths; prefer the first candidate that actually contains `kernel/`. * Under Bare standalone, prefer dirname(execPath) when it contains kernel/. */ export function packageRootDir(metaUrl) { const href = String(metaUrl) const seen = new Set() const candidates = [] const push = (c) => { if (!c) return const p = path.resolve(String(c)) if (!seen.has(p)) { seen.add(p) candidates.push(p) } } for (const c of bundledExecCandidates()) push(c) if (href.startsWith('file:')) { // Caller passes index.js import.meta.url → dirname is package root push(path.dirname(fileURLToPath(href))) } const rti = globalThis.Pear?.constructor?.RTI?.mount const swap = globalThis.Pear?.config?.swapDir const pearAppDir = globalThis.Pear?.app?.dir ?? globalThis.Pear?.config?.dir const rtiDir = globalThis.Pear?.constructor?.RTI?.dir for (const c of [rti, swap, pearAppDir, rtiDir, cwd()].filter(Boolean)) push(c) for (const root of candidates) { if (kernelDirPresent(root)) return root } return candidates[0] ?? cwd() } /** Default kernel tree: vendored `kernel/` next to the app (works for pear: and file:). */ export function defaultKernelRoot(pkgRoot, metaUrl) { const override = env('BARE_OS_KERNEL_ROOT') if (override) return path.resolve(override) return path.join(pkgRoot, 'kernel') } /** Corestore directory for the seeder (host cache, not Hyperdrive OS state). */ export function defaultSeedCorestorePath(pkgRoot, metaUrl) { const override = env('BARE_OS_SEED_STORE') if (override) return override return path.join(hostDataRoot(), 'corestore', 'seeder') }