UNPKG

soccer-go

Version:

Soccer CLI for stats and results.

56 lines (55 loc) 2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCacheDir = void 0; const os_1 = __importDefault(require("os")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const errors_1 = require("./errors"); const APP_CACHE_DIR_NAME = 'soccer-go'; /** * Gets the platform-specific, user-space cache directory path. * * This function covers the three main platforms: linux, macOS, and Windows. * Other platforms might have other directory conventions but, for now, a * temporary directory will suffice. */ const getPlatformSpecificCacheDirPath = () => { const homePath = os_1.default.homedir(); switch (os_1.default.platform()) { case 'linux': { return path_1.default.join(homePath, '.cache', APP_CACHE_DIR_NAME); } case 'darwin': { return path_1.default.join(homePath, 'Library', 'Caches', APP_CACHE_DIR_NAME); } case 'win32': { return path_1.default.join(homePath, 'AppData', 'Local', 'Temp', APP_CACHE_DIR_NAME); } default: { // Not sure yet what is the standard approach to app caching on other // platforms, so use a temporary folder. return path_1.default.join(os_1.default.tmpdir(), APP_CACHE_DIR_NAME); } } }; /** * Gets the application's cache directory path. * * If required, this function will also initialize the directory. */ const getCacheDir = () => { const cacheDirPath = getPlatformSpecificCacheDirPath(); try { fs_1.default.statSync(cacheDirPath); } catch (e) { if ((0, errors_1.isErrorNodeSystemError)(e) && e.code === 'ENOENT') { fs_1.default.mkdirSync(cacheDirPath, { recursive: true }); } } return cacheDirPath; }; exports.getCacheDir = getCacheDir;