|
|
import os from 'os' |
|
|
import path from 'path' |
|
|
import fs from 'fs' |
|
|
|
|
|
|
|
|
|
|
|
export function getCacheDirectory(fileDirectory: string, envPath?: string) { |
|
|
let result |
|
|
|
|
|
if (envPath) { |
|
|
result = envPath |
|
|
} else { |
|
|
let systemCacheDirectory |
|
|
if (process.platform === 'linux') { |
|
|
systemCacheDirectory = |
|
|
process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache') |
|
|
} else if (process.platform === 'darwin') { |
|
|
systemCacheDirectory = path.join(os.homedir(), 'Library', 'Caches') |
|
|
} else if (process.platform === 'win32') { |
|
|
systemCacheDirectory = |
|
|
process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local') |
|
|
} else { |
|
|
|
|
|
if (!systemCacheDirectory) { |
|
|
for (const dir of [ |
|
|
path.join(os.homedir(), '.cache'), |
|
|
path.join(os.tmpdir()), |
|
|
]) { |
|
|
if (fs.existsSync(dir)) { |
|
|
systemCacheDirectory = dir |
|
|
break |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (!systemCacheDirectory) { |
|
|
console.error(new Error('Unsupported platform: ' + process.platform)) |
|
|
process.exit(0) |
|
|
} |
|
|
} |
|
|
result = path.join(systemCacheDirectory, fileDirectory) |
|
|
} |
|
|
|
|
|
if (!path.isAbsolute(result)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = path.resolve(process.env['INIT_CWD'] || process.cwd(), result) |
|
|
} |
|
|
return result |
|
|
} |
|
|
|