From 6d3b37654da727bb0c0d82bfb2f59843dedfec5b Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Wed, 20 Sep 2023 12:53:54 +0500 Subject: [PATCH] rm fs cache --- src/utils/pluginEnvApi.ts | 59 ++++++--------------------------------- 1 file changed, 9 insertions(+), 50 deletions(-) diff --git a/src/utils/pluginEnvApi.ts b/src/utils/pluginEnvApi.ts index 692e32dab..5551fdee2 100644 --- a/src/utils/pluginEnvApi.ts +++ b/src/utils/pluginEnvApi.ts @@ -30,10 +30,6 @@ class PluginEnvApi { public readonly distRoot: string; public readonly cacheFile: CacheFile | undefined; - private readonly fileExistsCache = new Map(); - private readonly readFileCache = new Map(); - private readonly mkdirCache = new Set(); - private readonly asyncActionQueue: AsyncAction[] = []; constructor({root, distRoot, cacheFile}: PluginEnvApiProps) { @@ -49,7 +45,7 @@ class PluginEnvApi { const fullFrom = path.join(this.root, from); const fullTo = path.join(this.distRoot, to); - this.cacheMkdir(path.dirname(fullTo)); + fs.mkdirSync(path.dirname(fullTo), {recursive: true}); fs.copyFileSync(fullFrom, fullTo); if (this.cacheFile) { this.cacheFile.addCopyFile({from, to}); @@ -63,11 +59,11 @@ class PluginEnvApi { this.asyncActionQueue.push({type: AsyncActionType.Copy, from, to}); } - readFile(rawTarget: string, encoding: T | null = null) { + readFile(rawTarget: string, encoding: BufferEncoding | null): Uint8Array | string { const target = safeRelativePath(rawTarget); const fullTarget = path.join(this.root, target); - const result = this.cacheReadFileSync(fullTarget, encoding); + const result = fs.readFileSync(fullTarget, encoding); if (this.cacheFile) { this.cacheFile.addFileDep({filename: target, content: result}); } @@ -78,7 +74,7 @@ class PluginEnvApi { const target = safeRelativePath(rawTarget); const fullTarget = path.join(this.root, target); - const result = this.cacheExistsSync(fullTarget); + const result = fs.existsSync(fullTarget); if (this.cacheFile) { this.cacheFile.addFileExists({filename: target, state: result}); } @@ -89,7 +85,7 @@ class PluginEnvApi { const to = safeRelativePath(rawTo); const fullTo = path.join(this.distRoot, to); - this.cacheMkdir(path.dirname(fullTo)); + fs.mkdirSync(path.dirname(fullTo), {recursive: true}); fs.writeFileSync(fullTo, data); if (this.cacheFile) { this.cacheFile.addWriteFile(to, data); @@ -122,7 +118,7 @@ class PluginEnvApi { const fullFrom = path.join(this.root, from); const fullTo = path.join(this.distRoot, to); - this.cacheMkdir(path.dirname(fullTo)); + fs.mkdirSync(path.dirname(fullTo), {recursive: true}); fs.copyFileSync(fullFrom, fullTo); if (this.cacheFile) { this.cacheFile.addCopyFile({from, to}); @@ -133,7 +129,7 @@ class PluginEnvApi { const {to, data} = action; const fullTo = path.join(this.distRoot, to); - this.cacheMkdir(path.dirname(fullTo)); + fs.mkdirSync(path.dirname(fullTo), {recursive: true}); fs.writeFileSync(fullTo, data); if (this.cacheFile) { this.cacheFile.addWriteFile(to, data); @@ -154,7 +150,7 @@ class PluginEnvApi { const fullFrom = path.join(this.root, from); const fullTo = path.join(this.distRoot, to); - await this.cacheMkdirAsync(path.dirname(fullTo)); + await fs.promises.mkdir(path.dirname(fullTo), {recursive: true}); await fs.promises.copyFile(fullFrom, fullTo); if (this.cacheFile) { this.cacheFile.addCopyFile({from, to}); @@ -165,7 +161,7 @@ class PluginEnvApi { const {to, data} = action; const fullTo = path.join(this.distRoot, to); - await this.cacheMkdirAsync(path.dirname(fullTo)); + await fs.promises.mkdir(path.dirname(fullTo), {recursive: true}); await fs.promises.writeFile(fullTo, data); if (this.cacheFile) { this.cacheFile.addWriteFile(to, data); @@ -175,43 +171,6 @@ class PluginEnvApi { } })); } - - private cacheMkdir(path: string) { - const {mkdirCache} = this; - if (!mkdirCache.has(path)) { - fs.mkdirSync(path, {recursive: true}); - mkdirCache.add(path); - } - } - - private async cacheMkdirAsync(path: string) { - const {mkdirCache} = this; - if (!mkdirCache.has(path)) { - await fs.promises.mkdir(path, {recursive: true}); - mkdirCache.add(path); - } - } - - private cacheReadFileSync(filename: string, encoding: BufferEncoding | null = null) { - const {readFileCache} = this; - const key = `${filename}_${encoding}`; - let result = readFileCache.get(key); - if (result === undefined) { - result = fs.readFileSync(filename, encoding); - readFileCache.set(key, result); - } - return result; - } - - private cacheExistsSync(filename: string) { - const {fileExistsCache} = this; - let result = fileExistsCache.get(filename); - if (result === undefined) { - result = fs.existsSync(filename); - fileExistsCache.set(filename, result); - } - return result; - } } export default PluginEnvApi;