forked from stackwiseai/stackwise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.ts
More file actions
69 lines (61 loc) · 1.71 KB
/
registry.ts
File metadata and controls
69 lines (61 loc) · 1.71 KB
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
const fs = require('fs');
const path = require('path');
export default class stackRegistry {
static filePath = path.join(__dirname, '../stackRegistry.json');
static register(methodName, functionId) {
const registry = this.loadRegistry();
registry[methodName] = functionId;
console.log('registry');
console.log(registry);
this.saveRegistry(registry);
}
static exists(functionId): boolean {
const registry = this.loadRegistry();
for (const key in registry) {
if (registry[key] === functionId) {
return true;
}
}
return false;
}
static nameExists(methodName): boolean {
const registry = this.loadRegistry();
if (registry[methodName]) {
return true;
}
return false;
}
static loadRegistry() {
if (!fs.existsSync(this.filePath)) {
return {};
}
const fileContent = fs.readFileSync(this.filePath, 'utf8');
try {
return JSON.parse(fileContent);
} catch (error) {
console.error('Error reading registry file:', error);
return {};
}
}
static saveRegistry(registry) {
try {
const data = JSON.stringify(registry, null, 2);
fs.writeFileSync(this.filePath, data, 'utf8');
} catch (error) {
console.error('Error writing to registry file:', error);
}
}
static update(oldName, newName) {
console.log(`updating ${oldName} to ${newName} in registry`);
const registry = this.loadRegistry();
const functionId = registry[oldName];
delete registry[oldName];
registry[newName] = functionId;
this.saveRegistry(registry);
}
static remove(methodName) {
const registry = this.loadRegistry();
delete registry[methodName];
this.saveRegistry(registry);
}
}