-
Notifications
You must be signed in to change notification settings - Fork 0
/
exports.js
163 lines (142 loc) · 4.24 KB
/
exports.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
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
156
157
158
159
160
161
162
163
var Silk = require('silk-api');
var path = require('path');
var apps = [];
var appsFolder = __dirname.split(path.sep);
appsFolder = appsFolder.slice(0, appsFolder.length - 1);
appsFolder = appsFolder.join(path.sep);
module.exports = {
"apps/remove": function (data) {
send(void(0), "Deleting...");
var file = data.folder;
var folder = appsFolder + path.sep + file;
var deleteFolderRecursive = function (path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
send(void(0), " ");
}
};
deleteFolderRecursive(folder);
},
"list": function () {
var context = this;
context.async = true;
Silk.call('apps/list', {}, function (err, data) {
if (err) {
return context.return(err);
}
context.return(data);
});
},
"apps/install": startInstall
};
function startInstall(data) {
var path = data.url;
// check if path is local or a github repository
var localPath = false;
if (path.indexOf('/') === 0) {
localPath = true;
} else if (path.indexOf('~/') === 0) {
localPath = true;
}
if (localPath === false) {
download(data, call_obj, send);
} else {
Silk.call('apps/external/add', data.url, function (err, result) {
console.log('added external app', err, result);
});
}
}
function download(data, call_ob, send) {
fs.exists(appsFolder + path.sep + data.url.replace("/", "-"), function (exists) {
if (exists === true) {
send(new Error("App already exists"));
return;
}
if (data.url === "") {
send(new Error("Url can not be empty"));
}
var repo = data.url;
var url = "https://api.github.com/repos/" + repo + "/zipball";
var options = {
url: url,
headers: {
'User-Agent': 'silk-gui'
}
};
send(void(0), "Downloading...");
request(options).on('response', function (response) {
}).on('error', function (err) {
send(err)
}).on("end", function () {
send(void(0), "pending");
install(data, call_ob, send)
}).pipe(fs.createWriteStream(__dirname + path.sep + 'test.zip'));
});
}
function install(data, call_ob, send) {
send(void(0), "Installing ...");
var extractTo = appsFolder + path.sep + data.url.replace("/", "-");
try {
yauzl.open(__dirname + path.sep + 'test.zip', function (err, zipfile) {
if (err) {
send(err);
return;
}
zipfile.once('end', function () {
Silk.call('apps/start', data.url.replace('/', '-'), function (err, data) {
console.log(error);
send(void(0), "Finished installing!")
});
send(void(0), 'Starting App');
fs.unlink(__dirname + path.sep + 'test.zip', function (err) {
if (err) {
send(err);
}
console.log('Installed ' + data.url);
send(void(0), " ");
})
});
zipfile.on("entry", function (entry) {
if (/\/$/.test(entry.fileName)) {
// directory file names end with '/'
return;
}
var fileName = entry.fileName;
fileName = fileName.split(path.sep);
fileName = fileName.splice(1, fileName.length)
fileName = fileName.join(path.sep);
entry.fileName = fileName;
var dest = path.join(extractTo, entry.fileName)
var destDir = path.dirname(dest)
// dest = dest.split(path.sep);
// dest = dest.slice(0, dest.length - 1);
zipfile.openReadStream(entry, function (err, readStream) {
if (err) {
send(err);
return;
}
mkdirp(destDir, function (err) {
if (err) {
send(err);
}
//entry.fileName = data.url.replace("/", "-");
// ensure parent directory exists, and then:
readStream.pipe(fs.createWriteStream(dest).on("error", function (err) {
send(err)
}))
});
});
});
});
} catch (e) {
send(e);
}
}