@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
63 lines (62 loc) • 2.31 kB
JavaScript
import * as fs from "node:fs";
import * as path from "node:path";
import { homeDir } from "./home-paths.js";
export const ROOT_MANIFEST_FILENAME = "phren.root.yaml";
export function walkUpAllowlistPath() {
const config = process.env.XDG_CONFIG_HOME;
const directory = config && path.isAbsolute(config) ? config : path.join(homeDir(), ".config");
return path.join(directory, "phren", "accepted-stores.json");
}
export function readWalkUpAllowlist(file = walkUpAllowlistPath()) {
try {
const value = JSON.parse(fs.readFileSync(file, "utf8"));
if (Array.isArray(value) && value.every(entry => typeof entry === "string" && path.isAbsolute(entry)))
return value;
throw new Error(`Invalid store acceptance list at ${file}`);
}
catch (error) {
if (error.code === "ENOENT")
return [];
throw error;
}
}
export function findWalkUpCandidate(startDir = process.cwd()) {
let current = path.resolve(startDir);
for (;;) {
const candidate = path.join(current, ".phren");
if (fs.existsSync(path.join(candidate, ROOT_MANIFEST_FILENAME)))
return candidate;
const parent = path.dirname(current);
if (parent === current)
return null;
current = parent;
}
}
export function isAcceptedWalkUpStore(realPath) {
// Keep discovery fail-closed if local configuration cannot be read.
try {
return readWalkUpAllowlist().includes(realPath);
}
catch {
return false;
}
}
let reportedIgnoredStore = false;
export function findNearestPhrenPath(startDir = process.cwd()) {
const candidate = findWalkUpCandidate(startDir);
// The shared store needs no acceptance, even when walking up from home.
if (!candidate || candidate === path.join(homeDir(), ".phren"))
return null;
let realPath;
try {
realPath = fs.realpathSync(candidate);
}
catch { /* candidate disappeared */ }
if (realPath && isAcceptedWalkUpStore(realPath))
return realPath;
if (!reportedIgnoredStore) {
reportedIgnoredStore = true;
process.stderr.write(`phren: ignoring the store at ${JSON.stringify(candidate)} found above this folder; run 'phren store accept' to use it.\n`);
}
return null;
}