autosnippet
Version:
Extract code patterns into a knowledge base for AI coding assistants
151 lines (150 loc) • 4.8 kB
TypeScript
/**
* WikiUtils.js — Wiki 生成器工具函数
*
* 从 WikiGenerator.js 中提取的纯工具/辅助函数,无 class 依赖。
*
* @module WikiUtils
*/
/** 文本 slug 化 */
export declare function slug(name: string): string;
/** Mermaid 安全 ID */
export declare function mermaidId(name: string): string;
/** 遍历目录(排除 build/Pods/DerivedData 等) */
export declare function walkDir(dir: string, callback: (filePath: string) => void, maxFiles?: number): void;
/**
* 从文件相对路径推断所属模块名
* 支持多种项目结构约定:
* SPM: Sources/{ModuleName}/...
* Node.js: packages/{name}/... | src/{name}/... | lib/{name}/...
* Go: pkg/{name}/... | internal/{name}/... | cmd/{name}/...
* Rust: crates/{name}/... | src/ (单 crate)
* Python: src/{name}/... | {name}/ (顶层包)
* Java/Kt: src/main/java/{pkg}/... (取第一个包段)
* Dart: lib/{name}/...
*
* 兜底: 取第一级目录名
*/
export declare function inferModuleFromPath(filePath: string): string | null;
/**
* 获取某个 Target 对应的源文件列表
* 按优先级匹配: target.path → target.info.path → sourceFilesByModule[name]
*/
export declare function getModuleSourceFiles(target: {
name: string;
path?: string;
info?: {
path?: string;
};
}, projectInfo: {
sourceFilesByModule?: Record<string, string[]>;
sourceFiles?: string[];
}): string[];
/**
* 基于模块名称和内容推断模块功能
* 对常见命名模式做智能推断
*/
export declare function inferModulePurpose(name: string, classes: string[], protocols: string[], files: string[]): {
match: RegExp;
zh: string;
en: string;
} | null;
/**
* 从 CodeEntityGraph 提取继承根节点
* @returns >}
*/
export declare function getInheritanceRoots(codeEntityGraph: {
queryEntities?: (filter: Record<string, unknown>) => Array<{
entityId: string;
name: string;
}>;
queryEdges?: (filter: Record<string, unknown>) => Array<{
toId?: string;
to_id?: string;
fromId?: string;
}>;
} | null): {
name: string;
children: string[];
}[];
/**
* 两层去重
*
* Layer 1: Title slug 碰撞 — 同名文件不同目录 → hash 相同则删除副本
* Layer 2: Content hash — 跨文件内容完全相同 → 仅保留第一个
*
* @returns }
*/
export declare function dedup(files: {
path: string;
hash: string;
}[], wikiDir: string, emit: (phase: string, progress: number, message: string) => void): {
removed: string[];
kept: number;
};
/**
* 按主语言返回 AST 术语(中英文)
*
* 不同语言对"类"和"接口"有不同称谓,Wiki 文档应使用合适的措辞。
*
* @param langId LanguageService langId,如 'swift', 'python', 'go'
* @returns , interfaceLabel: {zh: string, en: string}, moduleMetric: {zh: string, en: string} }}
*/
export declare function getLangTerms(langId: string): {
typeLabel: {
zh: string;
en: string;
};
interfaceLabel: {
zh: string;
en: string;
};
moduleMetric: {
zh: string;
en: string;
};
};
/**
* 已知的构建系统标志文件 → 生态类型映射
*
* @deprecated 请使用 LanguageService.buildSystemMarkers。此处保留为只读引用以保持向后兼容。
*/
export declare const BUILD_SYSTEM_MARKERS: readonly {
file: string;
eco: string;
buildTool: string;
}[];
/**
* 检测项目根目录中存在的构建系统标志
*
* 两级检测:
* 1. 先检查根目录的一级文件
* 2. 如果根目录未找到,检查一级子目录(支持 monorepo 如 AppFlowy/frontend/...)
*
* @param rootEntryNames 项目根目录一级文件/目录名列表
* @param [projectRoot] 可选的项目根路径,用于二级检测
* @returns >} 匹配到的构建系统
*/
export declare function detectBuildSystems(rootEntryNames: string[], projectRoot?: string): {
eco: string;
buildTool: string;
}[];
/**
* 分析项目中重要文件夹,生成 FolderProfile 列表
*
* 适用场景: AST 无法提取 target(类/函数/协议)的语言,
* 通过文件夹结构、文件命名、轻量 import 分析来产出有意义的 wiki 内容。
*
* @param projectInfo WikiGenerator._scanProject() 的输出
* @param [options.minFiles=3] 文件夹最少文件数阈值
* @param [options.maxFolders=20] 最多分析的文件夹数
* @param [options.sampleLines=40] 每个文件采样行数 (用于 import 提取)
*/
export declare function profileFolders(projectInfo: {
root: string;
sourceFiles?: string[];
[key: string]: unknown;
}, options?: {
minFiles?: number;
maxFolders?: number;
sampleLines?: number;
}): Record<string, unknown>[];