@mandujs/core 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/constants.ts +15 -0
- package/src/content/content-layer.ts +314 -0
- package/src/content/content.test.ts +433 -0
- package/src/content/data-store.ts +245 -0
- package/src/content/digest.ts +133 -0
- package/src/content/index.ts +164 -0
- package/src/content/loader-context.ts +172 -0
- package/src/content/loaders/api.ts +216 -0
- package/src/content/loaders/file.ts +169 -0
- package/src/content/loaders/glob.ts +252 -0
- package/src/content/loaders/index.ts +34 -0
- package/src/content/loaders/types.ts +137 -0
- package/src/content/meta-store.ts +209 -0
- package/src/content/types.ts +282 -0
- package/src/content/watcher.ts +135 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MetaStore - 동기화 메타데이터 저장소
|
|
3
|
+
*
|
|
4
|
+
* API 동기화 토큰, 마지막 수정 시간, 커서 등
|
|
5
|
+
* 로더가 증분 업데이트를 수행하는 데 필요한 메타데이터 저장
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { MetaStore } from "./types";
|
|
9
|
+
import * as fs from "fs";
|
|
10
|
+
import * as path from "path";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* MetaStore 옵션
|
|
14
|
+
*/
|
|
15
|
+
export interface MetaStoreOptions {
|
|
16
|
+
/** 영속화 파일 경로 */
|
|
17
|
+
filePath?: string;
|
|
18
|
+
/** 자동 저장 활성화 */
|
|
19
|
+
autoSave?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 직렬화된 메타 스토어 형식
|
|
24
|
+
*/
|
|
25
|
+
interface SerializedMetaStore {
|
|
26
|
+
version: number;
|
|
27
|
+
collections: Record<string, Record<string, string>>;
|
|
28
|
+
timestamp: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const META_VERSION = 1;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 콘텐츠 메타 저장소 구현
|
|
35
|
+
*/
|
|
36
|
+
export class ContentMetaStore {
|
|
37
|
+
private collections: Map<string, Map<string, string>> = new Map();
|
|
38
|
+
private options: MetaStoreOptions;
|
|
39
|
+
private dirty: boolean = false;
|
|
40
|
+
|
|
41
|
+
constructor(options: MetaStoreOptions = {}) {
|
|
42
|
+
this.options = {
|
|
43
|
+
autoSave: true,
|
|
44
|
+
...options,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 컬렉션용 MetaStore 인터페이스 생성
|
|
50
|
+
*/
|
|
51
|
+
getStore(collection: string): MetaStore {
|
|
52
|
+
if (!this.collections.has(collection)) {
|
|
53
|
+
this.collections.set(collection, new Map());
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const store = this.collections.get(collection)!;
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
get: (key: string) => store.get(key),
|
|
60
|
+
|
|
61
|
+
set: (key: string, value: string) => {
|
|
62
|
+
store.set(key, value);
|
|
63
|
+
this.dirty = true;
|
|
64
|
+
this.autoSave();
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
has: (key: string) => store.has(key),
|
|
68
|
+
|
|
69
|
+
delete: (key: string) => {
|
|
70
|
+
if (store.has(key)) {
|
|
71
|
+
store.delete(key);
|
|
72
|
+
this.dirty = true;
|
|
73
|
+
this.autoSave();
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
clear: () => {
|
|
78
|
+
if (store.size > 0) {
|
|
79
|
+
store.clear();
|
|
80
|
+
this.dirty = true;
|
|
81
|
+
this.autoSave();
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
entries: () => Array.from(store.entries()),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 컬렉션 삭제
|
|
91
|
+
*/
|
|
92
|
+
deleteCollection(collection: string): void {
|
|
93
|
+
if (this.collections.has(collection)) {
|
|
94
|
+
this.collections.delete(collection);
|
|
95
|
+
this.dirty = true;
|
|
96
|
+
this.autoSave();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 자동 저장
|
|
102
|
+
*/
|
|
103
|
+
private autoSave(): void {
|
|
104
|
+
if (this.options.autoSave && this.options.filePath) {
|
|
105
|
+
this.save();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 파일에서 로드
|
|
111
|
+
*/
|
|
112
|
+
async load(): Promise<void> {
|
|
113
|
+
if (!this.options.filePath) return;
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
if (!fs.existsSync(this.options.filePath)) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const content = fs.readFileSync(this.options.filePath, "utf-8");
|
|
121
|
+
const data: SerializedMetaStore = JSON.parse(content);
|
|
122
|
+
|
|
123
|
+
if (data.version !== META_VERSION) {
|
|
124
|
+
console.warn(
|
|
125
|
+
`[MetaStore] Version mismatch (${data.version} vs ${META_VERSION}), starting fresh`
|
|
126
|
+
);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const [collection, entries] of Object.entries(data.collections)) {
|
|
131
|
+
const store = new Map<string, string>();
|
|
132
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
133
|
+
store.set(key, value);
|
|
134
|
+
}
|
|
135
|
+
this.collections.set(collection, store);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this.dirty = false;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.warn(
|
|
141
|
+
`[MetaStore] Failed to load:`,
|
|
142
|
+
error instanceof Error ? error.message : error
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 파일에 저장
|
|
149
|
+
*/
|
|
150
|
+
async save(): Promise<void> {
|
|
151
|
+
if (!this.options.filePath || !this.dirty) return;
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
const dir = path.dirname(this.options.filePath);
|
|
155
|
+
if (!fs.existsSync(dir)) {
|
|
156
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const data: SerializedMetaStore = {
|
|
160
|
+
version: META_VERSION,
|
|
161
|
+
collections: {},
|
|
162
|
+
timestamp: new Date().toISOString(),
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
for (const [collection, store] of this.collections) {
|
|
166
|
+
data.collections[collection] = Object.fromEntries(store);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
fs.writeFileSync(this.options.filePath, JSON.stringify(data, null, 2));
|
|
170
|
+
this.dirty = false;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
console.error(
|
|
173
|
+
`[MetaStore] Failed to save:`,
|
|
174
|
+
error instanceof Error ? error.message : error
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 전체 스토어 초기화
|
|
181
|
+
*/
|
|
182
|
+
clear(): void {
|
|
183
|
+
this.collections.clear();
|
|
184
|
+
this.dirty = true;
|
|
185
|
+
this.autoSave();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* 스토어 통계
|
|
190
|
+
*/
|
|
191
|
+
getStats(): { collections: number; totalKeys: number } {
|
|
192
|
+
let totalKeys = 0;
|
|
193
|
+
for (const store of this.collections.values()) {
|
|
194
|
+
totalKeys += store.size;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
collections: this.collections.size,
|
|
199
|
+
totalKeys,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* MetaStore 팩토리
|
|
206
|
+
*/
|
|
207
|
+
export function createMetaStore(options?: MetaStoreOptions): ContentMetaStore {
|
|
208
|
+
return new ContentMetaStore(options);
|
|
209
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content Layer Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Astro Content Layer에서 영감받은 빌드 타임 콘텐츠 로딩 시스템
|
|
5
|
+
* 다양한 소스(파일, API, DB)에서 콘텐츠를 로드하고 Zod 스키마로 검증
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { z, ZodSchema } from "zod";
|
|
9
|
+
|
|
10
|
+
// ============================================================================
|
|
11
|
+
// Core Types
|
|
12
|
+
// ============================================================================
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 렌더링된 콘텐츠 (Markdown → HTML)
|
|
16
|
+
*/
|
|
17
|
+
export interface RenderedContent {
|
|
18
|
+
/** 렌더링된 HTML */
|
|
19
|
+
html: string;
|
|
20
|
+
/** 추출된 헤딩 목록 */
|
|
21
|
+
headings?: ContentHeading[];
|
|
22
|
+
/** 렌더링 메타데이터 */
|
|
23
|
+
metadata?: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 콘텐츠 헤딩 정보
|
|
28
|
+
*/
|
|
29
|
+
export interface ContentHeading {
|
|
30
|
+
depth: 1 | 2 | 3 | 4 | 5 | 6;
|
|
31
|
+
slug: string;
|
|
32
|
+
text: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 데이터 엔트리 - DataStore에 저장되는 단위
|
|
37
|
+
*/
|
|
38
|
+
export interface DataEntry<T = Record<string, unknown>> {
|
|
39
|
+
/** 고유 식별자 */
|
|
40
|
+
id: string;
|
|
41
|
+
/** 검증된 데이터 */
|
|
42
|
+
data: T;
|
|
43
|
+
/** 원본 파일 경로 (파일 기반 로더) */
|
|
44
|
+
filePath?: string;
|
|
45
|
+
/** 본문 콘텐츠 (Markdown 등) */
|
|
46
|
+
body?: string;
|
|
47
|
+
/** 변경 감지용 다이제스트 */
|
|
48
|
+
digest?: string;
|
|
49
|
+
/** 렌더링된 콘텐츠 */
|
|
50
|
+
rendered?: RenderedContent;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 컬렉션 정의
|
|
55
|
+
*/
|
|
56
|
+
export interface CollectionConfig<T = unknown> {
|
|
57
|
+
/** 콘텐츠 로더 */
|
|
58
|
+
loader: Loader;
|
|
59
|
+
/** Zod 스키마 (검증용) */
|
|
60
|
+
schema?: ZodSchema<T>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Content 설정 파일 타입 (content.config.ts)
|
|
65
|
+
*/
|
|
66
|
+
export interface ContentConfig {
|
|
67
|
+
collections: Record<string, CollectionConfig>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ============================================================================
|
|
71
|
+
// Loader Types
|
|
72
|
+
// ============================================================================
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 로더 인터페이스 - 콘텐츠 소스 정의
|
|
76
|
+
*/
|
|
77
|
+
export interface Loader {
|
|
78
|
+
/** 로더 이름 */
|
|
79
|
+
name: string;
|
|
80
|
+
/** 콘텐츠 로드 함수 */
|
|
81
|
+
load: (context: LoaderContext) => Promise<void>;
|
|
82
|
+
/** 스키마 (로더 내부 기본값) */
|
|
83
|
+
schema?: ZodSchema | (() => ZodSchema | Promise<ZodSchema>);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 로더 컨텍스트 - load() 함수에 전달
|
|
88
|
+
*/
|
|
89
|
+
export interface LoaderContext {
|
|
90
|
+
/** 컬렉션 이름 */
|
|
91
|
+
collection: string;
|
|
92
|
+
/** 데이터 저장소 */
|
|
93
|
+
store: DataStore;
|
|
94
|
+
/** 메타 저장소 (동기화 토큰 등) */
|
|
95
|
+
meta: MetaStore;
|
|
96
|
+
/** 로거 */
|
|
97
|
+
logger: ContentLogger;
|
|
98
|
+
/** Mandu 설정 */
|
|
99
|
+
config: ManduContentConfig;
|
|
100
|
+
/** 데이터 파싱 및 검증 */
|
|
101
|
+
parseData: <T>(options: ParseDataOptions<T>) => Promise<T>;
|
|
102
|
+
/** 다이제스트 생성 */
|
|
103
|
+
generateDigest: (data: unknown) => string;
|
|
104
|
+
/** Markdown 렌더링 (선택) */
|
|
105
|
+
renderMarkdown?: (content: string) => Promise<RenderedContent>;
|
|
106
|
+
/** 파일 감시자 (dev 모드) */
|
|
107
|
+
watcher?: ContentWatcher;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* parseData 옵션
|
|
112
|
+
*/
|
|
113
|
+
export interface ParseDataOptions<T> {
|
|
114
|
+
/** 엔트리 ID */
|
|
115
|
+
id: string;
|
|
116
|
+
/** 원본 데이터 */
|
|
117
|
+
data: unknown;
|
|
118
|
+
/** 파일 경로 (있으면) */
|
|
119
|
+
filePath?: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ============================================================================
|
|
123
|
+
// Store Interfaces
|
|
124
|
+
// ============================================================================
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 데이터 저장소 인터페이스
|
|
128
|
+
*/
|
|
129
|
+
export interface DataStore {
|
|
130
|
+
/** 엔트리 조회 */
|
|
131
|
+
get<T = Record<string, unknown>>(id: string): DataEntry<T> | undefined;
|
|
132
|
+
/** 엔트리 저장 (변경 시 true 반환) */
|
|
133
|
+
set<T = Record<string, unknown>>(entry: DataEntry<T>): boolean;
|
|
134
|
+
/** 엔트리 삭제 */
|
|
135
|
+
delete(id: string): void;
|
|
136
|
+
/** 전체 삭제 */
|
|
137
|
+
clear(): void;
|
|
138
|
+
/** 모든 엔트리 조회 */
|
|
139
|
+
entries<T = Record<string, unknown>>(): Array<[string, DataEntry<T>]>;
|
|
140
|
+
/** 엔트리 존재 여부 */
|
|
141
|
+
has(id: string): boolean;
|
|
142
|
+
/** 엔트리 개수 */
|
|
143
|
+
size(): number;
|
|
144
|
+
/** ID 목록 */
|
|
145
|
+
keys(): string[];
|
|
146
|
+
/** 값 목록 */
|
|
147
|
+
values<T = Record<string, unknown>>(): Array<DataEntry<T>>;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 메타 저장소 인터페이스 (동기화 토큰, 커서 등)
|
|
152
|
+
*/
|
|
153
|
+
export interface MetaStore {
|
|
154
|
+
/** 메타 값 조회 */
|
|
155
|
+
get(key: string): string | undefined;
|
|
156
|
+
/** 메타 값 저장 */
|
|
157
|
+
set(key: string, value: string): void;
|
|
158
|
+
/** 메타 값 존재 여부 */
|
|
159
|
+
has(key: string): boolean;
|
|
160
|
+
/** 메타 값 삭제 */
|
|
161
|
+
delete(key: string): void;
|
|
162
|
+
/** 전체 삭제 */
|
|
163
|
+
clear(): void;
|
|
164
|
+
/** 모든 엔트리 */
|
|
165
|
+
entries(): Array<[string, string]>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ============================================================================
|
|
169
|
+
// Logger & Watcher
|
|
170
|
+
// ============================================================================
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 콘텐츠 로거
|
|
174
|
+
*/
|
|
175
|
+
export interface ContentLogger {
|
|
176
|
+
info(message: string): void;
|
|
177
|
+
warn(message: string): void;
|
|
178
|
+
error(message: string): void;
|
|
179
|
+
debug(message: string): void;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 콘텐츠 파일 감시자 (dev 모드)
|
|
184
|
+
*/
|
|
185
|
+
export interface ContentWatcher {
|
|
186
|
+
/** 파일 감시 추가 */
|
|
187
|
+
add(paths: string | string[]): void;
|
|
188
|
+
/** 파일 감시 제거 */
|
|
189
|
+
remove(paths: string | string[]): void;
|
|
190
|
+
/** 변경 이벤트 핸들러 */
|
|
191
|
+
on(event: "change" | "add" | "unlink", handler: (path: string) => void): void;
|
|
192
|
+
/** 감시 중지 */
|
|
193
|
+
close(): Promise<void>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ============================================================================
|
|
197
|
+
// Config Types
|
|
198
|
+
// ============================================================================
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Mandu Content 설정
|
|
202
|
+
*/
|
|
203
|
+
export interface ManduContentConfig {
|
|
204
|
+
/** 프로젝트 루트 */
|
|
205
|
+
root: string;
|
|
206
|
+
/** 콘텐츠 디렉토리 */
|
|
207
|
+
contentDir?: string;
|
|
208
|
+
/** 출력 디렉토리 */
|
|
209
|
+
outDir?: string;
|
|
210
|
+
/** 개발 모드 */
|
|
211
|
+
isDev?: boolean;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ============================================================================
|
|
215
|
+
// Error Types
|
|
216
|
+
// ============================================================================
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 콘텐츠 에러 기본 클래스
|
|
220
|
+
*/
|
|
221
|
+
export class ContentError extends Error {
|
|
222
|
+
constructor(
|
|
223
|
+
message: string,
|
|
224
|
+
public code: string,
|
|
225
|
+
public collection?: string,
|
|
226
|
+
public entryId?: string
|
|
227
|
+
) {
|
|
228
|
+
super(message);
|
|
229
|
+
this.name = "ContentError";
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 로더 에러
|
|
235
|
+
*/
|
|
236
|
+
export class LoaderError extends ContentError {
|
|
237
|
+
constructor(message: string, collection?: string) {
|
|
238
|
+
super(message, "LOADER_ERROR", collection);
|
|
239
|
+
this.name = "LoaderError";
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 파싱 에러
|
|
245
|
+
*/
|
|
246
|
+
export class ParseError extends ContentError {
|
|
247
|
+
constructor(message: string, collection?: string, entryId?: string) {
|
|
248
|
+
super(message, "PARSE_ERROR", collection, entryId);
|
|
249
|
+
this.name = "ParseError";
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* 검증 에러
|
|
255
|
+
*/
|
|
256
|
+
export class ValidationError extends ContentError {
|
|
257
|
+
constructor(
|
|
258
|
+
message: string,
|
|
259
|
+
public zodError: z.ZodError,
|
|
260
|
+
collection?: string,
|
|
261
|
+
entryId?: string
|
|
262
|
+
) {
|
|
263
|
+
super(message, "VALIDATION_ERROR", collection, entryId);
|
|
264
|
+
this.name = "ValidationError";
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ============================================================================
|
|
269
|
+
// Helper Types
|
|
270
|
+
// ============================================================================
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* 컬렉션의 데이터 타입 추론
|
|
274
|
+
*/
|
|
275
|
+
export type InferEntryData<T extends CollectionConfig> = T["schema"] extends ZodSchema<infer U>
|
|
276
|
+
? U
|
|
277
|
+
: Record<string, unknown>;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* 컬렉션 엔트리 타입 (schema 포함)
|
|
281
|
+
*/
|
|
282
|
+
export type CollectionEntry<T extends CollectionConfig> = DataEntry<InferEntryData<T>>;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content Watcher - 개발 모드 파일 감시
|
|
3
|
+
*
|
|
4
|
+
* 콘텐츠 파일 변경 시 자동 리로드
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ContentWatcher } from "./types";
|
|
8
|
+
import chokidar, { type FSWatcher } from "chokidar";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* ContentWatcher 옵션
|
|
12
|
+
*/
|
|
13
|
+
export interface ContentWatcherOptions {
|
|
14
|
+
/** 루트 디렉토리 */
|
|
15
|
+
root: string;
|
|
16
|
+
/** 무시할 패턴 */
|
|
17
|
+
ignored?: string[];
|
|
18
|
+
/** 디바운스 (ms) */
|
|
19
|
+
debounce?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* ContentWatcher 구현
|
|
24
|
+
*/
|
|
25
|
+
class ContentWatcherImpl implements ContentWatcher {
|
|
26
|
+
private watcher: FSWatcher;
|
|
27
|
+
private handlers: Map<string, Set<(path: string) => void>> = new Map();
|
|
28
|
+
private debounceTimers: Map<string, NodeJS.Timeout> = new Map();
|
|
29
|
+
private debounceMs: number;
|
|
30
|
+
|
|
31
|
+
constructor(options: ContentWatcherOptions) {
|
|
32
|
+
const { root, ignored = ["**/node_modules/**", "**/.git/**"], debounce = 300 } = options;
|
|
33
|
+
|
|
34
|
+
this.debounceMs = debounce;
|
|
35
|
+
|
|
36
|
+
this.watcher = chokidar.watch([], {
|
|
37
|
+
cwd: root,
|
|
38
|
+
ignored,
|
|
39
|
+
ignoreInitial: true,
|
|
40
|
+
awaitWriteFinish: {
|
|
41
|
+
stabilityThreshold: debounce,
|
|
42
|
+
pollInterval: 100,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// 이벤트 핸들러 연결
|
|
47
|
+
this.watcher.on("change", (path) => this.emit("change", path));
|
|
48
|
+
this.watcher.on("add", (path) => this.emit("add", path));
|
|
49
|
+
this.watcher.on("unlink", (path) => this.emit("unlink", path));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 파일/패턴 감시 추가
|
|
54
|
+
*/
|
|
55
|
+
add(paths: string | string[]): void {
|
|
56
|
+
this.watcher.add(paths);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 파일/패턴 감시 제거
|
|
61
|
+
*/
|
|
62
|
+
remove(paths: string | string[]): void {
|
|
63
|
+
this.watcher.unwatch(paths);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 이벤트 핸들러 등록
|
|
68
|
+
*/
|
|
69
|
+
on(event: "change" | "add" | "unlink", handler: (path: string) => void): void {
|
|
70
|
+
if (!this.handlers.has(event)) {
|
|
71
|
+
this.handlers.set(event, new Set());
|
|
72
|
+
}
|
|
73
|
+
this.handlers.get(event)!.add(handler);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 이벤트 핸들러 제거
|
|
78
|
+
*/
|
|
79
|
+
off(event: "change" | "add" | "unlink", handler: (path: string) => void): void {
|
|
80
|
+
this.handlers.get(event)?.delete(handler);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 감시 종료
|
|
85
|
+
*/
|
|
86
|
+
async close(): Promise<void> {
|
|
87
|
+
// 모든 디바운스 타이머 정리
|
|
88
|
+
for (const timer of this.debounceTimers.values()) {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
}
|
|
91
|
+
this.debounceTimers.clear();
|
|
92
|
+
|
|
93
|
+
await this.watcher.close();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 이벤트 발생 (디바운스 적용)
|
|
98
|
+
*/
|
|
99
|
+
private emit(event: "change" | "add" | "unlink", path: string): void {
|
|
100
|
+
const key = `${event}:${path}`;
|
|
101
|
+
|
|
102
|
+
// 기존 타이머 취소
|
|
103
|
+
if (this.debounceTimers.has(key)) {
|
|
104
|
+
clearTimeout(this.debounceTimers.get(key)!);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 디바운스 적용
|
|
108
|
+
const timer = setTimeout(() => {
|
|
109
|
+
this.debounceTimers.delete(key);
|
|
110
|
+
|
|
111
|
+
const handlers = this.handlers.get(event);
|
|
112
|
+
if (handlers) {
|
|
113
|
+
for (const handler of handlers) {
|
|
114
|
+
try {
|
|
115
|
+
handler(path);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error(
|
|
118
|
+
`[ContentWatcher] Handler error for ${event}:`,
|
|
119
|
+
error instanceof Error ? error.message : error
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}, this.debounceMs);
|
|
125
|
+
|
|
126
|
+
this.debounceTimers.set(key, timer);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* ContentWatcher 팩토리
|
|
132
|
+
*/
|
|
133
|
+
export function createContentWatcher(options: ContentWatcherOptions): ContentWatcher {
|
|
134
|
+
return new ContentWatcherImpl(options);
|
|
135
|
+
}
|