forked from GladysAssistant/Gladys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
67 lines (52 loc) · 1.64 KB
/
update.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
var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');
var BACKUP_FOLDERNAME = ".GladysBackUp";
var filesToKeep = [
'config/connections.js',
'config/serialport.js',
'config/machine.js',
'config/googleapi.js',
'config/signup.js'
];
backUpFiles();
syncWithGithub(function(err){
if(err) return console.log(err);
putfilesBack();
});
function cleanBackUpFolder(callback) {
fs.readdir(BACKUP_FOLDERNAME, function(err, files){
if(err) return callback(err);
for(var i = 0;i<files.length;i++){
fs.unlinkSync(path.join(BACKUP_FOLDERNAME, files[i]));
}
callback(null);
});
}
function backUpFiles(){
if (!fs.existsSync(BACKUP_FOLDERNAME)) {
fs.mkdirSync(BACKUP_FOLDERNAME);
}
for(var i = 0; i< filesToKeep.length;i++){
var pathDest = path.join(BACKUP_FOLDERNAME,path.basename(filesToKeep[i]));
fs.createReadStream(filesToKeep[i]).pipe(fs.createWriteStream(pathDest));
console.log('Backed Up file : ' + pathDest );
}
}
function putfilesBack(){
for(var i = 0; i< filesToKeep.length;i++){
var pathOrigin = path.join(BACKUP_FOLDERNAME,path.basename(filesToKeep[i]));
fs.createReadStream(pathOrigin).pipe(fs.createWriteStream(filesToKeep[i]));
console.log('File put back : ' + pathOrigin);
}
}
function syncWithGithub(callback){
exec('git reset --hard HEAD && git pull origin master', function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
callback(error);
});
}