This repository has been archived by the owner on May 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
update-addon.js
82 lines (64 loc) · 2.12 KB
/
update-addon.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
var request = require("request"),
logger = require(__dirname+'/logger.js'),
fs = require("fs"),
AdmZip = require("adm-zip"),
ncp = require('ncp').ncp;
ncp.limit = 16;
logger.info("update-addon started");
var arguments = process.argv.slice(2);
var url = arguments[0];
var name = arguments[1];
var urlParts = url.split("/");
var nameArr ="";
if (url.split("?")[1] == undefined) {
nameArr = urlParts.splice(-3)[0].toString();
} else {
nameArr = url.split("?")[1].toString();
}
var tmpFile = __dirname + "/tmp/" + nameArr + "master.zip";
var tmpDir = nameArr + "-master";
logger.info("update-addon download and unzip "+url);
// Download and Unzip
request(url).pipe(fs.createWriteStream(tmpFile)).on("close", function () {
var zip = new AdmZip(tmpFile);
zip.extractAllTo(__dirname+"/tmp", true);
logger.info("update-addon unzip done");
var sourcedir = __dirname+"/tmp/"+tmpDir,
destination = __dirname+"/www/"+name;
var source = sourcedir;
try {
var stats = fs.lstatSync(sourcedir+"/"+name);
if (stats.isDirectory()) {
source = sourcedir+"/"+name
}
}
catch (e) {
}
logger.info("update-addon copying "+source+" to "+destination);
ncp(source, destination, function (err) {
if (err) {
logger.error(err);
return
}
setTimeout(function () {
// Ordner im tmp Verzeichnis löschen
logger.info('update-addon delete tmp folder '+__dirname+"/tmp/"+tmpDir);
deleteFolderRecursive(__dirname+"/tmp/"+tmpDir);
logger.info('update-addon done');
process.exit(0);
}, 2000);
});
});
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};