-
Notifications
You must be signed in to change notification settings - Fork 5
/
storageService.ts
55 lines (42 loc) · 1.66 KB
/
storageService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import fs from 'fs-extra';
import path from 'path';
export class StorageService {
constructor(public baseDirectory: string) {}
public initSync(): void {
return fs.ensureDirSync(this.baseDirectory);
}
public getItemSync<T>(itemName: string): T | null {
const filePath = path.resolve(this.baseDirectory, itemName);
if (!fs.pathExistsSync(filePath)) {
return null;
}
return fs.readJsonSync(filePath);
}
public async getItem<T>(itemName: string): Promise<T | null> {
const filePath = path.resolve(this.baseDirectory, itemName);
if (!(await fs.pathExists(filePath))) {
return null;
}
return await fs.readJson(filePath);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setItemSync(itemName: string, data: Record<any, any> | Array<any>): void {
return fs.writeJsonSync(path.resolve(this.baseDirectory, itemName), data);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setItem(itemName: string, data: Record<any, any> | Array<any>): Promise<void> {
return fs.writeJson(path.resolve(this.baseDirectory, itemName), data);
}
public copyItem(srcItemName: string, destItemName: string): Promise<void> {
return fs.copyFile(path.resolve(this.baseDirectory, srcItemName), path.resolve(this.baseDirectory, destItemName));
}
public copyItemSync(srcItemName: string, destItemName: string): void {
return fs.copyFileSync(
path.resolve(this.baseDirectory, srcItemName),
path.resolve(this.baseDirectory, destItemName),
);
}
public removeItemSync(itemName: string): void {
return fs.removeSync(path.resolve(this.baseDirectory, itemName));
}
}