1 | var os = require('os');
|
2 | var fs = require('fs');
|
3 | var path = require('path');
|
4 | var request = require('request');
|
5 | var async = require('async');
|
6 | var unzip = require('unzip2');
|
7 | var ncp = require('ncp').ncp;
|
8 | var debug = require('debug')('4front:cli:template');
|
9 |
|
10 | module.exports.extract = function(url, destDir, callback) {
|
11 | var tempDir = path.join(os.tmpdir(), new Date().getTime().toString());
|
12 | debug("downloading zip to %s", tempDir);
|
13 |
|
14 | var sourceDir;
|
15 | async.series([
|
16 | function(cb) {
|
17 |
|
18 | fs.mkdir(tempDir, cb);
|
19 | },
|
20 | function(cb) {
|
21 |
|
22 | request.get({url: url, strictSSL: false})
|
23 | .pipe(unzip.Extract({path: tempDir}))
|
24 | .on('error', function(err) {
|
25 | return cb(err);
|
26 | })
|
27 |
|
28 | .on('close', cb);
|
29 | },
|
30 | function(cb) {
|
31 |
|
32 |
|
33 | fs.readdir(tempDir, function(err, files) {
|
34 | if (files.length === 1) {
|
35 |
|
36 |
|
37 | fs.stat(path.join(tempDir, files[0]), function(err, stats) {
|
38 | if (err) return cb(err);
|
39 |
|
40 | if (stats.isDirectory() === true)
|
41 | sourceDir = path.join(tempDir, files[0]);
|
42 |
|
43 | return cb();
|
44 | });
|
45 | }
|
46 | else
|
47 | cb();
|
48 | });
|
49 | },
|
50 | function(cb) {
|
51 | if (!sourceDir)
|
52 | sourceDir = tempDir;
|
53 |
|
54 | debug("copying from %s to %s", sourceDir, destDir);
|
55 |
|
56 | ncp(sourceDir, destDir, cb);
|
57 | }
|
58 | ], callback);
|
59 | };
|