UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

69 lines • 2.71 kB
import { constants, cpSync, existsSync, mkdirSync, readdirSync, realpathSync } from "node:fs"; import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path"; import chalk from "chalk"; const REGENERABLE_ENTRIES = new Set(["cache", "logs", "tmp", "migrations-state.json"]); export function isRegenerableEntry(entry) { return REGENERABLE_ENTRIES.has(entry) || entry.endsWith(".log"); } export function pathsPointToSameLocation(leftPath, rightPath) { try { return realpathSync(leftPath) === realpathSync(rightPath); } catch (error) { if (isMissingPathError(error)) return false; throw error; } } /** Resolves symlinks in the longest existing prefix, so `/tmp/x` and `/private/tmp/x` compare equal. */ function canonicalPath(path) { const absolute = resolve(path); if (existsSync(absolute)) return realpathSync(absolute); const parent = dirname(absolute); return parent === absolute ? absolute : join(canonicalPath(parent), basename(absolute)); } export function isWithinOrSamePath(childPath, parentPath) { const relativePath = relative(canonicalPath(parentPath), canonicalPath(childPath)); return relativePath === "" || (!relativePath.startsWith("..") && !isAbsolute(relativePath)); } function isMissingPathError(error) { return error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"); } /** * Copies the top-level entries of `source` that `target` lacks. Never overwrites and only reads * `source`; clones copy-on-write where supported so large session trees cost no extra space. */ export function copyMissingEntries(source, target, include) { let entries; try { entries = readdirSync(source); } catch (error) { if (isMissingPathError(error)) return []; throw error; } const copied = []; for (const entry of entries) { if (!include(entry) || existsSync(join(target, entry))) continue; try { mkdirSync(target, { recursive: true }); cpSync(join(source, entry), join(target, entry), { recursive: true, force: false, errorOnExist: false, preserveTimestamps: true, mode: constants.COPYFILE_FICLONE, }); copied.push(entry); } catch (error) { const reason = error instanceof Error ? error.message : String(error); console.warn(chalk.yellow(`Could not copy ${join(source, entry)} → ${join(target, entry)}: ${reason}`)); } } return copied; } //# sourceMappingURL=legacy-dir-copy.js.map