-
Notifications
You must be signed in to change notification settings - Fork 39
/
StorageHelper.ts
99 lines (82 loc) · 2.74 KB
/
StorageHelper.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
IMachine,
IDeployedDirectory,
IOldSavedApp
} from '../models/storage/StoredObjects'
import * as ConfigStore from 'configstore'
import Utils from './Utils'
const CAP_MACHINES = 'CapMachines'
const DEPLOYED_DIRS = 'DeployedDirs'
export default class StorageHelper {
static instance: StorageHelper
static get() {
if (!StorageHelper.instance) {
StorageHelper.instance = new StorageHelper()
}
return StorageHelper.instance
}
private data: ConfigStore
constructor() {
this.data = new ConfigStore('caprover')
}
getMachines(): IMachine[] {
return Utils.copyObject(this.data.get(CAP_MACHINES) || [])
}
findMachine(machineName: string) {
return this.getMachines().find(m => m.name === machineName)
}
removeMachine(machineName: string) {
const machines = this.getMachines()
const removedMachine = machines.filter(
machine => machine.name === machineName
)[0]
const newMachines = machines.filter(
machine => machine.name !== machineName
)
this.data.set(CAP_MACHINES, newMachines)
return removedMachine
}
saveMachine(machineToSaveOrUpdate: IMachine) {
const currMachines = this.getMachines()
let updatedMachine = false
for (let index = 0; index < currMachines.length; index++) {
const element = currMachines[index]
if (element.name === machineToSaveOrUpdate.name) {
updatedMachine = true
currMachines[index] = machineToSaveOrUpdate
break
}
}
if (!updatedMachine) {
currMachines.push(machineToSaveOrUpdate)
}
this.data.set(CAP_MACHINES, currMachines)
}
getDeployedDirectories(): IDeployedDirectory[] {
return Utils.copyObject(this.data.get(DEPLOYED_DIRS) || [])
}
saveDeployedDirectory(directoryToSaveOrUpdate: IDeployedDirectory) {
if (
!directoryToSaveOrUpdate ||
!directoryToSaveOrUpdate.appName ||
!directoryToSaveOrUpdate.cwd ||
!directoryToSaveOrUpdate.machineNameToDeploy
) {
return
}
const currDirs = this.getDeployedDirectories()
let updatedDir = false
for (let index = 0; index < currDirs.length; index++) {
const element = currDirs[index]
if (element.cwd === directoryToSaveOrUpdate.cwd) {
updatedDir = true
currDirs[index] = directoryToSaveOrUpdate
break
}
}
if (!updatedDir) {
currDirs.push(directoryToSaveOrUpdate)
}
this.data.set(DEPLOYED_DIRS, currDirs)
}
}