|
| 1 | +import { dirname, normalize } from 'path'; |
| 2 | +import { fs as mem } from 'memfs'; |
| 3 | +import { FileSystem } from './FileSystem'; |
| 4 | +// eslint-disable-next-line node/no-unsupported-features/node-builtins |
| 5 | +import { Dirent, Stats } from 'fs'; |
| 6 | + |
| 7 | +/** |
| 8 | + * It's an implementation of FileSystem interface which reads from the real file system, but write to the in-memory file system. |
| 9 | + * |
| 10 | + * @param caseSensitive |
| 11 | + * @param realFileSystem |
| 12 | + */ |
| 13 | +function createPassiveFileSystem(caseSensitive = false, realFileSystem: FileSystem): FileSystem { |
| 14 | + function normalizePath(path: string): string { |
| 15 | + return caseSensitive ? normalize(path) : normalize(path).toLowerCase(); |
| 16 | + } |
| 17 | + |
| 18 | + function memExists(path: string): boolean { |
| 19 | + return mem.existsSync(normalizePath(path)); |
| 20 | + } |
| 21 | + |
| 22 | + function memReadStats(path: string): Stats | undefined { |
| 23 | + return memExists(path) ? mem.statSync(normalizePath(path)) : undefined; |
| 24 | + } |
| 25 | + |
| 26 | + function memReadFile(path: string, encoding?: string): string | undefined { |
| 27 | + if (memExists(path)) { |
| 28 | + return mem |
| 29 | + .readFileSync(normalizePath(path), { encoding: encoding as BufferEncoding }) |
| 30 | + .toString(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + function memReadDir(path: string): Dirent[] { |
| 35 | + if (memExists(path)) { |
| 36 | + return mem.readdirSync(normalizePath(path), { withFileTypes: true }) as Dirent[]; |
| 37 | + } |
| 38 | + |
| 39 | + return []; |
| 40 | + } |
| 41 | + |
| 42 | + function exists(path: string) { |
| 43 | + return realFileSystem.exists(path) || memExists(path); |
| 44 | + } |
| 45 | + |
| 46 | + function readFile(path: string, encoding?: string) { |
| 47 | + const fsStats = realFileSystem.readStats(path); |
| 48 | + const memStats = memReadStats(path); |
| 49 | + |
| 50 | + if (fsStats && memStats) { |
| 51 | + return fsStats.mtimeMs > memStats.mtimeMs |
| 52 | + ? realFileSystem.readFile(path, encoding) |
| 53 | + : memReadFile(path, encoding); |
| 54 | + } else if (fsStats) { |
| 55 | + return realFileSystem.readFile(path, encoding); |
| 56 | + } else if (memStats) { |
| 57 | + return memReadFile(path, encoding); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + function readDir(path: string) { |
| 62 | + const fsDirents = realFileSystem.readDir(path); |
| 63 | + const memDirents = memReadDir(path); |
| 64 | + |
| 65 | + // merge list of dirents from fs and mem |
| 66 | + return fsDirents |
| 67 | + .filter((fsDirent) => !memDirents.some((memDirent) => memDirent.name === fsDirent.name)) |
| 68 | + .concat(memDirents); |
| 69 | + } |
| 70 | + |
| 71 | + function readStats(path: string) { |
| 72 | + const fsStats = realFileSystem.readStats(path); |
| 73 | + const memStats = memReadStats(path); |
| 74 | + |
| 75 | + if (fsStats && memStats) { |
| 76 | + return fsStats.mtimeMs > memStats.mtimeMs ? fsStats : memStats; |
| 77 | + } else if (fsStats) { |
| 78 | + return fsStats; |
| 79 | + } else if (memStats) { |
| 80 | + return memStats; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + function createDir(path: string) { |
| 85 | + mem.mkdirSync(normalizePath(path), { recursive: true }); |
| 86 | + } |
| 87 | + |
| 88 | + function writeFile(path: string, data: string) { |
| 89 | + if (!memExists(dirname(path))) { |
| 90 | + createDir(dirname(path)); |
| 91 | + } |
| 92 | + |
| 93 | + mem.writeFileSync(normalizePath(path), data); |
| 94 | + } |
| 95 | + |
| 96 | + function deleteFile(path: string) { |
| 97 | + if (memExists(path)) { |
| 98 | + mem.unlinkSync(normalizePath(path)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + function updateTimes(path: string, atime: Date, mtime: Date) { |
| 103 | + if (memExists(path)) { |
| 104 | + mem.utimesSync(normalizePath(path), atime, mtime); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return { |
| 109 | + exists(path: string) { |
| 110 | + return exists(realFileSystem.realPath(path)); |
| 111 | + }, |
| 112 | + readFile(path: string, encoding?: string) { |
| 113 | + return readFile(realFileSystem.realPath(path), encoding); |
| 114 | + }, |
| 115 | + readDir(path: string) { |
| 116 | + return readDir(realFileSystem.realPath(path)); |
| 117 | + }, |
| 118 | + readStats(path: string) { |
| 119 | + return readStats(realFileSystem.realPath(path)); |
| 120 | + }, |
| 121 | + realPath(path: string) { |
| 122 | + return realFileSystem.realPath(path); |
| 123 | + }, |
| 124 | + normalizePath(path: string) { |
| 125 | + return normalizePath(path); |
| 126 | + }, |
| 127 | + writeFile(path: string, data: string) { |
| 128 | + writeFile(realFileSystem.realPath(path), data); |
| 129 | + }, |
| 130 | + deleteFile(path: string) { |
| 131 | + deleteFile(realFileSystem.realPath(path)); |
| 132 | + }, |
| 133 | + createDir(path: string) { |
| 134 | + createDir(realFileSystem.realPath(path)); |
| 135 | + }, |
| 136 | + updateTimes(path: string, atime: Date, mtime: Date) { |
| 137 | + updateTimes(realFileSystem.realPath(path), atime, mtime); |
| 138 | + }, |
| 139 | + clearCache() { |
| 140 | + realFileSystem.clearCache(); |
| 141 | + }, |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +export { createPassiveFileSystem }; |
0 commit comments