( options?: SystemDirectoriesOptions, )
| 27 | * @param options Optional overrides for testing (env, homedir, platform) |
| 28 | */ |
| 29 | export function getSystemDirectories( |
| 30 | options?: SystemDirectoriesOptions, |
| 31 | ): SystemDirectories { |
| 32 | const platform = options?.platform ?? getPlatform() |
| 33 | const homeDir = options?.homedir ?? homedir() |
| 34 | const env = options?.env ?? process.env |
| 35 | |
| 36 | // Default paths used by most platforms |
| 37 | const defaults: SystemDirectories = { |
| 38 | HOME: homeDir, |
| 39 | DESKTOP: join(homeDir, 'Desktop'), |
| 40 | DOCUMENTS: join(homeDir, 'Documents'), |
| 41 | DOWNLOADS: join(homeDir, 'Downloads'), |
| 42 | } |
| 43 | |
| 44 | switch (platform) { |
| 45 | case 'windows': { |
| 46 | // Windows: Use USERPROFILE if available (handles localized folder names) |
| 47 | const userProfile = env.USERPROFILE || homeDir |
| 48 | return { |
| 49 | HOME: homeDir, |
| 50 | DESKTOP: join(userProfile, 'Desktop'), |
| 51 | DOCUMENTS: join(userProfile, 'Documents'), |
| 52 | DOWNLOADS: join(userProfile, 'Downloads'), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | case 'linux': |
| 57 | case 'wsl': { |
| 58 | // Linux/WSL: Check XDG Base Directory specification first |
| 59 | return { |
| 60 | HOME: homeDir, |
| 61 | DESKTOP: env.XDG_DESKTOP_DIR || defaults.DESKTOP, |
| 62 | DOCUMENTS: env.XDG_DOCUMENTS_DIR || defaults.DOCUMENTS, |
| 63 | DOWNLOADS: env.XDG_DOWNLOAD_DIR || defaults.DOWNLOADS, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | case 'macos': |
| 68 | default: { |
| 69 | // macOS and unknown platforms use standard paths |
| 70 | if (platform === 'unknown') { |
| 71 | logForDebugging(`Unknown platform detected, using default paths`) |
| 72 | } |
| 73 | return defaults |
| 74 | } |
| 75 | } |
| 76 | } |
no test coverage detected