-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuniversal-status.ts
75 lines (66 loc) · 1.87 KB
/
universal-status.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { getModelStatusPath } from 'codefox-common';
import * as fs from 'fs';
import * as path from 'path';
export interface UniversalStatus {
isDownloaded: boolean;
lastChecked: Date;
}
export class UniversalStatusManager {
private static instance: UniversalStatusManager;
private status: Record<string, UniversalStatus>;
private readonly statusPath: string;
private constructor() {
this.statusPath = getModelStatusPath();
this.loadStatus();
}
public static getInstance(): UniversalStatusManager {
if (!UniversalStatusManager.instance) {
UniversalStatusManager.instance = new UniversalStatusManager();
}
return UniversalStatusManager.instance;
}
private loadStatus() {
try {
const file = fs.readFileSync(this.statusPath, 'utf-8');
const data = JSON.parse(file);
this.status = Object.entries(data).reduce(
(acc, [key, value]: [string, any]) => {
acc[key] = {
...value,
lastChecked: value.lastChecked
? new Date(value.lastChecked)
: new Date(),
};
return acc;
},
{} as Record<string, UniversalStatus>,
);
} catch (error) {
this.status = {};
}
}
private saveStatus() {
const statusDir = path.dirname(this.statusPath);
if (!fs.existsSync(statusDir)) {
fs.mkdirSync(statusDir, { recursive: true });
}
fs.writeFileSync(
this.statusPath,
JSON.stringify(this.status, null, 2),
'utf-8',
);
}
updateStatus(UniversalName: string, isDownloaded: boolean) {
this.status[UniversalName] = {
isDownloaded,
lastChecked: new Date(),
};
this.saveStatus();
}
getStatus(UniversalName: string): UniversalStatus | undefined {
return this.status[UniversalName];
}
getAllStatus(): Record<string, UniversalStatus> {
return { ...this.status };
}
}