-
Notifications
You must be signed in to change notification settings - Fork 0
/
restoreBackup.js
65 lines (55 loc) · 2.01 KB
/
restoreBackup.js
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
const fs = require("fs-extra");
const readline = require("readline");
const log = require('./logger/log.js');
let versionBackup;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function recursiveReadDirAndBackup(pathFileOrFolder) {
const pathFileOrFolderBackup = `${process.cwd()}/${versionBackup}/${pathFileOrFolder}`;
const pathFileOrFolderRestore = `${process.cwd()}/${pathFileOrFolder}`;
if (fs.lstatSync(pathFileOrFolderBackup).isDirectory()) {
if (!fs.existsSync(pathFileOrFolderRestore))
fs.mkdirSync(pathFileOrFolderRestore);
const readDir = fs.readdirSync(pathFileOrFolderBackup);
readDir.forEach(fileOrFolder => {
recursiveReadDirAndBackup(`${pathFileOrFolder}/${fileOrFolder}`);
});
}
else {
pathFileOrFolder = pathFileOrFolder
.replace(/\\/g, '/');
fs.copyFileSync(pathFileOrFolderBackup, pathFileOrFolderRestore);
}
}
(async () => {
if (process.argv.length < 3) {
versionBackup = await new Promise((resolve) => {
rl.question("Input version backup: ", answer => {
resolve(answer);
});
});
}
else {
versionBackup = process.argv[2];
}
if (!versionBackup) {
log.error("ERROR", `Please input version backup`);
process.exit();
}
versionBackup = versionBackup.replace("backup_", ""); // remove backup_ if exists (may be user input backup_1.0.0)
versionBackup = `backup_${versionBackup}`;
const backupFolder = `${process.cwd()}/backups/${versionBackup}`;
if (!fs.existsSync(backupFolder)) {
log.error("ERROR", `Backup folder is not exists (${backupFolder})`);
process.exit();
}
const files = fs.readdirSync(backupFolder);
for (const file of files)
recursiveReadDirAndBackup(file);
const packageJson = require(`${process.cwd()}/package.json`);
packageJson.version = versionBackup.replace("backup_", "");
fs.writeFileSync(`${process.cwd()}/package.json`, JSON.stringify(packageJson, null, 2));
log.info("SUCCESS", `Restore backup ${versionBackup} success`);
})();