autosnippet
Version:
Extract code patterns into a knowledge base for AI coding assistants
84 lines (83 loc) • 2.68 kB
TypeScript
/**
* FileProtection — 非 AutoSnippet 独有文件的写入保护
*
* 保护逻辑:
* - 如果目标文件不存在 → 直接写入(首次生成)
* - 如果目标文件存在且包含 AutoSnippet 签名 → 允许覆盖(我们生成的)
* - 如果目标文件存在但不包含 AutoSnippet 签名 → 拒绝覆盖(用户原有文件)
*
* 签名标记(任意一个匹配即视为 AutoSnippet 所有):
* - "Auto-generated by AutoSnippet"
* - "Auto-generated by [AutoSnippet]"
* - "auto-generated by autosnippet" (case-insensitive)
*/
/**
* 检查文件是否可以被 AutoSnippet 安全写入
*
* @param filePath 目标文件绝对路径
* @returns }
* - canWrite: true → 可以安全写入
* - canWrite: false → 文件存在且非 AutoSnippet 生成,不应覆盖
*/
export declare function checkWriteSafety(filePath: string): {
canWrite: boolean;
reason: string;
};
/**
* 安全写入文件 — 带保护机制
*
* @param filePath 目标文件绝对路径
* @param content 文件内容
* @param [options.force=false] 强制覆盖(忽略保护)
* @param [options.logger] 日志器
* @returns }
*/
export declare function safeWriteFile(filePath: string, content: string, options?: {
force?: boolean;
logger?: {
info?: (...args: unknown[]) => void;
};
}): {
written: boolean;
reason: string;
filePath: string;
};
/**
* 安全复制文件 — 带保护机制
*
* @param srcPath 源文件路径
* @param destPath 目标文件路径
* @param [options.force=false] 强制覆盖
* @param [options.logger] 日志器
* @returns }
*/
export declare function safeCopyFile(srcPath: string, destPath: string, options?: {
force?: boolean;
logger?: {
info?: (...args: unknown[]) => void;
};
}): {
written: boolean;
reason: string;
filePath: string;
};
export type MergeStrategy = 'create' | 'replace-section' | 'rewrite-legacy' | 'append-section';
/**
* 智能合并 AutoSnippet 管理区段到目标文件
*
* 四种场景:
* 1. 文件不存在 → 创建完整文件(header + markers)
* 2. 文件有 begin/end 标记 → 仅替换标记区段(增量更新)
* 3. 文件有旧版 AutoSnippet 签名但无标记 → 全量重写并加标记(旧版迁移)
* 4. 文件无签名无标记(用户文件)→ 追加标记区段到末尾(共存)
*/
export declare function mergeSection(filePath: string, section: string, options?: {
header?: string;
logger?: {
info?: (...args: unknown[]) => void;
};
}): {
written: boolean;
strategy: MergeStrategy;
filePath: string;
};