|
1 | 1 | import type { Formatter } from 'picocolors/types'; |
2 | 2 | import { mkdirSync } from 'node:fs'; |
3 | | -import { tmpdir } from 'node:os'; |
| 3 | +import { tmpdir, userInfo } from 'node:os'; |
4 | 4 | import { join } from 'node:path'; |
5 | 5 | import process from 'node:process'; |
6 | 6 | import { formatCurrency } from '@ccusage/terminal/table'; |
@@ -39,8 +39,17 @@ function formatRemainingTime(remaining: number): string { |
39 | 39 | * Gets semaphore file for session-specific caching and process coordination |
40 | 40 | * Uses time-based expiry and transcript file modification detection for cache invalidation |
41 | 41 | */ |
| 42 | +function getSemaphoreDirName(uid: number): string { |
| 43 | + return uid === -1 ? 'ccusage-semaphore' : `ccusage-semaphore-${uid}`; |
| 44 | +} |
| 45 | + |
| 46 | +function getSemaphoreDir(): string { |
| 47 | + const { uid } = userInfo(); |
| 48 | + return join(tmpdir(), getSemaphoreDirName(uid)); |
| 49 | +} |
| 50 | + |
42 | 51 | function getSemaphore(sessionId: string): ReturnType<typeof createLimoJson<SemaphoreType | undefined>> { |
43 | | - const semaphoreDir = join(tmpdir(), 'ccusage-semaphore'); |
| 52 | + const semaphoreDir = getSemaphoreDir(); |
44 | 53 | const semaphorePath = join(semaphoreDir, `${sessionId}.lock`); |
45 | 54 |
|
46 | 55 | // Ensure semaphore directory exists |
@@ -538,3 +547,27 @@ export const statuslineCommand = define({ |
538 | 547 | } |
539 | 548 | }, |
540 | 549 | }); |
| 550 | + |
| 551 | +if (import.meta.vitest != null) { |
| 552 | + describe('getSemaphoreDirName', () => { |
| 553 | + it('includes uid in directory name on Unix (uid >= 0)', () => { |
| 554 | + const result = getSemaphoreDirName(1000); |
| 555 | + expect(result).toBe('ccusage-semaphore-1000'); |
| 556 | + }); |
| 557 | + |
| 558 | + it('excludes uid in directory name on Windows (uid === -1)', () => { |
| 559 | + const result = getSemaphoreDirName(-1); |
| 560 | + expect(result).toBe('ccusage-semaphore'); |
| 561 | + }); |
| 562 | + |
| 563 | + it('handles root uid (0) correctly', () => { |
| 564 | + const result = getSemaphoreDirName(0); |
| 565 | + expect(result).toBe('ccusage-semaphore-0'); |
| 566 | + }); |
| 567 | + |
| 568 | + it('handles various positive uid values', () => { |
| 569 | + expect(getSemaphoreDirName(501)).toBe('ccusage-semaphore-501'); |
| 570 | + expect(getSemaphoreDirName(65534)).toBe('ccusage-semaphore-65534'); |
| 571 | + }); |
| 572 | + }); |
| 573 | +} |
0 commit comments