-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-backup-service.ts
155 lines (124 loc) · 3.92 KB
/
project-backup-service.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { IBackup, IProjectBackupService } from "../definitions/project";
import { IFileSystem, IProjectHelper } from "../common/declarations";
import { injector } from "../common/yok";
import * as path from "path";
export class ProjectBackupService implements IProjectBackupService {
constructor(
protected $fs: IFileSystem,
protected $logger: ILogger,
protected $projectHelper: IProjectHelper
) {}
getBackup(backupName: string): IBackup {
return new ProjectBackupService.Backup(this, backupName);
}
backup(name: string, pathsToBackup: string[]): IBackup {
const backup = new ProjectBackupService.Backup(this, name, pathsToBackup);
return backup.create();
}
restore(name: string): IBackup {
const backup = new ProjectBackupService.Backup(this, name);
return backup.restore();
}
static Backup = class Backup implements IBackup {
constructor(
private $super: ProjectBackupService,
private name: string,
private pathsToBackup: string[] = [],
private basePath: string = $super.$projectHelper.projectDir
) {}
get backupDir() {
return path.resolve(this.basePath, `.${this.name}_backup`);
}
create() {
const backupData = this.getBackupData();
const backedUpPaths = backupData?.paths || [];
this.$super.$logger.trace("creating backup: ", this.name);
this.$super.$fs.createDirectory(this.backupDir);
for (const pathToBackup of this.pathsToBackup) {
const sourcePath = path.resolve(this.basePath, pathToBackup);
const targetPath = path.resolve(this.backupDir, pathToBackup);
if (this.$super.$fs.exists(sourcePath)) {
this.$super.$logger.trace(
`BACKING UP ${sourcePath.cyan} -> ${targetPath.green}`
);
this.$super.$fs.copyFile(sourcePath, targetPath);
backedUpPaths.push(pathToBackup);
}
}
// create backup.json
this.$super.$fs.writeJson(path.resolve(this.backupDir, "_backup.json"), {
name: this.name,
paths: backedUpPaths,
});
return this;
}
restore() {
const backupData = this.getBackupData();
if (!backupData) {
return this;
}
for (const pathToBackup of backupData.paths) {
const sourcePath = path.resolve(this.backupDir, pathToBackup);
const targetPath = path.resolve(this.basePath, pathToBackup);
this.$super.$logger.trace(
`RESTORING ${sourcePath.green} -> ${targetPath.cyan}`
);
if (this.$super.$fs.exists(sourcePath)) {
this.$super.$fs.copyFile(sourcePath, targetPath);
}
}
this.$super.$logger.trace(backupData);
// restore files
return this;
}
isUpToDate() {
const backupData = this.getBackupData();
if (!backupData) {
return false;
}
for (const pathToBackup of backupData.paths) {
const sourcePath = path.resolve(this.backupDir, pathToBackup);
// if any of the files don't exist the backup is not up-to-date
if (!this.$super.$fs.exists(sourcePath)) {
return false;
}
}
return true;
}
remove() {
if (!this.$super.$fs.exists(this.backupDir)) {
this.$super.$logger.trace(
`No backup named ${this.name} could be found.`
);
return;
}
this.$super.$fs.deleteDirectory(this.backupDir);
return this;
}
addPath(backupPath: string) {
this.pathsToBackup.push(path.relative(this.basePath, backupPath));
return this;
}
addPaths(backupPaths: string[]) {
backupPaths.forEach(this.addPath.bind(this));
return this;
}
private getBackupData(): { name: string; paths: string[] } {
if (!this.$super.$fs.exists(this.backupDir)) {
this.$super.$logger.trace(
`No backup named ${this.name} could be found.`
);
return;
}
const backupJSONPath = path.resolve(this.backupDir, "_backup.json");
if (!this.$super.$fs.exists(backupJSONPath)) {
this.$super.$logger.trace(
`The backup ${this.name} does not contain a _backup.json.`
);
return;
}
return this.$super.$fs.readJson(backupJSONPath);
}
};
}
injector.register("projectBackupService", ProjectBackupService);