forked from timelapseplus/VIEW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd_card_update.js
128 lines (118 loc) · 3.49 KB
/
sd_card_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
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
var fs = require('fs');
var child_process = require('child_process');
var spawn = child_process.spawn;
var exec = child_process.exec;
var baseInstallPath = "/home/view/";
var current = fs.readlinkSync(baseInstallPath + 'current');
current = current.match(/[^\/]+$/)[0];
function doInstall() {
var sdContents = fs.readdirSync("/media/");
console.log('sdContents', sdContents);
var versions = sdContents.filter(function(item){
return item.match(/^VIEW-[0-9]+\.[0-9]+(-beta)?[0-9.]*(-beta)?\.zip$/);
});
console.log('versions', versions);
if(versions.length > 0) {
versions = versions.map(function(item){
return item.replace('VIEW-', 'v');
});
versions = sortInstalls(versions, true);
var installVersion = versions[0];
var installZip = '/media/' + installVersion.replace('v', 'VIEW-');
installVersion = installVersion.replace('.zip', '').trim();
console.log("installing", installZip);
extract(installZip, baseInstallPath + installVersion, function(err, destFolder) {
if(err) {
console.log("extract failed");
process.exit(1);
} else {
fs.writeFile(baseInstallPath + installVersion + "/version.json", JSON.stringify({version: installVersion}), function(){
setVersion(installVersion, function(err){
console.log("installation complete!");
process.exit(0);
});
});
}
});
}
}
doInstall();
function setVersion(versionName, callback) {
var installs = fs.readdirSync(baseInstallPath);
if(installs.indexOf('current') !== -1) {
fs.unlinkSync(baseInstallPath + 'current');
}
fs.symlink(baseInstallPath + versionName, baseInstallPath + 'current', function(err) {
callback && callback(err);
});
}
function extract(zipPath, destFolder, callback) {
exec("test -e " + destFolder + '_zip_extract' + " && rm -rf " + destFolder + '_zip_extract', function(err){
var unzip = spawn('unzip', ['-q', zipPath, '-d', destFolder + '_zip_extract']);
console.log("extracting...");
unzip.once('close', function(code) {
fs.readdir(destFolder + '_zip_extract', function(err, files) {
console.log(err, files);
if(files && files.length > 0) {
exec("test -e " + destFolder + " && rm -rf " + destFolder, function(err2){
fs.rename(destFolder + '_zip_extract' + '/' + files[0], destFolder, function(){
fs.rmdir(destFolder + '_zip_extract');
console.log("done extracting");
callback(null, destFolder);
});
});
} else {
callback(err, destFolder);
}
});
});
});
}
function versionParse(item) {
var numbers = item.split(/[\-.]/);
if(numbers.length >= 3) {
var n1 = 0, n2 = 0, n3 = 0, n4 = 0;
n1 = parseInt(numbers[0].substr(1));
n2 = parseInt(numbers[1]);
bMatches = numbers[2].match(/(beta)?([0-9]+)/);
if(bMatches[1]) {
n3 = 0;
if(bMatches[2]) {
n4 = parseInt(bMatches[2]);
}
} else {
n3 = parseInt(bMatches[2]);
if(numbers[3]) {
bMatches = numbers[3].match(/(beta)?([0-9]+)/);
if(bMatches[1]) {
if(bMatches[2]) {
n4 = parseInt(bMatches[2]);
}
}
} else {
n4 = 0;
}
}
//console.log("item = ", n1, n2, n3, n4);
return [n1, n2, n3, n4];
} else {
return null;
}
}
function sortInstalls(list, reverse){
return list.sort(function(a, b) {
var a = versionParse(a);
var b = versionParse(b);
if(a && b) {
for(var i = 0; i < 4; i++) {
if(a[i] > b[i]) return reverse ? -1 : 1;
if(a[i] < b[i]) return reverse ? 1 : -1;
}
return 0;
} else {
if(a && !b) return reverse ? -1 : 1;
if(!a && b) return reverse ? 1 : -1;
return 0;
}
});
}