Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

3.4 KB
Raw Blame
import fsSync from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import { assertAsyncDirectoryGuard, assertSyncDirectoryGuard, createAsyncDirectoryGuard, createNearestExistingDirectoryGuard, createNearestExistingSyncDirectoryGuard, createSyncDirectoryGuard, } from "./directory-guard.js";
export async function withAsyncDirectoryGuards(guards, mutate, options = {}) {
    for (const guard of guards) {
        await assertAsyncDirectoryGuard(guard);
    }
    const result = await mutate();
    if (options.verifyAfter !== false) {
        try {
            for (const guard of guards) {
                await assertAsyncDirectoryGuard(guard);
            }
        }
        catch (error) {
            if (options.onPostGuardFailure) {
                try {
                    // The mutation may have returned an owned resource before the post-guard
                    // check detected a swapped directory. Give callers one chance to close
                    // handles without letting cleanup hide the boundary failure.
                    await options.onPostGuardFailure(result, error);
                }
                catch {
                    // Preserve the boundary failure. Cleanup is best-effort.
                }
            }
            throw error;
        }
    }
    return result;
}
export function withSyncDirectoryGuards(guards, mutate, options = {}) {
    for (const guard of guards) {
        assertSyncDirectoryGuard(guard);
    }
    const result = mutate();
    if (options.verifyAfter !== false) {
        for (const guard of guards) {
            assertSyncDirectoryGuard(guard);
        }
    }
    return result;
}
export async function guardedRename(params) {
    const sourceGuard = await createAsyncDirectoryGuard(path.dirname(params.from));
    const targetGuard = params.targetRoot
        ? await createNearestExistingDirectoryGuard(params.targetRoot, path.dirname(params.to))
        : await createAsyncDirectoryGuard(path.dirname(params.to));
    await withAsyncDirectoryGuards([sourceGuard, targetGuard], async () => {
        await fs.rename(params.from, params.to);
    }, { verifyAfter: params.verifyAfter });
}
export function guardedRenameSync(params) {
    const sourceGuard = createSyncDirectoryGuard(path.dirname(params.from));
    const targetGuard = params.targetRoot
        ? createNearestExistingSyncDirectoryGuard(params.targetRoot, path.dirname(params.to))
        : createSyncDirectoryGuard(path.dirname(params.to));
    withSyncDirectoryGuards([sourceGuard, targetGuard], () => fsSync.renameSync(params.from, params.to), { verifyAfter: params.verifyAfter });
}
export async function guardedRm(params) {
    const guard = await createAsyncDirectoryGuard(path.dirname(params.target));
    await withAsyncDirectoryGuards([guard], async () => {
        await fs.rm(params.target, {
            ...(params.recursive !== undefined ? { recursive: params.recursive } : {}),
            ...(params.force !== undefined ? { force: params.force } : {}),
        });
    }, { verifyAfter: params.verifyAfter });
}
export function guardedRmSync(params) {
    const guard = createSyncDirectoryGuard(path.dirname(params.target));
    withSyncDirectoryGuards([guard], () => fsSync.rmSync(params.target, {
        ...(params.recursive !== undefined ? { recursive: params.recursive } : {}),
        ...(params.force !== undefined ? { force: params.force } : {}),
    }), { verifyAfter: params.verifyAfter });
}