1
|
|
|
'use strict' |
2
|
|
|
|
3
|
|
|
const fs = require('fs') |
4
|
|
|
const os = require('os') |
5
|
|
|
const path = require('path') |
6
|
|
|
const https = require('https') |
7
|
|
|
const unzip = require('unzip-stream') |
8
|
|
|
|
9
|
|
|
var Kit = {} |
10
|
|
|
|
11
|
|
|
function extractZip (response, zipName, containerPath, callback) { |
12
|
|
|
var tmpdir = os.tmpdir() |
13
|
|
|
return response |
14
|
|
|
.pipe(unzip.Extract({ |
15
|
|
|
path: tmpdir |
16
|
|
|
})) |
17
|
|
|
.on('error', function (err) { |
18
|
|
|
callback(err) |
19
|
|
|
}) |
20
|
|
|
.on('close', function () { |
21
|
|
|
setTimeout(function () { |
22
|
|
|
try { |
23
|
|
|
// At this point, there is a good possibility that all files got |
24
|
|
|
// extracted to a subdirectory, which we should correct |
25
|
|
|
var zipPath = path.join(tmpdir, zipName) |
26
|
|
|
if (fs.existsSync(zipPath)) { |
27
|
|
|
fs.renameSync(zipPath, path.resolve(containerPath)) |
28
|
|
|
} else { |
29
|
|
|
callback(new Error('Error extracting zip file')) |
30
|
|
|
} |
31
|
|
|
// Calling back home |
32
|
|
|
callback(null) |
|
|
|
|
33
|
|
|
} catch (e) { |
34
|
|
|
return callback(e) |
35
|
|
|
} |
36
|
|
|
}, 100) |
37
|
|
|
}) |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function downloadZip (zipUrl, zipName, containerPath, callback) { |
41
|
|
|
https.get(zipUrl, function (response) { |
42
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) { |
43
|
|
|
return extractZip(response, zipName, containerPath, callback) |
44
|
|
|
} |
45
|
|
|
if (response.headers.location) { |
46
|
|
|
return downloadZip(response.headers.location, zipName, containerPath, callback) |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
callback(new Error(response.statusCode + ' ' + response.statusMessage)) |
|
|
|
|
50
|
|
|
}).on('error', function (err) { |
51
|
|
|
callback(err) |
52
|
|
|
}) |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
Kit.download = function (kitName, targetPath, callback) { |
56
|
|
|
var containerPath = path.resolve(targetPath) |
57
|
|
|
var zipUrl = 'https://github.com/includable-modules/starter-' + kitName + '/archive/master.zip' |
58
|
|
|
|
59
|
|
|
downloadZip(zipUrl, 'starter-' + kitName + '-master', containerPath, callback) |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
module.exports = Kit |
63
|
|
|
|