UNPKG

@boost/config

Version:

Powerful convention based finder, loader, and manager of both configuration and ignore files.

65 lines (64 loc) 1.62 kB
import fs from 'node:fs'; import { Path } from '@boost/common'; import { ConfigError } from './ConfigError.mjs'; class Cache { constructor() { this.configDir = void 0; this.dirFilesCache = {}; // eslint-disable-next-line @typescript-eslint/no-explicit-any this.fileContentCache = {}; this.pkgPath = void 0; this.rootDir = void 0; } async cacheFileContents(path, commit) { const key = path.path(); const cache = this.fileContentCache[key]; const stats = await fs.promises.stat(path.path()); if (cache && cache.mtime === stats.mtimeMs) { return cache.content; } const content = await commit(); this.fileContentCache[key] = { content, exists: true, mtime: stats.mtimeMs }; return content; } async cacheFilesInDir(dir, hash, commit) { const key = dir.path() + hash; if (this.dirFilesCache[key]) { return this.dirFilesCache[key]; } const files = await commit(); this.dirFilesCache[key] = files; return files; } clearFileCache() { this.fileContentCache = {}; } clearFinderCache() { this.dirFilesCache = {}; } getFileCache(path) { return this.fileContentCache[path.path()] || null; } markMissingFile(path) { this.fileContentCache[path.path()] = { content: null, exists: false, mtime: 0 }; return this; } setRootDir(dir) { const root = Path.resolve(dir); if (!root.isDirectory()) { throw new ConfigError('ROOT_INVALID_DIR'); } this.rootDir = root; return this; } } export { Cache }; //# sourceMappingURL=Cache.mjs.map