Skip to content

Commit

Permalink
rm fs cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe committed Sep 20, 2023
1 parent 067cc14 commit 6d3b376
Showing 1 changed file with 9 additions and 50 deletions.
59 changes: 9 additions & 50 deletions src/utils/pluginEnvApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class PluginEnvApi {
public readonly distRoot: string;

Check failure on line 30 in src/utils/pluginEnvApi.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Public accessibility modifier on class property distRoot
public readonly cacheFile: CacheFile | undefined;

Check failure on line 31 in src/utils/pluginEnvApi.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Public accessibility modifier on class property cacheFile

private readonly fileExistsCache = new Map<string, boolean>();
private readonly readFileCache = new Map<string, string | Uint8Array>();
private readonly mkdirCache = new Set();

private readonly asyncActionQueue: AsyncAction[] = [];

constructor({root, distRoot, cacheFile}: PluginEnvApiProps) {
Expand All @@ -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});
Expand All @@ -63,11 +59,11 @@ class PluginEnvApi {
this.asyncActionQueue.push({type: AsyncActionType.Copy, from, to});
}

readFile<T extends BufferEncoding>(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});
}
Expand All @@ -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});
}
Expand All @@ -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);
Expand Down Expand Up @@ -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});
Expand All @@ -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);
Expand All @@ -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});
Expand All @@ -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);
Expand All @@ -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;

0 comments on commit 6d3b376

Please sign in to comment.