From 641e373f3a8e9a142bd80e22d2420dd787a5c772 Mon Sep 17 00:00:00 2001 From: xiongxiao Date: Fri, 13 Mar 2026 18:43:40 +0800 Subject: [PATCH] feat: update version and refactor ignore patterns in project listener --- package.json | 7 +++-- src/project/project-listener/listener.ts | 10 ++----- src/project/project-search/index.ts | 4 +-- src/project/util/git.ts | 2 +- src/project/utils.ts | 36 ++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 src/project/utils.ts diff --git a/package.json b/package.json index 9bad2ca..e67d710 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/project-search", - "version": "0.0.2", + "version": "0.0.4", "description": "", "main": "index.js", "scripts": { @@ -24,11 +24,12 @@ "fast-glob": "^3.3.3", "meilisearch": "^0.55.0", "zod": "^4.3.6", - "@parcel/watcher": "^2.5.6", "es-toolkit": "^1.45.1", "eventemitter3": "^5.0.4" }, - "dependencies": {}, + "dependencies": { + "@parcel/watcher": "^2.5.6" + }, "exports": { ".": "./dist/app.js", "./routes": "./dist/app.js", diff --git a/src/project/project-listener/listener.ts b/src/project/project-listener/listener.ts index eddba34..1b74a21 100644 --- a/src/project/project-listener/listener.ts +++ b/src/project/project-listener/listener.ts @@ -2,11 +2,7 @@ import ParcelWatcher, { subscribe } from '@parcel/watcher'; import path from 'node:path'; import fs from 'node:fs'; import { EventEmitter } from "eventemitter3"; -const defaultIgnore = ['node_modules', 'dist', 'pack-dist', 'build', 'out', 'coverage', '.git', '.svn', '.hg', - '.next', - '.astro', - ".swc" -]; +import { normalizeIgnorePattern, defaultIgnorePatterns } from '../utils'; export class ProjectListener { projectPath: string; @@ -30,7 +26,7 @@ export class ProjectListener { .map(line => line.trim()) .filter(line => line && !line.startsWith('#')); } - const allIgnore = [...defaultIgnore, ...ignorePatterns]; + const allIgnore = [...defaultIgnorePatterns, ...ignorePatterns]; const sub = await subscribe( this.projectPath, // 监听路径(支持数组) async (err, events) => { @@ -69,7 +65,7 @@ export class ProjectListener { } }, { - ignore: allIgnore.map(pattern => `**/${pattern}/**`), + ignore: allIgnore.map(normalizeIgnorePattern), backend: 'watchman', // 可选:'fs-events'(默认)、'inotify'、'windows'、'watchman' } ); diff --git a/src/project/project-search/index.ts b/src/project/project-search/index.ts index 068fc06..5f904af 100644 --- a/src/project/project-search/index.ts +++ b/src/project/project-search/index.ts @@ -5,6 +5,7 @@ import fs from 'node:fs'; import fg from 'fast-glob'; import path from 'node:path'; import { Scheduler } from '../../scheduler/index'; +import { normalizeIgnorePattern, defaultIgnorePatterns } from '../utils'; export type FileProjectData = { /** @@ -265,8 +266,7 @@ export class ProjectSearch { .map(l => l.trim()) .filter(l => l && !l.startsWith('#')) : []; - const defaultIgnore = ['node_modules', 'dist', 'pack-dist', 'build', 'out', 'coverage', '.git', '.svn', '.hg', '.next', '.astro', '.swc']; - const allIgnore = [...defaultIgnore, ...gitIgnore]; + const allIgnore = [...defaultIgnorePatterns, ...gitIgnore].map(normalizeIgnorePattern); const files = await fg('**', { cwd: projectPath, diff --git a/src/project/util/git.ts b/src/project/util/git.ts index 95c83b5..3dd8ff0 100644 --- a/src/project/util/git.ts +++ b/src/project/util/git.ts @@ -10,7 +10,7 @@ export const getGitPathname = (repoPath: string): string | null => { } return null; } catch (err) { - console.error(`Failed to get remote URL for repo at ${repoPath}:`, err); + console.warn(`[getGitPathname] Failed to get git remote url for ${repoPath}:`); return null; } }; \ No newline at end of file diff --git a/src/project/utils.ts b/src/project/utils.ts new file mode 100644 index 0000000..89ec4f0 --- /dev/null +++ b/src/project/utils.ts @@ -0,0 +1,36 @@ +/** + * 规范化 ignore 模式,用于文件和目录监听器的 ignore 选项 + * - 如果已包含通配符 (* 或 ?),保持原样 + * - 如果以 / 结尾,视为目录 + * - 其他情况默认视为目录 + */ +export function normalizeIgnorePattern(pattern: string): string { + // 如果已经是完整的 glob 模式(包含通配符),保持原样 + if (pattern.includes('*') || pattern.includes('?')) { + return pattern; + } + // 以斜杠结尾 → 视为目录,加 /** + if (pattern.endsWith('/')) { + return `**/${pattern.replace(/\/$/, '')}/**`; + } + // 其他情况(文件名或目录名),加 /** + return `**/${pattern}/**`; +} + +/** + * 默认忽略的目录/文件列表 + */ +export const defaultIgnorePatterns = [ + 'node_modules', + 'dist', + 'pack-dist', + 'build', + 'out', + 'coverage', + '.git', + '.svn', + '.hg', + '.next', + '.astro', + '.swc', +];