UNPKG

vite-esbuild-typescript-checker

Version:

* Speeds up [TypeScript](https://github.com/Microsoft/TypeScript) type checking * Supports [Vue Single File Component](https://vuejs.org/v2/guide/single-file-components.html) * Displays nice error messages with the [code frame](https://babeljs.io/docs/en/

169 lines (168 loc) 5.29 kB
import { dirname, basename, join, normalize } from 'path'; import fs from 'fs'; const existsCache = new Map(); const readStatsCache = new Map(); const readFileCache = new Map(); const readDirCache = new Map(); const realPathCache = new Map(); /** * It's an implementation of the FileSystem interface which reads and writes directly to the real file system. */ export const realFileSystem = { exists (path) { return exists(getRealPath(path)); }, readFile (path, encoding) { return readFile(getRealPath(path), encoding); }, readDir (path) { return readDir(getRealPath(path)); }, readStats (path) { return readStats(getRealPath(path)); }, realPath (path) { return getRealPath(path); }, normalizePath (path) { return normalize(path); }, writeFile (path, data) { writeFile(getRealPath(path), data); }, deleteFile (path) { deleteFile(getRealPath(path)); }, createDir (path) { createDir(getRealPath(path)); }, updateTimes (path, atime, mtime) { updateTimes(getRealPath(path), atime, mtime); }, clearCache () { existsCache.clear(); readStatsCache.clear(); readFileCache.clear(); readDirCache.clear(); realPathCache.clear(); } }; // read methods function exists(path) { const normalizedPath = normalize(path); if (!existsCache.has(normalizedPath)) { existsCache.set(normalizedPath, fs.existsSync(normalizedPath)); } return !!existsCache.get(normalizedPath); } function readStats(path) { const normalizedPath = normalize(path); if (!readStatsCache.has(normalizedPath)) { if (exists(normalizedPath)) { readStatsCache.set(normalizedPath, fs.statSync(normalizedPath)); } } return readStatsCache.get(normalizedPath); } function readFile(path, encoding) { const normalizedPath = normalize(path); if (!readFileCache.has(normalizedPath)) { const stats = readStats(normalizedPath); if (stats && stats.isFile()) { readFileCache.set(normalizedPath, fs.readFileSync(normalizedPath, { encoding: encoding }).toString()); } else { readFileCache.set(normalizedPath, undefined); } } return readFileCache.get(normalizedPath); } function readDir(path) { const normalizedPath = normalize(path); if (!readDirCache.has(normalizedPath)) { const stats = readStats(normalizedPath); if (stats && stats.isDirectory()) { readDirCache.set(normalizedPath, fs.readdirSync(normalizedPath, { withFileTypes: true })); } else { readDirCache.set(normalizedPath, []); } } return readDirCache.get(normalizedPath) || []; } function getRealPath(path) { const normalizedPath = normalize(path); if (!realPathCache.has(normalizedPath)) { let base = normalizedPath; let nested = ''; while(base !== dirname(base)){ if (exists(base)) { realPathCache.set(normalizedPath, normalize(join(fs.realpathSync(base), nested))); break; } nested = join(basename(base), nested); base = dirname(base); } } return realPathCache.get(normalizedPath) || normalizedPath; } function createDir(path) { const normalizedPath = normalize(path); fs.mkdirSync(normalizedPath, { recursive: true }); // update cache existsCache.set(normalizedPath, true); if (readDirCache.has(dirname(normalizedPath))) { readDirCache.delete(dirname(normalizedPath)); } if (readStatsCache.has(normalizedPath)) { readStatsCache.delete(normalizedPath); } } function writeFile(path, data) { const normalizedPath = normalize(path); if (!exists(dirname(normalizedPath))) { createDir(dirname(normalizedPath)); } fs.writeFileSync(normalizedPath, data); // update cache existsCache.set(normalizedPath, true); if (readDirCache.has(dirname(normalizedPath))) { readDirCache.delete(dirname(normalizedPath)); } if (readStatsCache.has(normalizedPath)) { readStatsCache.delete(normalizedPath); } if (readFileCache.has(normalizedPath)) { readFileCache.delete(normalizedPath); } } function deleteFile(path) { if (exists(path)) { const normalizedPath = normalize(path); fs.unlinkSync(normalizedPath); // update cache existsCache.set(normalizedPath, false); if (readDirCache.has(dirname(normalizedPath))) { readDirCache.delete(dirname(normalizedPath)); } if (readStatsCache.has(normalizedPath)) { readStatsCache.delete(normalizedPath); } if (readFileCache.has(normalizedPath)) { readFileCache.delete(normalizedPath); } } } function updateTimes(path, atime, mtime) { if (exists(path)) { const normalizedPath = normalize(path); fs.utimesSync(normalize(path), atime, mtime); // update cache if (readStatsCache.has(normalizedPath)) { readStatsCache.delete(normalizedPath); } } }