tokelytics 0.1.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.
@@ -0,0 +1,2891 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/config.ts
4
+ import { promises as fs3 } from "node:fs";
5
+ import * as path5 from "node:path";
6
+
7
+ // src/paths.ts
8
+ import { promises as fs2 } from "node:fs";
9
+ import * as os3 from "node:os";
10
+ import * as path4 from "node:path";
11
+ import { randomUUID } from "node:crypto";
12
+
13
+ // ../packages/core/dist/types.js
14
+ function emptyScanState() {
15
+ return { files: {} };
16
+ }
17
+
18
+ // ../packages/core/dist/model-priority.js
19
+ var MODEL_PRIORITY = {
20
+ fable: 6,
21
+ mythos: 6,
22
+ "gpt-5.5": 5,
23
+ "gpt-5.4": 4,
24
+ "gpt-5": 4,
25
+ opus: 3,
26
+ sonnet: 2,
27
+ haiku: 1
28
+ };
29
+ function modelPriority(model) {
30
+ if (!model)
31
+ return 0;
32
+ const m = model.toLowerCase();
33
+ for (const [keyword, priority] of Object.entries(MODEL_PRIORITY)) {
34
+ if (m.includes(keyword))
35
+ return priority;
36
+ }
37
+ return 0;
38
+ }
39
+
40
+ // ../packages/core/dist/util.js
41
+ function projectNameFromCwd(cwd) {
42
+ if (!cwd)
43
+ return "unknown";
44
+ const parts = cwd.replace(/\\/g, "/").replace(/\/+$/, "").split("/");
45
+ if (parts.length >= 2)
46
+ return parts.slice(-2).join("/");
47
+ return parts[parts.length - 1] || "unknown";
48
+ }
49
+ function toCount(v) {
50
+ return typeof v === "number" && Number.isFinite(v) && v > 0 ? Math.trunc(v) : 0;
51
+ }
52
+
53
+ // ../packages/core/dist/aggregate.js
54
+ function dayOf(ts) {
55
+ return ts && ts.length >= 10 ? ts.slice(0, 10) : "";
56
+ }
57
+ function hourOf(ts) {
58
+ if (!ts || ts.length < 13)
59
+ return null;
60
+ const h = Number.parseInt(ts.slice(11, 13), 10);
61
+ return Number.isFinite(h) ? h : null;
62
+ }
63
+ function rollupId(provider, day) {
64
+ return `${provider}_${day}`;
65
+ }
66
+ function aggregateSession(sessionId, turns) {
67
+ const agg = {
68
+ sessionId,
69
+ provider: turns[0]?.provider ?? "claude",
70
+ project: "unknown",
71
+ gitBranch: "",
72
+ firstTs: "",
73
+ lastTs: "",
74
+ model: "",
75
+ turnCount: 0,
76
+ inputTokens: 0,
77
+ outputTokens: 0,
78
+ cacheReadTokens: 0,
79
+ cacheCreationTokens: 0,
80
+ cachedInputTokens: 0,
81
+ reasoningTokens: 0
82
+ };
83
+ for (const t of turns) {
84
+ agg.turnCount++;
85
+ agg.inputTokens += t.inputTokens;
86
+ agg.outputTokens += t.outputTokens;
87
+ agg.cacheReadTokens += t.cacheReadTokens;
88
+ agg.cacheCreationTokens += t.cacheCreationTokens;
89
+ agg.cachedInputTokens += t.cachedInputTokens;
90
+ agg.reasoningTokens += t.reasoningTokens;
91
+ if (t.ts && (!agg.firstTs || t.ts < agg.firstTs))
92
+ agg.firstTs = t.ts;
93
+ if (t.ts && (!agg.lastTs || t.ts > agg.lastTs))
94
+ agg.lastTs = t.ts;
95
+ if (t.project && agg.project === "unknown")
96
+ agg.project = t.project;
97
+ if (t.gitBranch && !agg.gitBranch)
98
+ agg.gitBranch = t.gitBranch;
99
+ if (t.model && modelPriority(t.model) > modelPriority(agg.model))
100
+ agg.model = t.model;
101
+ }
102
+ if (!agg.model) {
103
+ const withModel = turns.find((t) => t.model);
104
+ if (withModel)
105
+ agg.model = withModel.model;
106
+ }
107
+ return agg;
108
+ }
109
+ function emptyModelTotals(model) {
110
+ return {
111
+ model,
112
+ turns: 0,
113
+ inputTokens: 0,
114
+ outputTokens: 0,
115
+ cacheReadTokens: 0,
116
+ cacheCreationTokens: 0,
117
+ cachedInputTokens: 0,
118
+ reasoningTokens: 0
119
+ };
120
+ }
121
+ function buildRollup(provider, day, turns) {
122
+ const models = {};
123
+ const hours = /* @__PURE__ */ new Map();
124
+ for (const t of turns) {
125
+ const key = t.model || "unknown";
126
+ const m = models[key] ??= emptyModelTotals(key);
127
+ m.turns++;
128
+ m.inputTokens += t.inputTokens;
129
+ m.outputTokens += t.outputTokens;
130
+ m.cacheReadTokens += t.cacheReadTokens;
131
+ m.cacheCreationTokens += t.cacheCreationTokens;
132
+ m.cachedInputTokens += t.cachedInputTokens;
133
+ m.reasoningTokens += t.reasoningTokens;
134
+ const h = hourOf(t.ts);
135
+ if (h !== null) {
136
+ const bucket = hours.get(h) ?? { hour: h, output: 0, turns: 0 };
137
+ bucket.output += t.outputTokens;
138
+ bucket.turns++;
139
+ hours.set(h, bucket);
140
+ }
141
+ }
142
+ return {
143
+ id: rollupId(provider, day),
144
+ provider,
145
+ day,
146
+ models,
147
+ hourly: [...hours.values()].sort((a, b) => a.hour - b.hour)
148
+ };
149
+ }
150
+ function affectedKeys(turns) {
151
+ const sessionIds = /* @__PURE__ */ new Set();
152
+ const dayBuckets = /* @__PURE__ */ new Map();
153
+ for (const t of turns) {
154
+ sessionIds.add(t.sessionId);
155
+ const day = dayOf(t.ts);
156
+ if (day)
157
+ dayBuckets.set(rollupId(t.provider, day), { provider: t.provider, day });
158
+ }
159
+ return { sessionIds: [...sessionIds], dayBuckets: [...dayBuckets.values()] };
160
+ }
161
+
162
+ // ../packages/core/dist/providers/claude.js
163
+ import * as os from "node:os";
164
+ import * as path2 from "node:path";
165
+
166
+ // ../packages/core/dist/fs-scan.js
167
+ import { promises as fs } from "node:fs";
168
+ import * as path from "node:path";
169
+ async function walkFiles(root, match) {
170
+ const out = [];
171
+ async function recurse(dir) {
172
+ let entries;
173
+ try {
174
+ entries = await fs.readdir(dir, { withFileTypes: true });
175
+ } catch {
176
+ return;
177
+ }
178
+ for (const entry of entries) {
179
+ const full = path.join(dir, entry.name);
180
+ if (entry.isDirectory()) {
181
+ await recurse(full);
182
+ } else if (entry.isFile() && match(entry.name)) {
183
+ out.push(full);
184
+ }
185
+ }
186
+ }
187
+ await recurse(root);
188
+ out.sort();
189
+ return out;
190
+ }
191
+ async function fileMtimeMs(filePath) {
192
+ try {
193
+ const st = await fs.stat(filePath);
194
+ return st.mtimeMs;
195
+ } catch {
196
+ return null;
197
+ }
198
+ }
199
+ async function readJsonlFrom(filePath, fromLine) {
200
+ let text;
201
+ try {
202
+ text = await fs.readFile(filePath, "utf-8");
203
+ } catch {
204
+ return { totalLines: 0, entries: [] };
205
+ }
206
+ const lines = text.split("\n");
207
+ if (lines.length > 0 && lines[lines.length - 1] === "")
208
+ lines.pop();
209
+ const entries = [];
210
+ for (let i = 0; i < lines.length; i++) {
211
+ const lineNo = i + 1;
212
+ if (lineNo <= fromLine)
213
+ continue;
214
+ const raw = lines[i].trim();
215
+ if (!raw)
216
+ continue;
217
+ let record;
218
+ try {
219
+ record = JSON.parse(raw);
220
+ } catch {
221
+ continue;
222
+ }
223
+ entries.push({ lineNo, record });
224
+ }
225
+ return { totalLines: lines.length, entries };
226
+ }
227
+
228
+ // ../packages/core/dist/providers/claude.js
229
+ var MTIME_EPSILON_MS = 1;
230
+ function defaultClaudeDirs(home = os.homedir()) {
231
+ return [
232
+ path2.join(home, ".claude", "projects"),
233
+ // Xcode coding-assistant integration (macOS); silently skipped if absent.
234
+ path2.join(home, "Library", "Developer", "Xcode", "CodingAssistant", "ClaudeAgentConfig", "projects")
235
+ ];
236
+ }
237
+ function parseClaudeEntries(entries) {
238
+ const seen = /* @__PURE__ */ new Map();
239
+ const noId = [];
240
+ for (const { lineNo, record } of entries) {
241
+ const rec = record;
242
+ const rtype = rec.type;
243
+ if (rtype !== "assistant" && rtype !== "user")
244
+ continue;
245
+ const sessionId = rec.sessionId;
246
+ if (!sessionId)
247
+ continue;
248
+ if (rtype !== "assistant")
249
+ continue;
250
+ const msg = rec.message ?? {};
251
+ const usage = msg.usage ?? {};
252
+ const inputTokens = toCount(usage.input_tokens);
253
+ const outputTokens = toCount(usage.output_tokens);
254
+ const cacheReadTokens = toCount(usage.cache_read_input_tokens);
255
+ const cacheCreationTokens = toCount(usage.cache_creation_input_tokens);
256
+ if (inputTokens + outputTokens + cacheReadTokens + cacheCreationTokens === 0)
257
+ continue;
258
+ let toolName = null;
259
+ for (const item of msg.content ?? []) {
260
+ if (item && typeof item === "object" && item.type === "tool_use") {
261
+ toolName = item.name ?? null;
262
+ break;
263
+ }
264
+ }
265
+ const messageId = msg.id ?? "";
266
+ const cwd = rec.cwd ?? "";
267
+ const turn = {
268
+ provider: "claude",
269
+ turnId: messageId ? `claude:${messageId}` : `claude:noid:${sessionId}:${lineNo}`,
270
+ sessionId,
271
+ ts: rec.timestamp ?? "",
272
+ model: msg.model ?? "",
273
+ inputTokens,
274
+ outputTokens,
275
+ cacheReadTokens,
276
+ cacheCreationTokens,
277
+ cachedInputTokens: 0,
278
+ reasoningTokens: 0,
279
+ toolName,
280
+ project: projectNameFromCwd(cwd),
281
+ cwd,
282
+ gitBranch: rec.gitBranch ?? ""
283
+ };
284
+ if (messageId)
285
+ seen.set(messageId, turn);
286
+ else
287
+ noId.push(turn);
288
+ }
289
+ return [...noId, ...seen.values()];
290
+ }
291
+ var ClaudeConnector = class {
292
+ id = "claude";
293
+ dirs;
294
+ constructor(dirs = defaultClaudeDirs()) {
295
+ this.dirs = dirs;
296
+ }
297
+ watchPaths() {
298
+ return this.dirs;
299
+ }
300
+ async collect(state) {
301
+ const nextFiles = { ...state.files };
302
+ const turns = [];
303
+ for (const dir of this.dirs) {
304
+ const files = await walkFiles(dir, (name) => name.endsWith(".jsonl"));
305
+ for (const filePath of files) {
306
+ const mtimeMs = await fileMtimeMs(filePath);
307
+ if (mtimeMs === null)
308
+ continue;
309
+ const cursor = state.files[filePath];
310
+ if (cursor && Math.abs(cursor.mtimeMs - mtimeMs) < MTIME_EPSILON_MS) {
311
+ continue;
312
+ }
313
+ const fromLine = cursor ? cursor.lines : 0;
314
+ const { totalLines, entries } = await readJsonlFrom(filePath, fromLine);
315
+ if (entries.length > 0) {
316
+ turns.push(...parseClaudeEntries(entries));
317
+ }
318
+ nextFiles[filePath] = { mtimeMs, lines: totalLines };
319
+ }
320
+ }
321
+ return { turns, state: { files: nextFiles } };
322
+ }
323
+ };
324
+
325
+ // ../packages/core/dist/providers/codex.js
326
+ import * as os2 from "node:os";
327
+ import * as path3 from "node:path";
328
+ var MTIME_EPSILON_MS2 = 1;
329
+ function defaultCodexDirs(home = os2.homedir()) {
330
+ return [path3.join(home, ".codex", "sessions")];
331
+ }
332
+ function usageFromObj(obj) {
333
+ if (!obj || typeof obj !== "object")
334
+ return null;
335
+ const o = obj;
336
+ const hasAny = "input_tokens" in o || "output_tokens" in o || "total_tokens" in o || "reasoning_output_tokens" in o;
337
+ if (!hasAny)
338
+ return null;
339
+ const input = toCount(o["input_tokens"]);
340
+ const cachedInput = toCount(o["cached_input_tokens"]);
341
+ const output = toCount(o["output_tokens"]);
342
+ const reasoning = toCount(o["reasoning_output_tokens"]);
343
+ let total = toCount(o["total_tokens"]);
344
+ if (total === 0)
345
+ total = input + output + reasoning;
346
+ return { input, cachedInput, output, reasoning, total };
347
+ }
348
+ function extractUsageEvent(record) {
349
+ if (!record || typeof record !== "object")
350
+ return null;
351
+ const rec = record;
352
+ const ts = typeof rec["timestamp"] === "string" ? rec["timestamp"] : "";
353
+ const payload = rec["payload"] ?? rec;
354
+ const payloadType = payload["type"];
355
+ if (typeof payloadType === "string" && payloadType !== "token_count")
356
+ return null;
357
+ const info = payload["info"] ?? payload;
358
+ const model = pickModel(rec) ?? pickModel(payload) ?? pickModel(info) ?? "";
359
+ const last = usageFromObj(info["last_token_usage"]);
360
+ if (last)
361
+ return { kind: "delta", usage: last, ts, model };
362
+ const totalUsage = usageFromObj(info["total_token_usage"]) ?? usageFromObj(info["usage"]) ?? usageFromObj(info);
363
+ if (totalUsage)
364
+ return { kind: "cumulative", usage: totalUsage, ts, model };
365
+ return null;
366
+ }
367
+ function pickModel(obj) {
368
+ if (!obj || typeof obj !== "object")
369
+ return null;
370
+ const m = obj["model"];
371
+ return typeof m === "string" && m ? m : null;
372
+ }
373
+ function extractSessionMeta(record) {
374
+ if (!record || typeof record !== "object")
375
+ return null;
376
+ const rec = record;
377
+ const payload = rec["payload"] ?? rec;
378
+ const type = payload["type"] ?? rec["type"];
379
+ const looksLikeMeta = typeof type === "string" && type.toLowerCase().includes("session") || "cwd" in payload || "id" in payload && "instructions" in payload;
380
+ if (!looksLikeMeta)
381
+ return null;
382
+ const cwd = typeof payload["cwd"] === "string" ? payload["cwd"] : "";
383
+ const git = payload["git"];
384
+ const out = {};
385
+ if (typeof payload["id"] === "string")
386
+ out.sessionId = payload["id"];
387
+ const model = pickModel(payload);
388
+ if (model)
389
+ out.model = model;
390
+ if (cwd) {
391
+ out.cwd = cwd;
392
+ out.project = projectNameFromCwd(cwd);
393
+ }
394
+ if (git && typeof git["branch"] === "string")
395
+ out.gitBranch = git["branch"];
396
+ if (typeof rec["timestamp"] === "string")
397
+ out.startTs = rec["timestamp"];
398
+ return out;
399
+ }
400
+ var clamp0 = (n) => n > 0 ? n : 0;
401
+ function parseCodexEntries(fileId, entries) {
402
+ const meta = {
403
+ sessionId: `codex-${fileId}`,
404
+ model: "",
405
+ project: "unknown",
406
+ cwd: "",
407
+ gitBranch: "",
408
+ startTs: ""
409
+ };
410
+ for (const { record } of entries) {
411
+ const m = extractSessionMeta(record);
412
+ if (m)
413
+ Object.assign(meta, m);
414
+ }
415
+ const turns = [];
416
+ let prevCumulative = null;
417
+ let seq = 0;
418
+ let runningModel = meta.model;
419
+ for (const { record } of entries) {
420
+ const recModel = pickModel(record?.["payload"]) ?? pickModel(record);
421
+ if (recModel)
422
+ runningModel = recModel;
423
+ const ev = extractUsageEvent(record);
424
+ if (!ev)
425
+ continue;
426
+ if (ev.model)
427
+ runningModel = ev.model;
428
+ let delta;
429
+ if (ev.kind === "delta") {
430
+ delta = ev.usage;
431
+ } else {
432
+ const base = prevCumulative;
433
+ prevCumulative = ev.usage;
434
+ delta = base ? {
435
+ input: clamp0(ev.usage.input - base.input),
436
+ cachedInput: clamp0(ev.usage.cachedInput - base.cachedInput),
437
+ output: clamp0(ev.usage.output - base.output),
438
+ reasoning: clamp0(ev.usage.reasoning - base.reasoning),
439
+ total: clamp0(ev.usage.total - base.total)
440
+ } : ev.usage;
441
+ }
442
+ if (delta.input + delta.output + delta.reasoning + delta.cachedInput === 0)
443
+ continue;
444
+ turns.push({
445
+ provider: "codex",
446
+ turnId: `codex:${fileId}#${seq}`,
447
+ sessionId: meta.sessionId,
448
+ ts: ev.ts || meta.startTs,
449
+ model: runningModel,
450
+ inputTokens: delta.input,
451
+ outputTokens: delta.output,
452
+ cacheReadTokens: 0,
453
+ cacheCreationTokens: 0,
454
+ cachedInputTokens: delta.cachedInput,
455
+ reasoningTokens: delta.reasoning,
456
+ toolName: null,
457
+ project: meta.project,
458
+ cwd: meta.cwd,
459
+ gitBranch: meta.gitBranch
460
+ });
461
+ seq++;
462
+ }
463
+ return turns;
464
+ }
465
+ function codexFileId(filePath) {
466
+ return path3.basename(filePath).replace(/\.jsonl$/i, "");
467
+ }
468
+ var CodexConnector = class {
469
+ id = "codex";
470
+ dirs;
471
+ constructor(dirs = defaultCodexDirs()) {
472
+ this.dirs = dirs;
473
+ }
474
+ watchPaths() {
475
+ return this.dirs;
476
+ }
477
+ async collect(state) {
478
+ const nextFiles = { ...state.files };
479
+ const turns = [];
480
+ for (const dir of this.dirs) {
481
+ const files = await walkFiles(dir, (name) => name.startsWith("rollout-") && name.endsWith(".jsonl"));
482
+ for (const filePath of files) {
483
+ const mtimeMs = await fileMtimeMs(filePath);
484
+ if (mtimeMs === null)
485
+ continue;
486
+ const cursor = state.files[filePath];
487
+ if (cursor && Math.abs(cursor.mtimeMs - mtimeMs) < MTIME_EPSILON_MS2) {
488
+ continue;
489
+ }
490
+ const { totalLines, entries } = await readJsonlFrom(filePath, 0);
491
+ if (entries.length > 0) {
492
+ turns.push(...parseCodexEntries(codexFileId(filePath), entries));
493
+ }
494
+ nextFiles[filePath] = { mtimeMs, lines: totalLines };
495
+ }
496
+ }
497
+ return { turns, state: { files: nextFiles } };
498
+ }
499
+ };
500
+
501
+ // src/paths.ts
502
+ function configDir() {
503
+ return process.env.TOKELYTICS_HOME ?? path4.join(os3.homedir(), ".tokelytics");
504
+ }
505
+ var statePath = () => path4.join(configDir(), "state.json");
506
+ var credsPath = () => path4.join(configDir(), "credentials.json");
507
+ async function ensureDir() {
508
+ await fs2.mkdir(configDir(), { recursive: true });
509
+ }
510
+ async function readJson(file) {
511
+ try {
512
+ return JSON.parse(await fs2.readFile(file, "utf-8"));
513
+ } catch {
514
+ return null;
515
+ }
516
+ }
517
+ async function writeJson(file, value) {
518
+ await ensureDir();
519
+ await fs2.writeFile(file, JSON.stringify(value, null, 2), "utf-8");
520
+ }
521
+ async function loadState() {
522
+ const s = await readJson(statePath());
523
+ if (s && s.deviceId && s.scan) return s;
524
+ const fresh = { deviceId: randomUUID(), scan: emptyScanState() };
525
+ await writeJson(statePath(), fresh);
526
+ return fresh;
527
+ }
528
+ async function saveState(state) {
529
+ await writeJson(statePath(), state);
530
+ }
531
+ async function loadCredentials() {
532
+ return readJson(credsPath());
533
+ }
534
+ async function saveCredentials(creds) {
535
+ await writeJson(credsPath(), creds);
536
+ try {
537
+ await fs2.chmod(credsPath(), 384);
538
+ } catch {
539
+ }
540
+ }
541
+ async function clearCredentials() {
542
+ try {
543
+ await fs2.rm(credsPath(), { force: true });
544
+ } catch {
545
+ }
546
+ }
547
+
548
+ // src/config.ts
549
+ var DEFAULT_FIREBASE = {
550
+ apiKey: "AIzaSyA7j6CYHRIYlOpP94-fyaWqV5w3l0U5Blw",
551
+ projectId: "tokelytics"
552
+ };
553
+ var DEFAULT_WEB_URL = "https://tokelytics.web.app";
554
+ function fromEnv() {
555
+ const cfg = {};
556
+ const apiKey = process.env.TOKELYTICS_FIREBASE_API_KEY;
557
+ const projectId = process.env.TOKELYTICS_FIREBASE_PROJECT_ID;
558
+ if (apiKey && projectId) cfg.firebase = { apiKey, projectId };
559
+ if (process.env.TOKELYTICS_WEB_URL) cfg.webBaseUrl = process.env.TOKELYTICS_WEB_URL;
560
+ if (process.env.TOKELYTICS_GOOGLE_CLIENT_ID) {
561
+ cfg.google = {
562
+ clientId: process.env.TOKELYTICS_GOOGLE_CLIENT_ID,
563
+ clientSecret: process.env.TOKELYTICS_GOOGLE_CLIENT_SECRET ?? ""
564
+ };
565
+ }
566
+ if (process.env.TOKELYTICS_GITHUB_CLIENT_ID) {
567
+ cfg.github = {
568
+ clientId: process.env.TOKELYTICS_GITHUB_CLIENT_ID,
569
+ clientSecret: process.env.TOKELYTICS_GITHUB_CLIENT_SECRET ?? ""
570
+ };
571
+ }
572
+ const fsHost = process.env.FIRESTORE_EMULATOR_HOST ?? process.env.TOKELYTICS_FIRESTORE_EMULATOR_HOST;
573
+ if (fsHost) cfg.firestoreEmulatorHost = fsHost;
574
+ const authHost = process.env.FIREBASE_AUTH_EMULATOR_HOST ?? process.env.TOKELYTICS_AUTH_EMULATOR_HOST;
575
+ if (authHost) cfg.authEmulatorHost = authHost;
576
+ return cfg;
577
+ }
578
+ async function fromFile() {
579
+ try {
580
+ const raw = await fs3.readFile(path5.join(configDir(), "config.json"), "utf-8");
581
+ return JSON.parse(raw);
582
+ } catch {
583
+ return {};
584
+ }
585
+ }
586
+ async function loadConfig() {
587
+ const file = await fromFile();
588
+ const env = fromEnv();
589
+ return {
590
+ firebase: env.firebase ?? file.firebase ?? DEFAULT_FIREBASE,
591
+ webBaseUrl: (env.webBaseUrl ?? file.webBaseUrl ?? DEFAULT_WEB_URL).replace(/\/+$/, ""),
592
+ google: env.google ?? file.google,
593
+ github: env.github ?? file.github,
594
+ firestoreEmulatorHost: env.firestoreEmulatorHost ?? file.firestoreEmulatorHost,
595
+ authEmulatorHost: env.authEmulatorHost ?? file.authEmulatorHost
596
+ };
597
+ }
598
+
599
+ // src/auth.ts
600
+ import * as http from "node:http";
601
+ import { createHash, randomBytes } from "node:crypto";
602
+ import { spawn } from "node:child_process";
603
+ function identityToolkitBase(cfg) {
604
+ return cfg.authEmulatorHost ? `http://${cfg.authEmulatorHost}/identitytoolkit.googleapis.com` : "https://identitytoolkit.googleapis.com";
605
+ }
606
+ function secureTokenBase(cfg) {
607
+ return cfg.authEmulatorHost ? `http://${cfg.authEmulatorHost}/securetoken.googleapis.com` : "https://securetoken.googleapis.com";
608
+ }
609
+ function openBrowser(url) {
610
+ if (process.env.SSH_CONNECTION || process.env.SSH_TTY || process.env.TOKELYTICS_NO_BROWSER) {
611
+ return false;
612
+ }
613
+ const platform = process.platform;
614
+ const [cmd, args] = platform === "win32" ? ["cmd", ["/c", "start", "", url]] : platform === "darwin" ? ["open", [url]] : ["xdg-open", [url]];
615
+ try {
616
+ spawn(cmd, args, { detached: true, stdio: "ignore" }).unref();
617
+ return true;
618
+ } catch {
619
+ return false;
620
+ }
621
+ }
622
+ function captureOAuthCode(buildUrl) {
623
+ return new Promise((resolve3, reject) => {
624
+ const server = http.createServer((req, res) => {
625
+ const u = new URL(req.url ?? "/", "http://localhost");
626
+ const code = u.searchParams.get("code");
627
+ const error = u.searchParams.get("error");
628
+ res.writeHead(200, { "Content-Type": "text/html" });
629
+ res.end(
630
+ `<html><body style="font-family:system-ui;padding:3rem;text-align:center"><h2>Tokelytics</h2><p>${code ? "You're signed in. You can close this tab." : "Sign-in failed: " + error}</p></body></html>`
631
+ );
632
+ server.close();
633
+ if (code) resolve3({ code });
634
+ else reject(new Error(`OAuth failed: ${error ?? "no code returned"}`));
635
+ });
636
+ server.listen(0, "127.0.0.1", () => {
637
+ const { port } = server.address();
638
+ const redirectUri = `http://127.0.0.1:${port}`;
639
+ const url = buildUrl(redirectUri);
640
+ console.log(`
641
+ Opening your browser to sign in. If it doesn't open, visit:
642
+ ${url}
643
+ `);
644
+ openBrowser(url);
645
+ });
646
+ server.on("error", reject);
647
+ });
648
+ }
649
+ async function signInWithIdp(cfg, postBody, requestUri) {
650
+ const res = await fetch(`${identityToolkitBase(cfg)}/v1/accounts:signInWithIdp?key=${cfg.firebase.apiKey}`, {
651
+ method: "POST",
652
+ headers: { "Content-Type": "application/json" },
653
+ body: JSON.stringify({ postBody, requestUri, returnSecureToken: true, returnIdpCredential: true })
654
+ });
655
+ if (!res.ok) throw new Error(`signInWithIdp failed (${res.status}): ${await res.text()}`);
656
+ const data = await res.json();
657
+ return {
658
+ provider: postBody.includes("github.com") ? "github" : "google",
659
+ uid: data.localId,
660
+ email: data.email,
661
+ refreshToken: data.refreshToken,
662
+ idToken: data.idToken
663
+ };
664
+ }
665
+ async function googleLogin(cfg) {
666
+ if (!cfg.google) throw new Error("Google OAuth client not configured (TOKELYTICS_GOOGLE_CLIENT_ID).");
667
+ const verifier = randomBytes(32).toString("base64url");
668
+ const challenge = createHash("sha256").update(verifier).digest("base64url");
669
+ let redirectUri = "";
670
+ const { code } = await captureOAuthCode((ru) => {
671
+ redirectUri = ru;
672
+ const p = new URLSearchParams({
673
+ client_id: cfg.google.clientId,
674
+ redirect_uri: ru,
675
+ response_type: "code",
676
+ scope: "openid email profile",
677
+ code_challenge: challenge,
678
+ code_challenge_method: "S256"
679
+ });
680
+ return `https://accounts.google.com/o/oauth2/v2/auth?${p}`;
681
+ });
682
+ const tokenRes = await fetch("https://oauth2.googleapis.com/token", {
683
+ method: "POST",
684
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
685
+ body: new URLSearchParams({
686
+ client_id: cfg.google.clientId,
687
+ client_secret: cfg.google.clientSecret,
688
+ code,
689
+ code_verifier: verifier,
690
+ grant_type: "authorization_code",
691
+ redirect_uri: redirectUri
692
+ })
693
+ });
694
+ if (!tokenRes.ok) throw new Error(`Google token exchange failed (${tokenRes.status})`);
695
+ const { id_token } = await tokenRes.json();
696
+ const creds = await signInWithIdp(cfg, `id_token=${id_token}&providerId=google.com`, redirectUri);
697
+ return { provider: "google", uid: creds.uid, email: creds.email, refreshToken: creds.refreshToken };
698
+ }
699
+ async function githubLogin(cfg) {
700
+ if (!cfg.github) throw new Error("GitHub OAuth client not configured (TOKELYTICS_GITHUB_CLIENT_ID).");
701
+ let redirectUri = "";
702
+ const { code } = await captureOAuthCode((ru) => {
703
+ redirectUri = ru;
704
+ const p = new URLSearchParams({ client_id: cfg.github.clientId, redirect_uri: ru, scope: "read:user user:email" });
705
+ return `https://github.com/login/oauth/authorize?${p}`;
706
+ });
707
+ const tokenRes = await fetch("https://github.com/login/oauth/access_token", {
708
+ method: "POST",
709
+ headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" },
710
+ body: new URLSearchParams({
711
+ client_id: cfg.github.clientId,
712
+ client_secret: cfg.github.clientSecret,
713
+ code,
714
+ redirect_uri: redirectUri
715
+ })
716
+ });
717
+ if (!tokenRes.ok) throw new Error(`GitHub token exchange failed (${tokenRes.status})`);
718
+ const { access_token } = await tokenRes.json();
719
+ const creds = await signInWithIdp(cfg, `access_token=${access_token}&providerId=github.com`, redirectUri);
720
+ return { provider: "github", uid: creds.uid, email: creds.email, refreshToken: creds.refreshToken };
721
+ }
722
+ var CORS_HEADERS = {
723
+ "Access-Control-Allow-Origin": "*",
724
+ "Access-Control-Allow-Methods": "POST, OPTIONS",
725
+ "Access-Control-Allow-Headers": "Content-Type"
726
+ };
727
+ function readBody(req) {
728
+ return new Promise((resolve3, reject) => {
729
+ let data = "";
730
+ req.on("data", (chunk) => {
731
+ data += chunk;
732
+ if (data.length > 1e6) req.destroy();
733
+ });
734
+ req.on("end", () => resolve3(data));
735
+ req.on("error", reject);
736
+ });
737
+ }
738
+ async function exchangeRefreshToken(cfg, refreshToken) {
739
+ const res = await fetch(`${secureTokenBase(cfg)}/v1/token?key=${cfg.firebase.apiKey}`, {
740
+ method: "POST",
741
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
742
+ body: new URLSearchParams({ grant_type: "refresh_token", refresh_token: refreshToken })
743
+ });
744
+ if (!res.ok) throw new Error(`Could not validate the browser sign-in (${res.status}).`);
745
+ const data = await res.json();
746
+ return { uid: data.user_id };
747
+ }
748
+ async function browserHandoffLogin(cfg) {
749
+ const nonce = randomBytes(5).toString("hex").toUpperCase();
750
+ const TIMEOUT_MS = 5 * 6e4;
751
+ const payload = await new Promise((resolve3, reject) => {
752
+ let timer;
753
+ function finish(act) {
754
+ if (timer) clearTimeout(timer);
755
+ server.close();
756
+ act();
757
+ }
758
+ const server = http.createServer((req, res) => {
759
+ if (req.method === "OPTIONS") {
760
+ res.writeHead(204, CORS_HEADERS);
761
+ res.end();
762
+ return;
763
+ }
764
+ if (req.method !== "POST") {
765
+ res.writeHead(200, { "Content-Type": "text/html" });
766
+ res.end(
767
+ `<html><body style="font-family:system-ui;padding:3rem;text-align:center"><h2>Tokelytics</h2><p>Waiting for the browser to finish signing in\u2026</p></body></html>`
768
+ );
769
+ return;
770
+ }
771
+ void readBody(req).then((raw) => {
772
+ let body;
773
+ try {
774
+ body = JSON.parse(raw);
775
+ } catch {
776
+ }
777
+ if (!body || body.nonce !== nonce || !body.refreshToken) {
778
+ res.writeHead(403, { ...CORS_HEADERS, "Content-Type": "application/json" });
779
+ res.end(JSON.stringify({ ok: false, error: "verification code mismatch" }));
780
+ return;
781
+ }
782
+ res.writeHead(200, { ...CORS_HEADERS, "Content-Type": "application/json" });
783
+ res.end(JSON.stringify({ ok: true }));
784
+ finish(() => resolve3(body));
785
+ }).catch((err) => finish(() => reject(err)));
786
+ });
787
+ timer = setTimeout(
788
+ () => finish(() => reject(new Error('Timed out waiting for browser sign-in. Run "tokelytics login" again.'))),
789
+ TIMEOUT_MS
790
+ );
791
+ server.on("error", (err) => finish(() => reject(err)));
792
+ server.listen(0, "127.0.0.1", () => {
793
+ const { port } = server.address();
794
+ const url = `${cfg.webBaseUrl}/connect#${port}-${nonce}`;
795
+ console.log(`
796
+ To connect this machine, approve the sign-in in your browser:
797
+
798
+ ${url}
799
+
800
+ Verification code: ${nonce}
801
+ `);
802
+ console.log(
803
+ openBrowser(url) ? "Opening your browser\u2026 if nothing opens, click the link above.\n" : "Open the link above in your browser to continue.\n"
804
+ );
805
+ });
806
+ });
807
+ const provider = payload.provider === "github" ? "github" : "google";
808
+ const { uid } = await exchangeRefreshToken(cfg, payload.refreshToken);
809
+ return { provider, uid, email: payload.email, refreshToken: payload.refreshToken };
810
+ }
811
+ function sessionFromRefresh(cfg, creds) {
812
+ let cached;
813
+ return {
814
+ uid: creds.uid,
815
+ email: creds.email,
816
+ async getToken() {
817
+ if (cached && Date.now() < cached.expiresAt - 6e4) return cached.token;
818
+ const res = await fetch(`${secureTokenBase(cfg)}/v1/token?key=${cfg.firebase.apiKey}`, {
819
+ method: "POST",
820
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
821
+ body: new URLSearchParams({ grant_type: "refresh_token", refresh_token: creds.refreshToken })
822
+ });
823
+ if (!res.ok) throw new Error(`Token refresh failed (${res.status}). Run "tokelytics login" again.`);
824
+ const data = await res.json();
825
+ cached = { token: data.id_token, expiresAt: Date.now() + Number(data.expires_in) * 1e3 };
826
+ return cached.token;
827
+ }
828
+ };
829
+ }
830
+ async function interactiveLogin(cfg, method = "handoff") {
831
+ const creds = method === "google" ? await googleLogin(cfg) : method === "github" ? await githubLogin(cfg) : await browserHandoffLogin(cfg);
832
+ await saveCredentials(creds);
833
+ console.log(`Signed in as ${creds.email ?? creds.uid} (${creds.provider}).`);
834
+ return sessionFromRefresh(cfg, creds);
835
+ }
836
+ async function restoreSession(cfg) {
837
+ const creds = await loadCredentials();
838
+ if (!creds) return null;
839
+ return sessionFromRefresh(cfg, creds);
840
+ }
841
+
842
+ // src/runner.ts
843
+ import * as os4 from "node:os";
844
+
845
+ // src/connectors.ts
846
+ function buildConnectors() {
847
+ return [new ClaudeConnector(), new CodexConnector()];
848
+ }
849
+
850
+ // src/firestore-rest.ts
851
+ function encodeValue(v) {
852
+ if (v === null || v === void 0) return { nullValue: null };
853
+ if (typeof v === "string") return { stringValue: v };
854
+ if (typeof v === "boolean") return { booleanValue: v };
855
+ if (typeof v === "number") {
856
+ return Number.isInteger(v) ? { integerValue: String(v) } : { doubleValue: v };
857
+ }
858
+ if (Array.isArray(v)) return { arrayValue: { values: v.map(encodeValue) } };
859
+ if (typeof v === "object") return { mapValue: { fields: encodeFields(v) } };
860
+ return { stringValue: String(v) };
861
+ }
862
+ function encodeFields(obj) {
863
+ const out = {};
864
+ for (const [k, val] of Object.entries(obj)) {
865
+ if (val === void 0) continue;
866
+ out[k] = encodeValue(val);
867
+ }
868
+ return out;
869
+ }
870
+ function decodeValue(v) {
871
+ if ("stringValue" in v) return v.stringValue;
872
+ if ("integerValue" in v) return Number(v.integerValue);
873
+ if ("doubleValue" in v) return v.doubleValue;
874
+ if ("booleanValue" in v) return v.booleanValue;
875
+ if ("nullValue" in v) return null;
876
+ if ("arrayValue" in v) return (v.arrayValue.values ?? []).map(decodeValue);
877
+ if ("mapValue" in v) return decodeFields(v.mapValue.fields ?? {});
878
+ return null;
879
+ }
880
+ function decodeFields(fields) {
881
+ const out = {};
882
+ for (const [k, val] of Object.entries(fields)) out[k] = decodeValue(val);
883
+ return out;
884
+ }
885
+ var FirestoreRest = class {
886
+ projectId;
887
+ getToken;
888
+ origin;
889
+ constructor(opts) {
890
+ this.projectId = opts.projectId;
891
+ this.getToken = opts.getToken;
892
+ this.origin = opts.emulatorHost ? `http://${opts.emulatorHost}` : "https://firestore.googleapis.com";
893
+ }
894
+ docsRoot() {
895
+ return `projects/${this.projectId}/databases/(default)/documents`;
896
+ }
897
+ url(suffix) {
898
+ return `${this.origin}/v1/${suffix}`;
899
+ }
900
+ async headers() {
901
+ return { "Content-Type": "application/json", Authorization: `Bearer ${await this.getToken()}` };
902
+ }
903
+ /** Build a full document resource name from path segments (each URL-encoded). */
904
+ docName(...segments) {
905
+ return `${this.docsRoot()}/${segments.map((s) => encodeURIComponent(s)).join("/")}`;
906
+ }
907
+ /** Commit a batch of full-document upserts (overwrite semantics). */
908
+ async upsert(docs) {
909
+ if (docs.length === 0) return;
910
+ const writes = docs.map((d) => ({ update: { name: d.name, fields: encodeFields(d.fields) } }));
911
+ const res = await fetch(this.url(`${this.docsRoot()}:commit`), {
912
+ method: "POST",
913
+ headers: await this.headers(),
914
+ body: JSON.stringify({ writes })
915
+ });
916
+ if (!res.ok) throw new Error(`Firestore commit failed (${res.status}): ${await res.text()}`);
917
+ }
918
+ /** Run an equality query under a parent path, returning decoded documents. */
919
+ async queryEqual(parentSegments, collectionId, filters) {
920
+ const parent = parentSegments.length ? this.docName(...parentSegments) : this.docsRoot();
921
+ const where = filters.length === 1 ? { fieldFilter: { field: { fieldPath: filters[0].field }, op: filters[0].op, value: encodeValue(filters[0].value) } } : {
922
+ compositeFilter: {
923
+ op: "AND",
924
+ filters: filters.map((f) => ({
925
+ fieldFilter: { field: { fieldPath: f.field }, op: f.op, value: encodeValue(f.value) }
926
+ }))
927
+ }
928
+ };
929
+ const res = await fetch(this.url(`${parent}:runQuery`), {
930
+ method: "POST",
931
+ headers: await this.headers(),
932
+ body: JSON.stringify({ structuredQuery: { from: [{ collectionId }], where } })
933
+ });
934
+ if (!res.ok) throw new Error(`Firestore query failed (${res.status}): ${await res.text()}`);
935
+ const rows = await res.json();
936
+ return rows.filter((r) => r.document?.fields).map((r) => decodeFields(r.document.fields));
937
+ }
938
+ };
939
+
940
+ // src/firestore-sink.ts
941
+ var BATCH = 400;
942
+ var FirestoreSink = class {
943
+ constructor(fs4, uid) {
944
+ this.fs = fs4;
945
+ this.uid = uid;
946
+ }
947
+ async writeTurns(turns) {
948
+ for (let i = 0; i < turns.length; i += BATCH) {
949
+ const slice = turns.slice(i, i + BATCH);
950
+ await this.fs.upsert(
951
+ slice.map((t) => ({
952
+ name: this.fs.docName("users", this.uid, "turns", t.turnId),
953
+ fields: { ...t, day: dayOf(t.ts) }
954
+ }))
955
+ );
956
+ }
957
+ }
958
+ async turnsForSession(sessionId) {
959
+ const rows = await this.fs.queryEqual(["users", this.uid], "turns", [
960
+ { field: "sessionId", op: "EQUAL", value: sessionId }
961
+ ]);
962
+ return rows;
963
+ }
964
+ async turnsForBucket(provider, day) {
965
+ const rows = await this.fs.queryEqual(["users", this.uid], "turns", [
966
+ { field: "provider", op: "EQUAL", value: provider },
967
+ { field: "day", op: "EQUAL", value: day }
968
+ ]);
969
+ return rows;
970
+ }
971
+ async recomputeSessions(sessionIds) {
972
+ for (const sid of sessionIds) {
973
+ const turns = await this.turnsForSession(sid);
974
+ if (!turns.length) continue;
975
+ const agg = aggregateSession(sid, turns);
976
+ await this.fs.upsert([{ name: this.fs.docName("users", this.uid, "sessions", sid), fields: { ...agg } }]);
977
+ }
978
+ }
979
+ async recomputeRollups(buckets) {
980
+ for (const { provider, day } of buckets) {
981
+ const turns = await this.turnsForBucket(provider, day);
982
+ if (!turns.length) continue;
983
+ const rollup = buildRollup(provider, day, turns);
984
+ await this.fs.upsert([
985
+ { name: this.fs.docName("users", this.uid, "rollups", rollupId(provider, day)), fields: { ...rollup } }
986
+ ]);
987
+ }
988
+ }
989
+ async touchDevice(meta) {
990
+ await this.fs.upsert([
991
+ { name: this.fs.docName("users", this.uid, "devices", meta.deviceId), fields: { ...meta } }
992
+ ]);
993
+ }
994
+ };
995
+
996
+ // src/sync.ts
997
+ async function runSync(connectors, sink, state, device) {
998
+ let st = state;
999
+ const collected = [];
1000
+ const byProvider = {};
1001
+ for (const c of connectors) {
1002
+ const { turns: turns2, state: next } = await c.collect(st);
1003
+ st = next;
1004
+ for (const t of turns2) {
1005
+ collected.push(t);
1006
+ byProvider[t.provider] = (byProvider[t.provider] ?? 0) + 1;
1007
+ }
1008
+ }
1009
+ const unique = /* @__PURE__ */ new Map();
1010
+ for (const t of collected) unique.set(t.turnId, t);
1011
+ const turns = [...unique.values()];
1012
+ if (turns.length > 0) {
1013
+ await sink.writeTurns(turns);
1014
+ const { sessionIds, dayBuckets } = affectedKeys(turns);
1015
+ await sink.recomputeSessions(sessionIds);
1016
+ await sink.recomputeRollups(dayBuckets);
1017
+ }
1018
+ await sink.touchDevice({ ...device, lastSyncAt: (/* @__PURE__ */ new Date()).toISOString() });
1019
+ return { newTurns: turns.length, byProvider, state: st };
1020
+ }
1021
+
1022
+ // src/runner.ts
1023
+ async function createRunner() {
1024
+ const cfg = await loadConfig();
1025
+ const session = await restoreSession(cfg);
1026
+ if (!session) throw new Error('Not signed in. Run "tokelytics login" first.');
1027
+ const connectors = buildConnectors();
1028
+ const rest = new FirestoreRest({
1029
+ projectId: cfg.firebase.projectId,
1030
+ getToken: () => session.getToken(),
1031
+ emulatorHost: cfg.firestoreEmulatorHost
1032
+ });
1033
+ const sink = new FirestoreSink(rest, session.uid);
1034
+ return {
1035
+ connectors,
1036
+ watchPaths() {
1037
+ return connectors.flatMap((c) => c.watchPaths());
1038
+ },
1039
+ async syncOnce() {
1040
+ const state = await loadState();
1041
+ const device = {
1042
+ deviceId: state.deviceId,
1043
+ name: os4.hostname(),
1044
+ lastSyncAt: "",
1045
+ providers: connectors.map((c) => c.id)
1046
+ };
1047
+ const res = await runSync(connectors, sink, state.scan, device);
1048
+ await saveState({ deviceId: state.deviceId, scan: res.state });
1049
+ return { newTurns: res.newTurns, byProvider: res.byProvider };
1050
+ }
1051
+ };
1052
+ }
1053
+ async function registerDevice(cfg, session) {
1054
+ const rest = new FirestoreRest({
1055
+ projectId: cfg.firebase.projectId,
1056
+ getToken: () => session.getToken(),
1057
+ emulatorHost: cfg.firestoreEmulatorHost
1058
+ });
1059
+ const sink = new FirestoreSink(rest, session.uid);
1060
+ const state = await loadState();
1061
+ const connectors = buildConnectors();
1062
+ await sink.touchDevice({
1063
+ deviceId: state.deviceId,
1064
+ name: os4.hostname(),
1065
+ lastSyncAt: "",
1066
+ providers: connectors.map((c) => c.id)
1067
+ });
1068
+ }
1069
+
1070
+ // ../node_modules/chokidar/esm/index.js
1071
+ import { stat as statcb } from "fs";
1072
+ import { stat as stat3, readdir as readdir2 } from "fs/promises";
1073
+ import { EventEmitter } from "events";
1074
+ import * as sysPath2 from "path";
1075
+
1076
+ // ../node_modules/readdirp/esm/index.js
1077
+ import { stat, lstat, readdir, realpath } from "node:fs/promises";
1078
+ import { Readable } from "node:stream";
1079
+ import { resolve as presolve, relative as prelative, join as pjoin, sep as psep } from "node:path";
1080
+ var EntryTypes = {
1081
+ FILE_TYPE: "files",
1082
+ DIR_TYPE: "directories",
1083
+ FILE_DIR_TYPE: "files_directories",
1084
+ EVERYTHING_TYPE: "all"
1085
+ };
1086
+ var defaultOptions = {
1087
+ root: ".",
1088
+ fileFilter: (_entryInfo) => true,
1089
+ directoryFilter: (_entryInfo) => true,
1090
+ type: EntryTypes.FILE_TYPE,
1091
+ lstat: false,
1092
+ depth: 2147483648,
1093
+ alwaysStat: false,
1094
+ highWaterMark: 4096
1095
+ };
1096
+ Object.freeze(defaultOptions);
1097
+ var RECURSIVE_ERROR_CODE = "READDIRP_RECURSIVE_ERROR";
1098
+ var NORMAL_FLOW_ERRORS = /* @__PURE__ */ new Set(["ENOENT", "EPERM", "EACCES", "ELOOP", RECURSIVE_ERROR_CODE]);
1099
+ var ALL_TYPES = [
1100
+ EntryTypes.DIR_TYPE,
1101
+ EntryTypes.EVERYTHING_TYPE,
1102
+ EntryTypes.FILE_DIR_TYPE,
1103
+ EntryTypes.FILE_TYPE
1104
+ ];
1105
+ var DIR_TYPES = /* @__PURE__ */ new Set([
1106
+ EntryTypes.DIR_TYPE,
1107
+ EntryTypes.EVERYTHING_TYPE,
1108
+ EntryTypes.FILE_DIR_TYPE
1109
+ ]);
1110
+ var FILE_TYPES = /* @__PURE__ */ new Set([
1111
+ EntryTypes.EVERYTHING_TYPE,
1112
+ EntryTypes.FILE_DIR_TYPE,
1113
+ EntryTypes.FILE_TYPE
1114
+ ]);
1115
+ var isNormalFlowError = (error) => NORMAL_FLOW_ERRORS.has(error.code);
1116
+ var wantBigintFsStats = process.platform === "win32";
1117
+ var emptyFn = (_entryInfo) => true;
1118
+ var normalizeFilter = (filter) => {
1119
+ if (filter === void 0)
1120
+ return emptyFn;
1121
+ if (typeof filter === "function")
1122
+ return filter;
1123
+ if (typeof filter === "string") {
1124
+ const fl = filter.trim();
1125
+ return (entry) => entry.basename === fl;
1126
+ }
1127
+ if (Array.isArray(filter)) {
1128
+ const trItems = filter.map((item) => item.trim());
1129
+ return (entry) => trItems.some((f) => entry.basename === f);
1130
+ }
1131
+ return emptyFn;
1132
+ };
1133
+ var ReaddirpStream = class extends Readable {
1134
+ constructor(options = {}) {
1135
+ super({
1136
+ objectMode: true,
1137
+ autoDestroy: true,
1138
+ highWaterMark: options.highWaterMark
1139
+ });
1140
+ const opts = { ...defaultOptions, ...options };
1141
+ const { root, type } = opts;
1142
+ this._fileFilter = normalizeFilter(opts.fileFilter);
1143
+ this._directoryFilter = normalizeFilter(opts.directoryFilter);
1144
+ const statMethod = opts.lstat ? lstat : stat;
1145
+ if (wantBigintFsStats) {
1146
+ this._stat = (path6) => statMethod(path6, { bigint: true });
1147
+ } else {
1148
+ this._stat = statMethod;
1149
+ }
1150
+ this._maxDepth = opts.depth ?? defaultOptions.depth;
1151
+ this._wantsDir = type ? DIR_TYPES.has(type) : false;
1152
+ this._wantsFile = type ? FILE_TYPES.has(type) : false;
1153
+ this._wantsEverything = type === EntryTypes.EVERYTHING_TYPE;
1154
+ this._root = presolve(root);
1155
+ this._isDirent = !opts.alwaysStat;
1156
+ this._statsProp = this._isDirent ? "dirent" : "stats";
1157
+ this._rdOptions = { encoding: "utf8", withFileTypes: this._isDirent };
1158
+ this.parents = [this._exploreDir(root, 1)];
1159
+ this.reading = false;
1160
+ this.parent = void 0;
1161
+ }
1162
+ async _read(batch) {
1163
+ if (this.reading)
1164
+ return;
1165
+ this.reading = true;
1166
+ try {
1167
+ while (!this.destroyed && batch > 0) {
1168
+ const par = this.parent;
1169
+ const fil = par && par.files;
1170
+ if (fil && fil.length > 0) {
1171
+ const { path: path6, depth } = par;
1172
+ const slice = fil.splice(0, batch).map((dirent) => this._formatEntry(dirent, path6));
1173
+ const awaited = await Promise.all(slice);
1174
+ for (const entry of awaited) {
1175
+ if (!entry)
1176
+ continue;
1177
+ if (this.destroyed)
1178
+ return;
1179
+ const entryType = await this._getEntryType(entry);
1180
+ if (entryType === "directory" && this._directoryFilter(entry)) {
1181
+ if (depth <= this._maxDepth) {
1182
+ this.parents.push(this._exploreDir(entry.fullPath, depth + 1));
1183
+ }
1184
+ if (this._wantsDir) {
1185
+ this.push(entry);
1186
+ batch--;
1187
+ }
1188
+ } else if ((entryType === "file" || this._includeAsFile(entry)) && this._fileFilter(entry)) {
1189
+ if (this._wantsFile) {
1190
+ this.push(entry);
1191
+ batch--;
1192
+ }
1193
+ }
1194
+ }
1195
+ } else {
1196
+ const parent = this.parents.pop();
1197
+ if (!parent) {
1198
+ this.push(null);
1199
+ break;
1200
+ }
1201
+ this.parent = await parent;
1202
+ if (this.destroyed)
1203
+ return;
1204
+ }
1205
+ }
1206
+ } catch (error) {
1207
+ this.destroy(error);
1208
+ } finally {
1209
+ this.reading = false;
1210
+ }
1211
+ }
1212
+ async _exploreDir(path6, depth) {
1213
+ let files;
1214
+ try {
1215
+ files = await readdir(path6, this._rdOptions);
1216
+ } catch (error) {
1217
+ this._onError(error);
1218
+ }
1219
+ return { files, depth, path: path6 };
1220
+ }
1221
+ async _formatEntry(dirent, path6) {
1222
+ let entry;
1223
+ const basename4 = this._isDirent ? dirent.name : dirent;
1224
+ try {
1225
+ const fullPath = presolve(pjoin(path6, basename4));
1226
+ entry = { path: prelative(this._root, fullPath), fullPath, basename: basename4 };
1227
+ entry[this._statsProp] = this._isDirent ? dirent : await this._stat(fullPath);
1228
+ } catch (err) {
1229
+ this._onError(err);
1230
+ return;
1231
+ }
1232
+ return entry;
1233
+ }
1234
+ _onError(err) {
1235
+ if (isNormalFlowError(err) && !this.destroyed) {
1236
+ this.emit("warn", err);
1237
+ } else {
1238
+ this.destroy(err);
1239
+ }
1240
+ }
1241
+ async _getEntryType(entry) {
1242
+ if (!entry && this._statsProp in entry) {
1243
+ return "";
1244
+ }
1245
+ const stats = entry[this._statsProp];
1246
+ if (stats.isFile())
1247
+ return "file";
1248
+ if (stats.isDirectory())
1249
+ return "directory";
1250
+ if (stats && stats.isSymbolicLink()) {
1251
+ const full = entry.fullPath;
1252
+ try {
1253
+ const entryRealPath = await realpath(full);
1254
+ const entryRealPathStats = await lstat(entryRealPath);
1255
+ if (entryRealPathStats.isFile()) {
1256
+ return "file";
1257
+ }
1258
+ if (entryRealPathStats.isDirectory()) {
1259
+ const len = entryRealPath.length;
1260
+ if (full.startsWith(entryRealPath) && full.substr(len, 1) === psep) {
1261
+ const recursiveError = new Error(`Circular symlink detected: "${full}" points to "${entryRealPath}"`);
1262
+ recursiveError.code = RECURSIVE_ERROR_CODE;
1263
+ return this._onError(recursiveError);
1264
+ }
1265
+ return "directory";
1266
+ }
1267
+ } catch (error) {
1268
+ this._onError(error);
1269
+ return "";
1270
+ }
1271
+ }
1272
+ }
1273
+ _includeAsFile(entry) {
1274
+ const stats = entry && entry[this._statsProp];
1275
+ return stats && this._wantsEverything && !stats.isDirectory();
1276
+ }
1277
+ };
1278
+ function readdirp(root, options = {}) {
1279
+ let type = options.entryType || options.type;
1280
+ if (type === "both")
1281
+ type = EntryTypes.FILE_DIR_TYPE;
1282
+ if (type)
1283
+ options.type = type;
1284
+ if (!root) {
1285
+ throw new Error("readdirp: root argument is required. Usage: readdirp(root, options)");
1286
+ } else if (typeof root !== "string") {
1287
+ throw new TypeError("readdirp: root argument must be a string. Usage: readdirp(root, options)");
1288
+ } else if (type && !ALL_TYPES.includes(type)) {
1289
+ throw new Error(`readdirp: Invalid type passed. Use one of ${ALL_TYPES.join(", ")}`);
1290
+ }
1291
+ options.root = root;
1292
+ return new ReaddirpStream(options);
1293
+ }
1294
+
1295
+ // ../node_modules/chokidar/esm/handler.js
1296
+ import { watchFile, unwatchFile, watch as fs_watch } from "fs";
1297
+ import { open, stat as stat2, lstat as lstat2, realpath as fsrealpath } from "fs/promises";
1298
+ import * as sysPath from "path";
1299
+ import { type as osType } from "os";
1300
+ var STR_DATA = "data";
1301
+ var STR_END = "end";
1302
+ var STR_CLOSE = "close";
1303
+ var EMPTY_FN = () => {
1304
+ };
1305
+ var pl = process.platform;
1306
+ var isWindows = pl === "win32";
1307
+ var isMacos = pl === "darwin";
1308
+ var isLinux = pl === "linux";
1309
+ var isFreeBSD = pl === "freebsd";
1310
+ var isIBMi = osType() === "OS400";
1311
+ var EVENTS = {
1312
+ ALL: "all",
1313
+ READY: "ready",
1314
+ ADD: "add",
1315
+ CHANGE: "change",
1316
+ ADD_DIR: "addDir",
1317
+ UNLINK: "unlink",
1318
+ UNLINK_DIR: "unlinkDir",
1319
+ RAW: "raw",
1320
+ ERROR: "error"
1321
+ };
1322
+ var EV = EVENTS;
1323
+ var THROTTLE_MODE_WATCH = "watch";
1324
+ var statMethods = { lstat: lstat2, stat: stat2 };
1325
+ var KEY_LISTENERS = "listeners";
1326
+ var KEY_ERR = "errHandlers";
1327
+ var KEY_RAW = "rawEmitters";
1328
+ var HANDLER_KEYS = [KEY_LISTENERS, KEY_ERR, KEY_RAW];
1329
+ var binaryExtensions = /* @__PURE__ */ new Set([
1330
+ "3dm",
1331
+ "3ds",
1332
+ "3g2",
1333
+ "3gp",
1334
+ "7z",
1335
+ "a",
1336
+ "aac",
1337
+ "adp",
1338
+ "afdesign",
1339
+ "afphoto",
1340
+ "afpub",
1341
+ "ai",
1342
+ "aif",
1343
+ "aiff",
1344
+ "alz",
1345
+ "ape",
1346
+ "apk",
1347
+ "appimage",
1348
+ "ar",
1349
+ "arj",
1350
+ "asf",
1351
+ "au",
1352
+ "avi",
1353
+ "bak",
1354
+ "baml",
1355
+ "bh",
1356
+ "bin",
1357
+ "bk",
1358
+ "bmp",
1359
+ "btif",
1360
+ "bz2",
1361
+ "bzip2",
1362
+ "cab",
1363
+ "caf",
1364
+ "cgm",
1365
+ "class",
1366
+ "cmx",
1367
+ "cpio",
1368
+ "cr2",
1369
+ "cur",
1370
+ "dat",
1371
+ "dcm",
1372
+ "deb",
1373
+ "dex",
1374
+ "djvu",
1375
+ "dll",
1376
+ "dmg",
1377
+ "dng",
1378
+ "doc",
1379
+ "docm",
1380
+ "docx",
1381
+ "dot",
1382
+ "dotm",
1383
+ "dra",
1384
+ "DS_Store",
1385
+ "dsk",
1386
+ "dts",
1387
+ "dtshd",
1388
+ "dvb",
1389
+ "dwg",
1390
+ "dxf",
1391
+ "ecelp4800",
1392
+ "ecelp7470",
1393
+ "ecelp9600",
1394
+ "egg",
1395
+ "eol",
1396
+ "eot",
1397
+ "epub",
1398
+ "exe",
1399
+ "f4v",
1400
+ "fbs",
1401
+ "fh",
1402
+ "fla",
1403
+ "flac",
1404
+ "flatpak",
1405
+ "fli",
1406
+ "flv",
1407
+ "fpx",
1408
+ "fst",
1409
+ "fvt",
1410
+ "g3",
1411
+ "gh",
1412
+ "gif",
1413
+ "graffle",
1414
+ "gz",
1415
+ "gzip",
1416
+ "h261",
1417
+ "h263",
1418
+ "h264",
1419
+ "icns",
1420
+ "ico",
1421
+ "ief",
1422
+ "img",
1423
+ "ipa",
1424
+ "iso",
1425
+ "jar",
1426
+ "jpeg",
1427
+ "jpg",
1428
+ "jpgv",
1429
+ "jpm",
1430
+ "jxr",
1431
+ "key",
1432
+ "ktx",
1433
+ "lha",
1434
+ "lib",
1435
+ "lvp",
1436
+ "lz",
1437
+ "lzh",
1438
+ "lzma",
1439
+ "lzo",
1440
+ "m3u",
1441
+ "m4a",
1442
+ "m4v",
1443
+ "mar",
1444
+ "mdi",
1445
+ "mht",
1446
+ "mid",
1447
+ "midi",
1448
+ "mj2",
1449
+ "mka",
1450
+ "mkv",
1451
+ "mmr",
1452
+ "mng",
1453
+ "mobi",
1454
+ "mov",
1455
+ "movie",
1456
+ "mp3",
1457
+ "mp4",
1458
+ "mp4a",
1459
+ "mpeg",
1460
+ "mpg",
1461
+ "mpga",
1462
+ "mxu",
1463
+ "nef",
1464
+ "npx",
1465
+ "numbers",
1466
+ "nupkg",
1467
+ "o",
1468
+ "odp",
1469
+ "ods",
1470
+ "odt",
1471
+ "oga",
1472
+ "ogg",
1473
+ "ogv",
1474
+ "otf",
1475
+ "ott",
1476
+ "pages",
1477
+ "pbm",
1478
+ "pcx",
1479
+ "pdb",
1480
+ "pdf",
1481
+ "pea",
1482
+ "pgm",
1483
+ "pic",
1484
+ "png",
1485
+ "pnm",
1486
+ "pot",
1487
+ "potm",
1488
+ "potx",
1489
+ "ppa",
1490
+ "ppam",
1491
+ "ppm",
1492
+ "pps",
1493
+ "ppsm",
1494
+ "ppsx",
1495
+ "ppt",
1496
+ "pptm",
1497
+ "pptx",
1498
+ "psd",
1499
+ "pya",
1500
+ "pyc",
1501
+ "pyo",
1502
+ "pyv",
1503
+ "qt",
1504
+ "rar",
1505
+ "ras",
1506
+ "raw",
1507
+ "resources",
1508
+ "rgb",
1509
+ "rip",
1510
+ "rlc",
1511
+ "rmf",
1512
+ "rmvb",
1513
+ "rpm",
1514
+ "rtf",
1515
+ "rz",
1516
+ "s3m",
1517
+ "s7z",
1518
+ "scpt",
1519
+ "sgi",
1520
+ "shar",
1521
+ "snap",
1522
+ "sil",
1523
+ "sketch",
1524
+ "slk",
1525
+ "smv",
1526
+ "snk",
1527
+ "so",
1528
+ "stl",
1529
+ "suo",
1530
+ "sub",
1531
+ "swf",
1532
+ "tar",
1533
+ "tbz",
1534
+ "tbz2",
1535
+ "tga",
1536
+ "tgz",
1537
+ "thmx",
1538
+ "tif",
1539
+ "tiff",
1540
+ "tlz",
1541
+ "ttc",
1542
+ "ttf",
1543
+ "txz",
1544
+ "udf",
1545
+ "uvh",
1546
+ "uvi",
1547
+ "uvm",
1548
+ "uvp",
1549
+ "uvs",
1550
+ "uvu",
1551
+ "viv",
1552
+ "vob",
1553
+ "war",
1554
+ "wav",
1555
+ "wax",
1556
+ "wbmp",
1557
+ "wdp",
1558
+ "weba",
1559
+ "webm",
1560
+ "webp",
1561
+ "whl",
1562
+ "wim",
1563
+ "wm",
1564
+ "wma",
1565
+ "wmv",
1566
+ "wmx",
1567
+ "woff",
1568
+ "woff2",
1569
+ "wrm",
1570
+ "wvx",
1571
+ "xbm",
1572
+ "xif",
1573
+ "xla",
1574
+ "xlam",
1575
+ "xls",
1576
+ "xlsb",
1577
+ "xlsm",
1578
+ "xlsx",
1579
+ "xlt",
1580
+ "xltm",
1581
+ "xltx",
1582
+ "xm",
1583
+ "xmind",
1584
+ "xpi",
1585
+ "xpm",
1586
+ "xwd",
1587
+ "xz",
1588
+ "z",
1589
+ "zip",
1590
+ "zipx"
1591
+ ]);
1592
+ var isBinaryPath = (filePath) => binaryExtensions.has(sysPath.extname(filePath).slice(1).toLowerCase());
1593
+ var foreach = (val, fn) => {
1594
+ if (val instanceof Set) {
1595
+ val.forEach(fn);
1596
+ } else {
1597
+ fn(val);
1598
+ }
1599
+ };
1600
+ var addAndConvert = (main2, prop, item) => {
1601
+ let container = main2[prop];
1602
+ if (!(container instanceof Set)) {
1603
+ main2[prop] = container = /* @__PURE__ */ new Set([container]);
1604
+ }
1605
+ container.add(item);
1606
+ };
1607
+ var clearItem = (cont) => (key) => {
1608
+ const set = cont[key];
1609
+ if (set instanceof Set) {
1610
+ set.clear();
1611
+ } else {
1612
+ delete cont[key];
1613
+ }
1614
+ };
1615
+ var delFromSet = (main2, prop, item) => {
1616
+ const container = main2[prop];
1617
+ if (container instanceof Set) {
1618
+ container.delete(item);
1619
+ } else if (container === item) {
1620
+ delete main2[prop];
1621
+ }
1622
+ };
1623
+ var isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
1624
+ var FsWatchInstances = /* @__PURE__ */ new Map();
1625
+ function createFsWatchInstance(path6, options, listener, errHandler, emitRaw) {
1626
+ const handleEvent = (rawEvent, evPath) => {
1627
+ listener(path6);
1628
+ emitRaw(rawEvent, evPath, { watchedPath: path6 });
1629
+ if (evPath && path6 !== evPath) {
1630
+ fsWatchBroadcast(sysPath.resolve(path6, evPath), KEY_LISTENERS, sysPath.join(path6, evPath));
1631
+ }
1632
+ };
1633
+ try {
1634
+ return fs_watch(path6, {
1635
+ persistent: options.persistent
1636
+ }, handleEvent);
1637
+ } catch (error) {
1638
+ errHandler(error);
1639
+ return void 0;
1640
+ }
1641
+ }
1642
+ var fsWatchBroadcast = (fullPath, listenerType, val1, val2, val3) => {
1643
+ const cont = FsWatchInstances.get(fullPath);
1644
+ if (!cont)
1645
+ return;
1646
+ foreach(cont[listenerType], (listener) => {
1647
+ listener(val1, val2, val3);
1648
+ });
1649
+ };
1650
+ var setFsWatchListener = (path6, fullPath, options, handlers) => {
1651
+ const { listener, errHandler, rawEmitter } = handlers;
1652
+ let cont = FsWatchInstances.get(fullPath);
1653
+ let watcher;
1654
+ if (!options.persistent) {
1655
+ watcher = createFsWatchInstance(path6, options, listener, errHandler, rawEmitter);
1656
+ if (!watcher)
1657
+ return;
1658
+ return watcher.close.bind(watcher);
1659
+ }
1660
+ if (cont) {
1661
+ addAndConvert(cont, KEY_LISTENERS, listener);
1662
+ addAndConvert(cont, KEY_ERR, errHandler);
1663
+ addAndConvert(cont, KEY_RAW, rawEmitter);
1664
+ } else {
1665
+ watcher = createFsWatchInstance(
1666
+ path6,
1667
+ options,
1668
+ fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
1669
+ errHandler,
1670
+ // no need to use broadcast here
1671
+ fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
1672
+ );
1673
+ if (!watcher)
1674
+ return;
1675
+ watcher.on(EV.ERROR, async (error) => {
1676
+ const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
1677
+ if (cont)
1678
+ cont.watcherUnusable = true;
1679
+ if (isWindows && error.code === "EPERM") {
1680
+ try {
1681
+ const fd = await open(path6, "r");
1682
+ await fd.close();
1683
+ broadcastErr(error);
1684
+ } catch (err) {
1685
+ }
1686
+ } else {
1687
+ broadcastErr(error);
1688
+ }
1689
+ });
1690
+ cont = {
1691
+ listeners: listener,
1692
+ errHandlers: errHandler,
1693
+ rawEmitters: rawEmitter,
1694
+ watcher
1695
+ };
1696
+ FsWatchInstances.set(fullPath, cont);
1697
+ }
1698
+ return () => {
1699
+ delFromSet(cont, KEY_LISTENERS, listener);
1700
+ delFromSet(cont, KEY_ERR, errHandler);
1701
+ delFromSet(cont, KEY_RAW, rawEmitter);
1702
+ if (isEmptySet(cont.listeners)) {
1703
+ cont.watcher.close();
1704
+ FsWatchInstances.delete(fullPath);
1705
+ HANDLER_KEYS.forEach(clearItem(cont));
1706
+ cont.watcher = void 0;
1707
+ Object.freeze(cont);
1708
+ }
1709
+ };
1710
+ };
1711
+ var FsWatchFileInstances = /* @__PURE__ */ new Map();
1712
+ var setFsWatchFileListener = (path6, fullPath, options, handlers) => {
1713
+ const { listener, rawEmitter } = handlers;
1714
+ let cont = FsWatchFileInstances.get(fullPath);
1715
+ const copts = cont && cont.options;
1716
+ if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
1717
+ unwatchFile(fullPath);
1718
+ cont = void 0;
1719
+ }
1720
+ if (cont) {
1721
+ addAndConvert(cont, KEY_LISTENERS, listener);
1722
+ addAndConvert(cont, KEY_RAW, rawEmitter);
1723
+ } else {
1724
+ cont = {
1725
+ listeners: listener,
1726
+ rawEmitters: rawEmitter,
1727
+ options,
1728
+ watcher: watchFile(fullPath, options, (curr, prev) => {
1729
+ foreach(cont.rawEmitters, (rawEmitter2) => {
1730
+ rawEmitter2(EV.CHANGE, fullPath, { curr, prev });
1731
+ });
1732
+ const currmtime = curr.mtimeMs;
1733
+ if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
1734
+ foreach(cont.listeners, (listener2) => listener2(path6, curr));
1735
+ }
1736
+ })
1737
+ };
1738
+ FsWatchFileInstances.set(fullPath, cont);
1739
+ }
1740
+ return () => {
1741
+ delFromSet(cont, KEY_LISTENERS, listener);
1742
+ delFromSet(cont, KEY_RAW, rawEmitter);
1743
+ if (isEmptySet(cont.listeners)) {
1744
+ FsWatchFileInstances.delete(fullPath);
1745
+ unwatchFile(fullPath);
1746
+ cont.options = cont.watcher = void 0;
1747
+ Object.freeze(cont);
1748
+ }
1749
+ };
1750
+ };
1751
+ var NodeFsHandler = class {
1752
+ constructor(fsW) {
1753
+ this.fsw = fsW;
1754
+ this._boundHandleError = (error) => fsW._handleError(error);
1755
+ }
1756
+ /**
1757
+ * Watch file for changes with fs_watchFile or fs_watch.
1758
+ * @param path to file or dir
1759
+ * @param listener on fs change
1760
+ * @returns closer for the watcher instance
1761
+ */
1762
+ _watchWithNodeFs(path6, listener) {
1763
+ const opts = this.fsw.options;
1764
+ const directory = sysPath.dirname(path6);
1765
+ const basename4 = sysPath.basename(path6);
1766
+ const parent = this.fsw._getWatchedDir(directory);
1767
+ parent.add(basename4);
1768
+ const absolutePath = sysPath.resolve(path6);
1769
+ const options = {
1770
+ persistent: opts.persistent
1771
+ };
1772
+ if (!listener)
1773
+ listener = EMPTY_FN;
1774
+ let closer;
1775
+ if (opts.usePolling) {
1776
+ const enableBin = opts.interval !== opts.binaryInterval;
1777
+ options.interval = enableBin && isBinaryPath(basename4) ? opts.binaryInterval : opts.interval;
1778
+ closer = setFsWatchFileListener(path6, absolutePath, options, {
1779
+ listener,
1780
+ rawEmitter: this.fsw._emitRaw
1781
+ });
1782
+ } else {
1783
+ closer = setFsWatchListener(path6, absolutePath, options, {
1784
+ listener,
1785
+ errHandler: this._boundHandleError,
1786
+ rawEmitter: this.fsw._emitRaw
1787
+ });
1788
+ }
1789
+ return closer;
1790
+ }
1791
+ /**
1792
+ * Watch a file and emit add event if warranted.
1793
+ * @returns closer for the watcher instance
1794
+ */
1795
+ _handleFile(file, stats, initialAdd) {
1796
+ if (this.fsw.closed) {
1797
+ return;
1798
+ }
1799
+ const dirname3 = sysPath.dirname(file);
1800
+ const basename4 = sysPath.basename(file);
1801
+ const parent = this.fsw._getWatchedDir(dirname3);
1802
+ let prevStats = stats;
1803
+ if (parent.has(basename4))
1804
+ return;
1805
+ const listener = async (path6, newStats) => {
1806
+ if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5))
1807
+ return;
1808
+ if (!newStats || newStats.mtimeMs === 0) {
1809
+ try {
1810
+ const newStats2 = await stat2(file);
1811
+ if (this.fsw.closed)
1812
+ return;
1813
+ const at = newStats2.atimeMs;
1814
+ const mt = newStats2.mtimeMs;
1815
+ if (!at || at <= mt || mt !== prevStats.mtimeMs) {
1816
+ this.fsw._emit(EV.CHANGE, file, newStats2);
1817
+ }
1818
+ if ((isMacos || isLinux || isFreeBSD) && prevStats.ino !== newStats2.ino) {
1819
+ this.fsw._closeFile(path6);
1820
+ prevStats = newStats2;
1821
+ const closer2 = this._watchWithNodeFs(file, listener);
1822
+ if (closer2)
1823
+ this.fsw._addPathCloser(path6, closer2);
1824
+ } else {
1825
+ prevStats = newStats2;
1826
+ }
1827
+ } catch (error) {
1828
+ this.fsw._remove(dirname3, basename4);
1829
+ }
1830
+ } else if (parent.has(basename4)) {
1831
+ const at = newStats.atimeMs;
1832
+ const mt = newStats.mtimeMs;
1833
+ if (!at || at <= mt || mt !== prevStats.mtimeMs) {
1834
+ this.fsw._emit(EV.CHANGE, file, newStats);
1835
+ }
1836
+ prevStats = newStats;
1837
+ }
1838
+ };
1839
+ const closer = this._watchWithNodeFs(file, listener);
1840
+ if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
1841
+ if (!this.fsw._throttle(EV.ADD, file, 0))
1842
+ return;
1843
+ this.fsw._emit(EV.ADD, file, stats);
1844
+ }
1845
+ return closer;
1846
+ }
1847
+ /**
1848
+ * Handle symlinks encountered while reading a dir.
1849
+ * @param entry returned by readdirp
1850
+ * @param directory path of dir being read
1851
+ * @param path of this item
1852
+ * @param item basename of this item
1853
+ * @returns true if no more processing is needed for this entry.
1854
+ */
1855
+ async _handleSymlink(entry, directory, path6, item) {
1856
+ if (this.fsw.closed) {
1857
+ return;
1858
+ }
1859
+ const full = entry.fullPath;
1860
+ const dir = this.fsw._getWatchedDir(directory);
1861
+ if (!this.fsw.options.followSymlinks) {
1862
+ this.fsw._incrReadyCount();
1863
+ let linkPath;
1864
+ try {
1865
+ linkPath = await fsrealpath(path6);
1866
+ } catch (e) {
1867
+ this.fsw._emitReady();
1868
+ return true;
1869
+ }
1870
+ if (this.fsw.closed)
1871
+ return;
1872
+ if (dir.has(item)) {
1873
+ if (this.fsw._symlinkPaths.get(full) !== linkPath) {
1874
+ this.fsw._symlinkPaths.set(full, linkPath);
1875
+ this.fsw._emit(EV.CHANGE, path6, entry.stats);
1876
+ }
1877
+ } else {
1878
+ dir.add(item);
1879
+ this.fsw._symlinkPaths.set(full, linkPath);
1880
+ this.fsw._emit(EV.ADD, path6, entry.stats);
1881
+ }
1882
+ this.fsw._emitReady();
1883
+ return true;
1884
+ }
1885
+ if (this.fsw._symlinkPaths.has(full)) {
1886
+ return true;
1887
+ }
1888
+ this.fsw._symlinkPaths.set(full, true);
1889
+ }
1890
+ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
1891
+ directory = sysPath.join(directory, "");
1892
+ throttler = this.fsw._throttle("readdir", directory, 1e3);
1893
+ if (!throttler)
1894
+ return;
1895
+ const previous = this.fsw._getWatchedDir(wh.path);
1896
+ const current = /* @__PURE__ */ new Set();
1897
+ let stream = this.fsw._readdirp(directory, {
1898
+ fileFilter: (entry) => wh.filterPath(entry),
1899
+ directoryFilter: (entry) => wh.filterDir(entry)
1900
+ });
1901
+ if (!stream)
1902
+ return;
1903
+ stream.on(STR_DATA, async (entry) => {
1904
+ if (this.fsw.closed) {
1905
+ stream = void 0;
1906
+ return;
1907
+ }
1908
+ const item = entry.path;
1909
+ let path6 = sysPath.join(directory, item);
1910
+ current.add(item);
1911
+ if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path6, item)) {
1912
+ return;
1913
+ }
1914
+ if (this.fsw.closed) {
1915
+ stream = void 0;
1916
+ return;
1917
+ }
1918
+ if (item === target || !target && !previous.has(item)) {
1919
+ this.fsw._incrReadyCount();
1920
+ path6 = sysPath.join(dir, sysPath.relative(dir, path6));
1921
+ this._addToNodeFs(path6, initialAdd, wh, depth + 1);
1922
+ }
1923
+ }).on(EV.ERROR, this._boundHandleError);
1924
+ return new Promise((resolve3, reject) => {
1925
+ if (!stream)
1926
+ return reject();
1927
+ stream.once(STR_END, () => {
1928
+ if (this.fsw.closed) {
1929
+ stream = void 0;
1930
+ return;
1931
+ }
1932
+ const wasThrottled = throttler ? throttler.clear() : false;
1933
+ resolve3(void 0);
1934
+ previous.getChildren().filter((item) => {
1935
+ return item !== directory && !current.has(item);
1936
+ }).forEach((item) => {
1937
+ this.fsw._remove(directory, item);
1938
+ });
1939
+ stream = void 0;
1940
+ if (wasThrottled)
1941
+ this._handleRead(directory, false, wh, target, dir, depth, throttler);
1942
+ });
1943
+ });
1944
+ }
1945
+ /**
1946
+ * Read directory to add / remove files from `@watched` list and re-read it on change.
1947
+ * @param dir fs path
1948
+ * @param stats
1949
+ * @param initialAdd
1950
+ * @param depth relative to user-supplied path
1951
+ * @param target child path targeted for watch
1952
+ * @param wh Common watch helpers for this path
1953
+ * @param realpath
1954
+ * @returns closer for the watcher instance.
1955
+ */
1956
+ async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath2) {
1957
+ const parentDir = this.fsw._getWatchedDir(sysPath.dirname(dir));
1958
+ const tracked = parentDir.has(sysPath.basename(dir));
1959
+ if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
1960
+ this.fsw._emit(EV.ADD_DIR, dir, stats);
1961
+ }
1962
+ parentDir.add(sysPath.basename(dir));
1963
+ this.fsw._getWatchedDir(dir);
1964
+ let throttler;
1965
+ let closer;
1966
+ const oDepth = this.fsw.options.depth;
1967
+ if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath2)) {
1968
+ if (!target) {
1969
+ await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
1970
+ if (this.fsw.closed)
1971
+ return;
1972
+ }
1973
+ closer = this._watchWithNodeFs(dir, (dirPath, stats2) => {
1974
+ if (stats2 && stats2.mtimeMs === 0)
1975
+ return;
1976
+ this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
1977
+ });
1978
+ }
1979
+ return closer;
1980
+ }
1981
+ /**
1982
+ * Handle added file, directory, or glob pattern.
1983
+ * Delegates call to _handleFile / _handleDir after checks.
1984
+ * @param path to file or ir
1985
+ * @param initialAdd was the file added at watch instantiation?
1986
+ * @param priorWh depth relative to user-supplied path
1987
+ * @param depth Child path actually targeted for watch
1988
+ * @param target Child path actually targeted for watch
1989
+ */
1990
+ async _addToNodeFs(path6, initialAdd, priorWh, depth, target) {
1991
+ const ready = this.fsw._emitReady;
1992
+ if (this.fsw._isIgnored(path6) || this.fsw.closed) {
1993
+ ready();
1994
+ return false;
1995
+ }
1996
+ const wh = this.fsw._getWatchHelpers(path6);
1997
+ if (priorWh) {
1998
+ wh.filterPath = (entry) => priorWh.filterPath(entry);
1999
+ wh.filterDir = (entry) => priorWh.filterDir(entry);
2000
+ }
2001
+ try {
2002
+ const stats = await statMethods[wh.statMethod](wh.watchPath);
2003
+ if (this.fsw.closed)
2004
+ return;
2005
+ if (this.fsw._isIgnored(wh.watchPath, stats)) {
2006
+ ready();
2007
+ return false;
2008
+ }
2009
+ const follow = this.fsw.options.followSymlinks;
2010
+ let closer;
2011
+ if (stats.isDirectory()) {
2012
+ const absPath = sysPath.resolve(path6);
2013
+ const targetPath = follow ? await fsrealpath(path6) : path6;
2014
+ if (this.fsw.closed)
2015
+ return;
2016
+ closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
2017
+ if (this.fsw.closed)
2018
+ return;
2019
+ if (absPath !== targetPath && targetPath !== void 0) {
2020
+ this.fsw._symlinkPaths.set(absPath, targetPath);
2021
+ }
2022
+ } else if (stats.isSymbolicLink()) {
2023
+ const targetPath = follow ? await fsrealpath(path6) : path6;
2024
+ if (this.fsw.closed)
2025
+ return;
2026
+ const parent = sysPath.dirname(wh.watchPath);
2027
+ this.fsw._getWatchedDir(parent).add(wh.watchPath);
2028
+ this.fsw._emit(EV.ADD, wh.watchPath, stats);
2029
+ closer = await this._handleDir(parent, stats, initialAdd, depth, path6, wh, targetPath);
2030
+ if (this.fsw.closed)
2031
+ return;
2032
+ if (targetPath !== void 0) {
2033
+ this.fsw._symlinkPaths.set(sysPath.resolve(path6), targetPath);
2034
+ }
2035
+ } else {
2036
+ closer = this._handleFile(wh.watchPath, stats, initialAdd);
2037
+ }
2038
+ ready();
2039
+ if (closer)
2040
+ this.fsw._addPathCloser(path6, closer);
2041
+ return false;
2042
+ } catch (error) {
2043
+ if (this.fsw._handleError(error)) {
2044
+ ready();
2045
+ return path6;
2046
+ }
2047
+ }
2048
+ }
2049
+ };
2050
+
2051
+ // ../node_modules/chokidar/esm/index.js
2052
+ var SLASH = "/";
2053
+ var SLASH_SLASH = "//";
2054
+ var ONE_DOT = ".";
2055
+ var TWO_DOTS = "..";
2056
+ var STRING_TYPE = "string";
2057
+ var BACK_SLASH_RE = /\\/g;
2058
+ var DOUBLE_SLASH_RE = /\/\//;
2059
+ var DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
2060
+ var REPLACER_RE = /^\.[/\\]/;
2061
+ function arrify(item) {
2062
+ return Array.isArray(item) ? item : [item];
2063
+ }
2064
+ var isMatcherObject = (matcher) => typeof matcher === "object" && matcher !== null && !(matcher instanceof RegExp);
2065
+ function createPattern(matcher) {
2066
+ if (typeof matcher === "function")
2067
+ return matcher;
2068
+ if (typeof matcher === "string")
2069
+ return (string) => matcher === string;
2070
+ if (matcher instanceof RegExp)
2071
+ return (string) => matcher.test(string);
2072
+ if (typeof matcher === "object" && matcher !== null) {
2073
+ return (string) => {
2074
+ if (matcher.path === string)
2075
+ return true;
2076
+ if (matcher.recursive) {
2077
+ const relative3 = sysPath2.relative(matcher.path, string);
2078
+ if (!relative3) {
2079
+ return false;
2080
+ }
2081
+ return !relative3.startsWith("..") && !sysPath2.isAbsolute(relative3);
2082
+ }
2083
+ return false;
2084
+ };
2085
+ }
2086
+ return () => false;
2087
+ }
2088
+ function normalizePath(path6) {
2089
+ if (typeof path6 !== "string")
2090
+ throw new Error("string expected");
2091
+ path6 = sysPath2.normalize(path6);
2092
+ path6 = path6.replace(/\\/g, "/");
2093
+ let prepend = false;
2094
+ if (path6.startsWith("//"))
2095
+ prepend = true;
2096
+ const DOUBLE_SLASH_RE2 = /\/\//;
2097
+ while (path6.match(DOUBLE_SLASH_RE2))
2098
+ path6 = path6.replace(DOUBLE_SLASH_RE2, "/");
2099
+ if (prepend)
2100
+ path6 = "/" + path6;
2101
+ return path6;
2102
+ }
2103
+ function matchPatterns(patterns, testString, stats) {
2104
+ const path6 = normalizePath(testString);
2105
+ for (let index = 0; index < patterns.length; index++) {
2106
+ const pattern = patterns[index];
2107
+ if (pattern(path6, stats)) {
2108
+ return true;
2109
+ }
2110
+ }
2111
+ return false;
2112
+ }
2113
+ function anymatch(matchers, testString) {
2114
+ if (matchers == null) {
2115
+ throw new TypeError("anymatch: specify first argument");
2116
+ }
2117
+ const matchersArray = arrify(matchers);
2118
+ const patterns = matchersArray.map((matcher) => createPattern(matcher));
2119
+ if (testString == null) {
2120
+ return (testString2, stats) => {
2121
+ return matchPatterns(patterns, testString2, stats);
2122
+ };
2123
+ }
2124
+ return matchPatterns(patterns, testString);
2125
+ }
2126
+ var unifyPaths = (paths_) => {
2127
+ const paths = arrify(paths_).flat();
2128
+ if (!paths.every((p) => typeof p === STRING_TYPE)) {
2129
+ throw new TypeError(`Non-string provided as watch path: ${paths}`);
2130
+ }
2131
+ return paths.map(normalizePathToUnix);
2132
+ };
2133
+ var toUnix = (string) => {
2134
+ let str = string.replace(BACK_SLASH_RE, SLASH);
2135
+ let prepend = false;
2136
+ if (str.startsWith(SLASH_SLASH)) {
2137
+ prepend = true;
2138
+ }
2139
+ while (str.match(DOUBLE_SLASH_RE)) {
2140
+ str = str.replace(DOUBLE_SLASH_RE, SLASH);
2141
+ }
2142
+ if (prepend) {
2143
+ str = SLASH + str;
2144
+ }
2145
+ return str;
2146
+ };
2147
+ var normalizePathToUnix = (path6) => toUnix(sysPath2.normalize(toUnix(path6)));
2148
+ var normalizeIgnored = (cwd = "") => (path6) => {
2149
+ if (typeof path6 === "string") {
2150
+ return normalizePathToUnix(sysPath2.isAbsolute(path6) ? path6 : sysPath2.join(cwd, path6));
2151
+ } else {
2152
+ return path6;
2153
+ }
2154
+ };
2155
+ var getAbsolutePath = (path6, cwd) => {
2156
+ if (sysPath2.isAbsolute(path6)) {
2157
+ return path6;
2158
+ }
2159
+ return sysPath2.join(cwd, path6);
2160
+ };
2161
+ var EMPTY_SET = Object.freeze(/* @__PURE__ */ new Set());
2162
+ var DirEntry = class {
2163
+ constructor(dir, removeWatcher) {
2164
+ this.path = dir;
2165
+ this._removeWatcher = removeWatcher;
2166
+ this.items = /* @__PURE__ */ new Set();
2167
+ }
2168
+ add(item) {
2169
+ const { items } = this;
2170
+ if (!items)
2171
+ return;
2172
+ if (item !== ONE_DOT && item !== TWO_DOTS)
2173
+ items.add(item);
2174
+ }
2175
+ async remove(item) {
2176
+ const { items } = this;
2177
+ if (!items)
2178
+ return;
2179
+ items.delete(item);
2180
+ if (items.size > 0)
2181
+ return;
2182
+ const dir = this.path;
2183
+ try {
2184
+ await readdir2(dir);
2185
+ } catch (err) {
2186
+ if (this._removeWatcher) {
2187
+ this._removeWatcher(sysPath2.dirname(dir), sysPath2.basename(dir));
2188
+ }
2189
+ }
2190
+ }
2191
+ has(item) {
2192
+ const { items } = this;
2193
+ if (!items)
2194
+ return;
2195
+ return items.has(item);
2196
+ }
2197
+ getChildren() {
2198
+ const { items } = this;
2199
+ if (!items)
2200
+ return [];
2201
+ return [...items.values()];
2202
+ }
2203
+ dispose() {
2204
+ this.items.clear();
2205
+ this.path = "";
2206
+ this._removeWatcher = EMPTY_FN;
2207
+ this.items = EMPTY_SET;
2208
+ Object.freeze(this);
2209
+ }
2210
+ };
2211
+ var STAT_METHOD_F = "stat";
2212
+ var STAT_METHOD_L = "lstat";
2213
+ var WatchHelper = class {
2214
+ constructor(path6, follow, fsw) {
2215
+ this.fsw = fsw;
2216
+ const watchPath = path6;
2217
+ this.path = path6 = path6.replace(REPLACER_RE, "");
2218
+ this.watchPath = watchPath;
2219
+ this.fullWatchPath = sysPath2.resolve(watchPath);
2220
+ this.dirParts = [];
2221
+ this.dirParts.forEach((parts) => {
2222
+ if (parts.length > 1)
2223
+ parts.pop();
2224
+ });
2225
+ this.followSymlinks = follow;
2226
+ this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
2227
+ }
2228
+ entryPath(entry) {
2229
+ return sysPath2.join(this.watchPath, sysPath2.relative(this.watchPath, entry.fullPath));
2230
+ }
2231
+ filterPath(entry) {
2232
+ const { stats } = entry;
2233
+ if (stats && stats.isSymbolicLink())
2234
+ return this.filterDir(entry);
2235
+ const resolvedPath = this.entryPath(entry);
2236
+ return this.fsw._isntIgnored(resolvedPath, stats) && this.fsw._hasReadPermissions(stats);
2237
+ }
2238
+ filterDir(entry) {
2239
+ return this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
2240
+ }
2241
+ };
2242
+ var FSWatcher = class extends EventEmitter {
2243
+ // Not indenting methods for history sake; for now.
2244
+ constructor(_opts = {}) {
2245
+ super();
2246
+ this.closed = false;
2247
+ this._closers = /* @__PURE__ */ new Map();
2248
+ this._ignoredPaths = /* @__PURE__ */ new Set();
2249
+ this._throttled = /* @__PURE__ */ new Map();
2250
+ this._streams = /* @__PURE__ */ new Set();
2251
+ this._symlinkPaths = /* @__PURE__ */ new Map();
2252
+ this._watched = /* @__PURE__ */ new Map();
2253
+ this._pendingWrites = /* @__PURE__ */ new Map();
2254
+ this._pendingUnlinks = /* @__PURE__ */ new Map();
2255
+ this._readyCount = 0;
2256
+ this._readyEmitted = false;
2257
+ const awf = _opts.awaitWriteFinish;
2258
+ const DEF_AWF = { stabilityThreshold: 2e3, pollInterval: 100 };
2259
+ const opts = {
2260
+ // Defaults
2261
+ persistent: true,
2262
+ ignoreInitial: false,
2263
+ ignorePermissionErrors: false,
2264
+ interval: 100,
2265
+ binaryInterval: 300,
2266
+ followSymlinks: true,
2267
+ usePolling: false,
2268
+ // useAsync: false,
2269
+ atomic: true,
2270
+ // NOTE: overwritten later (depends on usePolling)
2271
+ ..._opts,
2272
+ // Change format
2273
+ ignored: _opts.ignored ? arrify(_opts.ignored) : arrify([]),
2274
+ awaitWriteFinish: awf === true ? DEF_AWF : typeof awf === "object" ? { ...DEF_AWF, ...awf } : false
2275
+ };
2276
+ if (isIBMi)
2277
+ opts.usePolling = true;
2278
+ if (opts.atomic === void 0)
2279
+ opts.atomic = !opts.usePolling;
2280
+ const envPoll = process.env.CHOKIDAR_USEPOLLING;
2281
+ if (envPoll !== void 0) {
2282
+ const envLower = envPoll.toLowerCase();
2283
+ if (envLower === "false" || envLower === "0")
2284
+ opts.usePolling = false;
2285
+ else if (envLower === "true" || envLower === "1")
2286
+ opts.usePolling = true;
2287
+ else
2288
+ opts.usePolling = !!envLower;
2289
+ }
2290
+ const envInterval = process.env.CHOKIDAR_INTERVAL;
2291
+ if (envInterval)
2292
+ opts.interval = Number.parseInt(envInterval, 10);
2293
+ let readyCalls = 0;
2294
+ this._emitReady = () => {
2295
+ readyCalls++;
2296
+ if (readyCalls >= this._readyCount) {
2297
+ this._emitReady = EMPTY_FN;
2298
+ this._readyEmitted = true;
2299
+ process.nextTick(() => this.emit(EVENTS.READY));
2300
+ }
2301
+ };
2302
+ this._emitRaw = (...args) => this.emit(EVENTS.RAW, ...args);
2303
+ this._boundRemove = this._remove.bind(this);
2304
+ this.options = opts;
2305
+ this._nodeFsHandler = new NodeFsHandler(this);
2306
+ Object.freeze(opts);
2307
+ }
2308
+ _addIgnoredPath(matcher) {
2309
+ if (isMatcherObject(matcher)) {
2310
+ for (const ignored of this._ignoredPaths) {
2311
+ if (isMatcherObject(ignored) && ignored.path === matcher.path && ignored.recursive === matcher.recursive) {
2312
+ return;
2313
+ }
2314
+ }
2315
+ }
2316
+ this._ignoredPaths.add(matcher);
2317
+ }
2318
+ _removeIgnoredPath(matcher) {
2319
+ this._ignoredPaths.delete(matcher);
2320
+ if (typeof matcher === "string") {
2321
+ for (const ignored of this._ignoredPaths) {
2322
+ if (isMatcherObject(ignored) && ignored.path === matcher) {
2323
+ this._ignoredPaths.delete(ignored);
2324
+ }
2325
+ }
2326
+ }
2327
+ }
2328
+ // Public methods
2329
+ /**
2330
+ * Adds paths to be watched on an existing FSWatcher instance.
2331
+ * @param paths_ file or file list. Other arguments are unused
2332
+ */
2333
+ add(paths_, _origAdd, _internal) {
2334
+ const { cwd } = this.options;
2335
+ this.closed = false;
2336
+ this._closePromise = void 0;
2337
+ let paths = unifyPaths(paths_);
2338
+ if (cwd) {
2339
+ paths = paths.map((path6) => {
2340
+ const absPath = getAbsolutePath(path6, cwd);
2341
+ return absPath;
2342
+ });
2343
+ }
2344
+ paths.forEach((path6) => {
2345
+ this._removeIgnoredPath(path6);
2346
+ });
2347
+ this._userIgnored = void 0;
2348
+ if (!this._readyCount)
2349
+ this._readyCount = 0;
2350
+ this._readyCount += paths.length;
2351
+ Promise.all(paths.map(async (path6) => {
2352
+ const res = await this._nodeFsHandler._addToNodeFs(path6, !_internal, void 0, 0, _origAdd);
2353
+ if (res)
2354
+ this._emitReady();
2355
+ return res;
2356
+ })).then((results) => {
2357
+ if (this.closed)
2358
+ return;
2359
+ results.forEach((item) => {
2360
+ if (item)
2361
+ this.add(sysPath2.dirname(item), sysPath2.basename(_origAdd || item));
2362
+ });
2363
+ });
2364
+ return this;
2365
+ }
2366
+ /**
2367
+ * Close watchers or start ignoring events from specified paths.
2368
+ */
2369
+ unwatch(paths_) {
2370
+ if (this.closed)
2371
+ return this;
2372
+ const paths = unifyPaths(paths_);
2373
+ const { cwd } = this.options;
2374
+ paths.forEach((path6) => {
2375
+ if (!sysPath2.isAbsolute(path6) && !this._closers.has(path6)) {
2376
+ if (cwd)
2377
+ path6 = sysPath2.join(cwd, path6);
2378
+ path6 = sysPath2.resolve(path6);
2379
+ }
2380
+ this._closePath(path6);
2381
+ this._addIgnoredPath(path6);
2382
+ if (this._watched.has(path6)) {
2383
+ this._addIgnoredPath({
2384
+ path: path6,
2385
+ recursive: true
2386
+ });
2387
+ }
2388
+ this._userIgnored = void 0;
2389
+ });
2390
+ return this;
2391
+ }
2392
+ /**
2393
+ * Close watchers and remove all listeners from watched paths.
2394
+ */
2395
+ close() {
2396
+ if (this._closePromise) {
2397
+ return this._closePromise;
2398
+ }
2399
+ this.closed = true;
2400
+ this.removeAllListeners();
2401
+ const closers = [];
2402
+ this._closers.forEach((closerList) => closerList.forEach((closer) => {
2403
+ const promise = closer();
2404
+ if (promise instanceof Promise)
2405
+ closers.push(promise);
2406
+ }));
2407
+ this._streams.forEach((stream) => stream.destroy());
2408
+ this._userIgnored = void 0;
2409
+ this._readyCount = 0;
2410
+ this._readyEmitted = false;
2411
+ this._watched.forEach((dirent) => dirent.dispose());
2412
+ this._closers.clear();
2413
+ this._watched.clear();
2414
+ this._streams.clear();
2415
+ this._symlinkPaths.clear();
2416
+ this._throttled.clear();
2417
+ this._closePromise = closers.length ? Promise.all(closers).then(() => void 0) : Promise.resolve();
2418
+ return this._closePromise;
2419
+ }
2420
+ /**
2421
+ * Expose list of watched paths
2422
+ * @returns for chaining
2423
+ */
2424
+ getWatched() {
2425
+ const watchList = {};
2426
+ this._watched.forEach((entry, dir) => {
2427
+ const key = this.options.cwd ? sysPath2.relative(this.options.cwd, dir) : dir;
2428
+ const index = key || ONE_DOT;
2429
+ watchList[index] = entry.getChildren().sort();
2430
+ });
2431
+ return watchList;
2432
+ }
2433
+ emitWithAll(event, args) {
2434
+ this.emit(event, ...args);
2435
+ if (event !== EVENTS.ERROR)
2436
+ this.emit(EVENTS.ALL, event, ...args);
2437
+ }
2438
+ // Common helpers
2439
+ // --------------
2440
+ /**
2441
+ * Normalize and emit events.
2442
+ * Calling _emit DOES NOT MEAN emit() would be called!
2443
+ * @param event Type of event
2444
+ * @param path File or directory path
2445
+ * @param stats arguments to be passed with event
2446
+ * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
2447
+ */
2448
+ async _emit(event, path6, stats) {
2449
+ if (this.closed)
2450
+ return;
2451
+ const opts = this.options;
2452
+ if (isWindows)
2453
+ path6 = sysPath2.normalize(path6);
2454
+ if (opts.cwd)
2455
+ path6 = sysPath2.relative(opts.cwd, path6);
2456
+ const args = [path6];
2457
+ if (stats != null)
2458
+ args.push(stats);
2459
+ const awf = opts.awaitWriteFinish;
2460
+ let pw;
2461
+ if (awf && (pw = this._pendingWrites.get(path6))) {
2462
+ pw.lastChange = /* @__PURE__ */ new Date();
2463
+ return this;
2464
+ }
2465
+ if (opts.atomic) {
2466
+ if (event === EVENTS.UNLINK) {
2467
+ this._pendingUnlinks.set(path6, [event, ...args]);
2468
+ setTimeout(() => {
2469
+ this._pendingUnlinks.forEach((entry, path7) => {
2470
+ this.emit(...entry);
2471
+ this.emit(EVENTS.ALL, ...entry);
2472
+ this._pendingUnlinks.delete(path7);
2473
+ });
2474
+ }, typeof opts.atomic === "number" ? opts.atomic : 100);
2475
+ return this;
2476
+ }
2477
+ if (event === EVENTS.ADD && this._pendingUnlinks.has(path6)) {
2478
+ event = EVENTS.CHANGE;
2479
+ this._pendingUnlinks.delete(path6);
2480
+ }
2481
+ }
2482
+ if (awf && (event === EVENTS.ADD || event === EVENTS.CHANGE) && this._readyEmitted) {
2483
+ const awfEmit = (err, stats2) => {
2484
+ if (err) {
2485
+ event = EVENTS.ERROR;
2486
+ args[0] = err;
2487
+ this.emitWithAll(event, args);
2488
+ } else if (stats2) {
2489
+ if (args.length > 1) {
2490
+ args[1] = stats2;
2491
+ } else {
2492
+ args.push(stats2);
2493
+ }
2494
+ this.emitWithAll(event, args);
2495
+ }
2496
+ };
2497
+ this._awaitWriteFinish(path6, awf.stabilityThreshold, event, awfEmit);
2498
+ return this;
2499
+ }
2500
+ if (event === EVENTS.CHANGE) {
2501
+ const isThrottled = !this._throttle(EVENTS.CHANGE, path6, 50);
2502
+ if (isThrottled)
2503
+ return this;
2504
+ }
2505
+ if (opts.alwaysStat && stats === void 0 && (event === EVENTS.ADD || event === EVENTS.ADD_DIR || event === EVENTS.CHANGE)) {
2506
+ const fullPath = opts.cwd ? sysPath2.join(opts.cwd, path6) : path6;
2507
+ let stats2;
2508
+ try {
2509
+ stats2 = await stat3(fullPath);
2510
+ } catch (err) {
2511
+ }
2512
+ if (!stats2 || this.closed)
2513
+ return;
2514
+ args.push(stats2);
2515
+ }
2516
+ this.emitWithAll(event, args);
2517
+ return this;
2518
+ }
2519
+ /**
2520
+ * Common handler for errors
2521
+ * @returns The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
2522
+ */
2523
+ _handleError(error) {
2524
+ const code = error && error.code;
2525
+ if (error && code !== "ENOENT" && code !== "ENOTDIR" && (!this.options.ignorePermissionErrors || code !== "EPERM" && code !== "EACCES")) {
2526
+ this.emit(EVENTS.ERROR, error);
2527
+ }
2528
+ return error || this.closed;
2529
+ }
2530
+ /**
2531
+ * Helper utility for throttling
2532
+ * @param actionType type being throttled
2533
+ * @param path being acted upon
2534
+ * @param timeout duration of time to suppress duplicate actions
2535
+ * @returns tracking object or false if action should be suppressed
2536
+ */
2537
+ _throttle(actionType, path6, timeout) {
2538
+ if (!this._throttled.has(actionType)) {
2539
+ this._throttled.set(actionType, /* @__PURE__ */ new Map());
2540
+ }
2541
+ const action = this._throttled.get(actionType);
2542
+ if (!action)
2543
+ throw new Error("invalid throttle");
2544
+ const actionPath = action.get(path6);
2545
+ if (actionPath) {
2546
+ actionPath.count++;
2547
+ return false;
2548
+ }
2549
+ let timeoutObject;
2550
+ const clear = () => {
2551
+ const item = action.get(path6);
2552
+ const count = item ? item.count : 0;
2553
+ action.delete(path6);
2554
+ clearTimeout(timeoutObject);
2555
+ if (item)
2556
+ clearTimeout(item.timeoutObject);
2557
+ return count;
2558
+ };
2559
+ timeoutObject = setTimeout(clear, timeout);
2560
+ const thr = { timeoutObject, clear, count: 0 };
2561
+ action.set(path6, thr);
2562
+ return thr;
2563
+ }
2564
+ _incrReadyCount() {
2565
+ return this._readyCount++;
2566
+ }
2567
+ /**
2568
+ * Awaits write operation to finish.
2569
+ * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
2570
+ * @param path being acted upon
2571
+ * @param threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
2572
+ * @param event
2573
+ * @param awfEmit Callback to be called when ready for event to be emitted.
2574
+ */
2575
+ _awaitWriteFinish(path6, threshold, event, awfEmit) {
2576
+ const awf = this.options.awaitWriteFinish;
2577
+ if (typeof awf !== "object")
2578
+ return;
2579
+ const pollInterval = awf.pollInterval;
2580
+ let timeoutHandler;
2581
+ let fullPath = path6;
2582
+ if (this.options.cwd && !sysPath2.isAbsolute(path6)) {
2583
+ fullPath = sysPath2.join(this.options.cwd, path6);
2584
+ }
2585
+ const now = /* @__PURE__ */ new Date();
2586
+ const writes = this._pendingWrites;
2587
+ function awaitWriteFinishFn(prevStat) {
2588
+ statcb(fullPath, (err, curStat) => {
2589
+ if (err || !writes.has(path6)) {
2590
+ if (err && err.code !== "ENOENT")
2591
+ awfEmit(err);
2592
+ return;
2593
+ }
2594
+ const now2 = Number(/* @__PURE__ */ new Date());
2595
+ if (prevStat && curStat.size !== prevStat.size) {
2596
+ writes.get(path6).lastChange = now2;
2597
+ }
2598
+ const pw = writes.get(path6);
2599
+ const df = now2 - pw.lastChange;
2600
+ if (df >= threshold) {
2601
+ writes.delete(path6);
2602
+ awfEmit(void 0, curStat);
2603
+ } else {
2604
+ timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval, curStat);
2605
+ }
2606
+ });
2607
+ }
2608
+ if (!writes.has(path6)) {
2609
+ writes.set(path6, {
2610
+ lastChange: now,
2611
+ cancelWait: () => {
2612
+ writes.delete(path6);
2613
+ clearTimeout(timeoutHandler);
2614
+ return event;
2615
+ }
2616
+ });
2617
+ timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval);
2618
+ }
2619
+ }
2620
+ /**
2621
+ * Determines whether user has asked to ignore this path.
2622
+ */
2623
+ _isIgnored(path6, stats) {
2624
+ if (this.options.atomic && DOT_RE.test(path6))
2625
+ return true;
2626
+ if (!this._userIgnored) {
2627
+ const { cwd } = this.options;
2628
+ const ign = this.options.ignored;
2629
+ const ignored = (ign || []).map(normalizeIgnored(cwd));
2630
+ const ignoredPaths = [...this._ignoredPaths];
2631
+ const list = [...ignoredPaths.map(normalizeIgnored(cwd)), ...ignored];
2632
+ this._userIgnored = anymatch(list, void 0);
2633
+ }
2634
+ return this._userIgnored(path6, stats);
2635
+ }
2636
+ _isntIgnored(path6, stat4) {
2637
+ return !this._isIgnored(path6, stat4);
2638
+ }
2639
+ /**
2640
+ * Provides a set of common helpers and properties relating to symlink handling.
2641
+ * @param path file or directory pattern being watched
2642
+ */
2643
+ _getWatchHelpers(path6) {
2644
+ return new WatchHelper(path6, this.options.followSymlinks, this);
2645
+ }
2646
+ // Directory helpers
2647
+ // -----------------
2648
+ /**
2649
+ * Provides directory tracking objects
2650
+ * @param directory path of the directory
2651
+ */
2652
+ _getWatchedDir(directory) {
2653
+ const dir = sysPath2.resolve(directory);
2654
+ if (!this._watched.has(dir))
2655
+ this._watched.set(dir, new DirEntry(dir, this._boundRemove));
2656
+ return this._watched.get(dir);
2657
+ }
2658
+ // File helpers
2659
+ // ------------
2660
+ /**
2661
+ * Check for read permissions: https://stackoverflow.com/a/11781404/1358405
2662
+ */
2663
+ _hasReadPermissions(stats) {
2664
+ if (this.options.ignorePermissionErrors)
2665
+ return true;
2666
+ return Boolean(Number(stats.mode) & 256);
2667
+ }
2668
+ /**
2669
+ * Handles emitting unlink events for
2670
+ * files and directories, and via recursion, for
2671
+ * files and directories within directories that are unlinked
2672
+ * @param directory within which the following item is located
2673
+ * @param item base path of item/directory
2674
+ */
2675
+ _remove(directory, item, isDirectory) {
2676
+ const path6 = sysPath2.join(directory, item);
2677
+ const fullPath = sysPath2.resolve(path6);
2678
+ isDirectory = isDirectory != null ? isDirectory : this._watched.has(path6) || this._watched.has(fullPath);
2679
+ if (!this._throttle("remove", path6, 100))
2680
+ return;
2681
+ if (!isDirectory && this._watched.size === 1) {
2682
+ this.add(directory, item, true);
2683
+ }
2684
+ const wp = this._getWatchedDir(path6);
2685
+ const nestedDirectoryChildren = wp.getChildren();
2686
+ nestedDirectoryChildren.forEach((nested) => this._remove(path6, nested));
2687
+ const parent = this._getWatchedDir(directory);
2688
+ const wasTracked = parent.has(item);
2689
+ parent.remove(item);
2690
+ if (this._symlinkPaths.has(fullPath)) {
2691
+ this._symlinkPaths.delete(fullPath);
2692
+ }
2693
+ let relPath = path6;
2694
+ if (this.options.cwd)
2695
+ relPath = sysPath2.relative(this.options.cwd, path6);
2696
+ if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
2697
+ const event = this._pendingWrites.get(relPath).cancelWait();
2698
+ if (event === EVENTS.ADD)
2699
+ return;
2700
+ }
2701
+ this._watched.delete(path6);
2702
+ this._watched.delete(fullPath);
2703
+ const eventName = isDirectory ? EVENTS.UNLINK_DIR : EVENTS.UNLINK;
2704
+ if (wasTracked && !this._isIgnored(path6))
2705
+ this._emit(eventName, path6);
2706
+ this._closePath(path6);
2707
+ }
2708
+ /**
2709
+ * Closes all watchers for a path
2710
+ */
2711
+ _closePath(path6) {
2712
+ this._closeFile(path6);
2713
+ const dir = sysPath2.dirname(path6);
2714
+ this._getWatchedDir(dir).remove(sysPath2.basename(path6));
2715
+ }
2716
+ /**
2717
+ * Closes only file-specific watchers
2718
+ */
2719
+ _closeFile(path6) {
2720
+ const closers = this._closers.get(path6);
2721
+ if (!closers)
2722
+ return;
2723
+ closers.forEach((closer) => closer());
2724
+ this._closers.delete(path6);
2725
+ }
2726
+ _addPathCloser(path6, closer) {
2727
+ if (!closer)
2728
+ return;
2729
+ let list = this._closers.get(path6);
2730
+ if (!list) {
2731
+ list = [];
2732
+ this._closers.set(path6, list);
2733
+ }
2734
+ list.push(closer);
2735
+ }
2736
+ _readdirp(root, opts) {
2737
+ if (this.closed)
2738
+ return;
2739
+ const options = { type: EVENTS.ALL, alwaysStat: true, lstat: true, ...opts, depth: 0 };
2740
+ let stream = readdirp(root, options);
2741
+ this._streams.add(stream);
2742
+ stream.once(STR_CLOSE, () => {
2743
+ stream = void 0;
2744
+ });
2745
+ stream.once(STR_END, () => {
2746
+ if (stream) {
2747
+ this._streams.delete(stream);
2748
+ stream = void 0;
2749
+ }
2750
+ });
2751
+ return stream;
2752
+ }
2753
+ };
2754
+ function watch(paths, options = {}) {
2755
+ const watcher = new FSWatcher(options);
2756
+ watcher.add(paths);
2757
+ return watcher;
2758
+ }
2759
+ var esm_default = { watch, FSWatcher };
2760
+
2761
+ // src/watch.ts
2762
+ var DEBOUNCE_MS = 1200;
2763
+ async function watch2(runner) {
2764
+ await safeSync(runner, "startup");
2765
+ const paths = runner.watchPaths();
2766
+ const watcher = esm_default.watch(paths, {
2767
+ ignoreInitial: true,
2768
+ awaitWriteFinish: { stabilityThreshold: 400, pollInterval: 100 },
2769
+ ignored: (p) => !(p.endsWith(".jsonl") || !p.includes("."))
2770
+ });
2771
+ let timer;
2772
+ let pending = false;
2773
+ let running = false;
2774
+ const schedule = () => {
2775
+ if (timer) clearTimeout(timer);
2776
+ timer = setTimeout(async () => {
2777
+ if (running) {
2778
+ pending = true;
2779
+ return;
2780
+ }
2781
+ running = true;
2782
+ await safeSync(runner, "change");
2783
+ running = false;
2784
+ if (pending) {
2785
+ pending = false;
2786
+ schedule();
2787
+ }
2788
+ }, DEBOUNCE_MS);
2789
+ };
2790
+ watcher.on("add", schedule).on("change", schedule);
2791
+ console.log(`Watching for usage in:
2792
+ ${paths.join("\n ")}
2793
+ Press Ctrl+C to stop.`);
2794
+ await new Promise((resolve3) => {
2795
+ process.on("SIGINT", () => {
2796
+ void watcher.close().then(resolve3);
2797
+ });
2798
+ });
2799
+ }
2800
+ async function safeSync(runner, reason) {
2801
+ try {
2802
+ const { newTurns, byProvider } = await runner.syncOnce();
2803
+ if (newTurns > 0) {
2804
+ const detail = Object.entries(byProvider).map(([p, n]) => `${p}:${n}`).join(" ");
2805
+ console.log(`[${(/* @__PURE__ */ new Date()).toLocaleTimeString()}] synced ${newTurns} new turn(s) (${detail})`);
2806
+ } else if (reason === "startup") {
2807
+ console.log(`[${(/* @__PURE__ */ new Date()).toLocaleTimeString()}] up to date`);
2808
+ }
2809
+ } catch (err) {
2810
+ console.error(`Sync error: ${err.message}`);
2811
+ }
2812
+ }
2813
+
2814
+ // src/cli.ts
2815
+ var USAGE = `Tokelytics agent
2816
+
2817
+ Usage:
2818
+ tokelytics login Sign in by approving in your browser
2819
+ tokelytics sync Run one incremental sync to your dashboard
2820
+ tokelytics watch Stream usage in realtime (filesystem watcher)
2821
+ tokelytics status Show current sign-in and device
2822
+ tokelytics logout Forget stored credentials
2823
+ `;
2824
+ async function main(argv) {
2825
+ const cmd = argv[0];
2826
+ switch (cmd) {
2827
+ case "login": {
2828
+ const cfg = await loadConfig();
2829
+ const method = argv.includes("--google") ? "google" : argv.includes("--github") ? "github" : "handoff";
2830
+ const session = await interactiveLogin(cfg, method);
2831
+ try {
2832
+ await registerDevice(cfg, session);
2833
+ } catch (err) {
2834
+ console.warn(`(Couldn't register device yet \u2014 will retry on first sync: ${err.message})`);
2835
+ }
2836
+ console.log('Done. Now run "tokelytics watch" to stream your usage.');
2837
+ return 0;
2838
+ }
2839
+ case "sync": {
2840
+ const runner = await createRunner();
2841
+ const { newTurns, byProvider } = await runner.syncOnce();
2842
+ const detail = Object.entries(byProvider).map(([p, n]) => `${p}:${n}`).join(" ") || "none";
2843
+ console.log(`Synced ${newTurns} new turn(s) (${detail}).`);
2844
+ return 0;
2845
+ }
2846
+ case "watch": {
2847
+ const runner = await createRunner();
2848
+ await watch2(runner);
2849
+ return 0;
2850
+ }
2851
+ case "status": {
2852
+ const creds = await loadCredentials();
2853
+ const state = await loadState();
2854
+ if (!creds) {
2855
+ console.log('Not signed in. Run "tokelytics login".');
2856
+ } else {
2857
+ console.log(`Signed in as ${creds.email ?? creds.uid} (${creds.provider}).`);
2858
+ console.log(`Device id: ${state.deviceId}`);
2859
+ console.log(`Tracked files: ${Object.keys(state.scan.files).length}`);
2860
+ }
2861
+ try {
2862
+ const cfg = await loadConfig();
2863
+ const session = await restoreSession(cfg);
2864
+ if (session) await session.getToken();
2865
+ console.log(session ? "Session OK." : "No session.");
2866
+ } catch (err) {
2867
+ console.log(`Session needs refresh: ${err.message}`);
2868
+ }
2869
+ return 0;
2870
+ }
2871
+ case "logout": {
2872
+ await clearCredentials();
2873
+ console.log("Logged out.");
2874
+ return 0;
2875
+ }
2876
+ default:
2877
+ console.log(USAGE);
2878
+ return cmd ? 1 : 0;
2879
+ }
2880
+ }
2881
+ main(process.argv.slice(2)).then((code) => {
2882
+ process.exitCode = code;
2883
+ }).catch((err) => {
2884
+ console.error(`Error: ${err.message}`);
2885
+ process.exitCode = 1;
2886
+ });
2887
+ /*! Bundled license information:
2888
+
2889
+ chokidar/esm/index.js:
2890
+ (*! chokidar - MIT License (c) 2012 Paul Miller (paulmillr.com) *)
2891
+ */