cartography.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #!/usr/bin/env bun
  2. import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
  3. import { join, resolve } from 'node:path';
  4. import { createMD5, md5 } from 'hash-wasm';
  5. import ignore from 'ignore';
  6. interface FileEntry {
  7. p: string;
  8. h: string;
  9. }
  10. interface CodemapData {
  11. h: string;
  12. f: FileEntry[];
  13. }
  14. const DEFAULT_IGNORE = [
  15. 'node_modules',
  16. '.git',
  17. 'dist',
  18. 'build',
  19. '.next',
  20. 'coverage',
  21. '.turbo',
  22. 'out',
  23. '*.log',
  24. '.DS_Store',
  25. ];
  26. function parseGitignore(folder: string): ignore.Ignore {
  27. const gitignorePath = join(folder, '.gitignore');
  28. if (existsSync(gitignorePath)) {
  29. const content = readFileSync(gitignorePath, 'utf-8');
  30. return ignore().add(content.split('\n'));
  31. }
  32. return ignore();
  33. }
  34. function shouldIgnore(relPath: string, ignorer: ignore.Ignore): boolean {
  35. if (DEFAULT_IGNORE.some((pattern) => relPath.includes(pattern))) {
  36. return true;
  37. }
  38. return ignorer.ignores(relPath);
  39. }
  40. function getFiles(
  41. folder: string,
  42. extensions: string[],
  43. ignorer: ignore.Ignore,
  44. ): string[] {
  45. const files: string[] = [];
  46. function scan(dir: string, base: string = '') {
  47. const entries = readdirSync(dir, { withFileTypes: true });
  48. for (const entry of entries) {
  49. const fullPath = join(dir, entry.name);
  50. const relPath = base ? join(base, entry.name) : entry.name;
  51. if (shouldIgnore(relPath, ignorer)) {
  52. continue;
  53. }
  54. if (entry.isDirectory()) {
  55. scan(fullPath, relPath);
  56. } else if (entry.isFile()) {
  57. const ext = entry.name.includes('.')
  58. ? '.' + entry.name.split('.').pop()!
  59. : '';
  60. if (extensions.includes(ext)) {
  61. files.push(relPath);
  62. }
  63. }
  64. }
  65. }
  66. scan(folder);
  67. return files.sort((a, b) => a.localeCompare(b));
  68. }
  69. async function calculateHashes(
  70. folder: string,
  71. files: string[],
  72. ): Promise<Map<string, string>> {
  73. const hashes = new Map<string, string>();
  74. for (const file of files) {
  75. const fullPath = join(folder, file);
  76. try {
  77. const content = await Bun.file(fullPath).text();
  78. hashes.set(file, await md5(content));
  79. } catch (error) {
  80. console.error(`Failed to hash ${file}:`, error);
  81. }
  82. }
  83. return hashes;
  84. }
  85. async function calculateFolderHash(
  86. fileHashes: Map<string, string>,
  87. ): Promise<string> {
  88. const hasher = await createMD5();
  89. hasher.init();
  90. const sortedEntries = Array.from(fileHashes.entries()).sort(([a], [b]) =>
  91. a.localeCompare(b),
  92. );
  93. for (const [path, hash] of sortedEntries) {
  94. hasher.update(`${path}:${hash}|`);
  95. }
  96. return hasher.digest();
  97. }
  98. function readCodemapData(codemapPath: string): CodemapData | null {
  99. if (!existsSync(codemapPath)) {
  100. return null;
  101. }
  102. try {
  103. const content = readFileSync(codemapPath, 'utf-8');
  104. return JSON.parse(content) as CodemapData;
  105. } catch {
  106. return null;
  107. }
  108. }
  109. function writeCodemapData(codemapPath: string, data: CodemapData): void {
  110. const content = `${JSON.stringify(data, null, 2)}\n`;
  111. writeFileSync(codemapPath, content, 'utf-8');
  112. }
  113. function diffFiles(
  114. currentHashes: Map<string, string>,
  115. previous: CodemapData | null,
  116. ): string[] {
  117. if (!previous) {
  118. return Array.from(currentHashes.keys()).sort((a, b) => a.localeCompare(b));
  119. }
  120. const oldHashes = new Map(previous.f.map((f) => [f.p, f.h]));
  121. const changed = new Set<string>();
  122. for (const [path, hash] of currentHashes) {
  123. if (oldHashes.get(path) !== hash) {
  124. changed.add(path);
  125. }
  126. }
  127. for (const path of oldHashes.keys()) {
  128. if (!currentHashes.has(path)) {
  129. changed.add(path);
  130. }
  131. }
  132. return Array.from(changed).sort((a, b) => a.localeCompare(b));
  133. }
  134. async function updateCodemap(
  135. folder: string,
  136. extensions: string[],
  137. ): Promise<{ updated: boolean; fileCount: number; changedFiles: string[] }> {
  138. const ignorer = parseGitignore(folder);
  139. const files = getFiles(folder, extensions, ignorer);
  140. const fileHashes = await calculateHashes(folder, files);
  141. const folderHash = await calculateFolderHash(fileHashes);
  142. const codemapPath = join(folder, '.codemap.json');
  143. const existing = readCodemapData(codemapPath);
  144. if (existing?.h === folderHash) {
  145. return { updated: false, fileCount: files.length, changedFiles: [] };
  146. }
  147. const changedFiles = diffFiles(fileHashes, existing);
  148. const data: CodemapData = {
  149. h: folderHash,
  150. f: files.map((p) => ({ p, h: fileHashes.get(p)! })),
  151. };
  152. writeCodemapData(codemapPath, data);
  153. return { updated: true, fileCount: files.length, changedFiles };
  154. }
  155. async function getChanges(
  156. folder: string,
  157. extensions: string[],
  158. ): Promise<{
  159. fileCount: number;
  160. folderHash: string;
  161. changedFiles: string[];
  162. }> {
  163. const ignorer = parseGitignore(folder);
  164. const files = getFiles(folder, extensions, ignorer);
  165. const fileHashes = await calculateHashes(folder, files);
  166. const folderHash = await calculateFolderHash(fileHashes);
  167. const codemapPath = join(folder, '.codemap.json');
  168. const existing = readCodemapData(codemapPath);
  169. const changedFiles = diffFiles(fileHashes, existing);
  170. return {
  171. fileCount: files.length,
  172. folderHash,
  173. changedFiles,
  174. };
  175. }
  176. async function main() {
  177. const command = process.argv[2];
  178. const folderArg = process.argv[3];
  179. const folder = folderArg ? resolve(folderArg) : process.cwd();
  180. const extArg = process.argv.find((a) => a.startsWith('--extensions'));
  181. let extensions: string[];
  182. if (extArg) {
  183. const extList = extArg.split('=')[1];
  184. if (extList) {
  185. extensions = extList
  186. .split(',')
  187. .map((e) => '.' + e.trim().replace(/^\./, '')); // 预先计算点号前缀
  188. } else {
  189. extensions = ['.ts', '.tsx', '.js', '.jsx', '.py', '.go', '.rs'];
  190. }
  191. } else {
  192. extensions = ['.ts', '.tsx', '.js', '.jsx', '.py', '.go', '.rs'];
  193. }
  194. switch (command) {
  195. case 'scan': {
  196. const ignorer = parseGitignore(folder);
  197. const files = getFiles(folder, extensions, ignorer);
  198. console.log(JSON.stringify({ folder, files }, null, 2));
  199. break;
  200. }
  201. case 'hash': {
  202. const ignorer = parseGitignore(folder);
  203. const files = getFiles(folder, extensions, ignorer);
  204. const fileHashes = await calculateHashes(folder, files);
  205. const folderHash = await calculateFolderHash(fileHashes);
  206. console.log(
  207. JSON.stringify(
  208. {
  209. folderHash,
  210. files: Object.fromEntries(fileHashes),
  211. },
  212. null,
  213. 2,
  214. ),
  215. );
  216. break;
  217. }
  218. case 'update': {
  219. const result = await updateCodemap(folder, extensions);
  220. if (result.updated) {
  221. console.log(
  222. JSON.stringify(
  223. {
  224. updated: true,
  225. folder,
  226. fileCount: result.fileCount,
  227. changedFiles: result.changedFiles,
  228. },
  229. null,
  230. 2,
  231. ),
  232. );
  233. } else {
  234. console.log(
  235. JSON.stringify(
  236. {
  237. updated: false,
  238. folder,
  239. message: 'No changes detected',
  240. },
  241. null,
  242. 2,
  243. ),
  244. );
  245. }
  246. break;
  247. }
  248. case 'changes': {
  249. const result = await getChanges(folder, extensions);
  250. console.log(
  251. JSON.stringify(
  252. {
  253. folder,
  254. fileCount: result.fileCount,
  255. folderHash: result.folderHash,
  256. changedFiles: result.changedFiles,
  257. hasChanges: result.changedFiles.length > 0,
  258. },
  259. null,
  260. 2,
  261. ),
  262. );
  263. break;
  264. }
  265. default:
  266. console.error(
  267. 'Usage: cartography <scan|hash|update|changes> [folder] [--extensions ts,tsx,js]',
  268. );
  269. process.exit(1);
  270. }
  271. }
  272. main().catch((error) => {
  273. console.error('Error:', error);
  274. process.exit(1);
  275. });