Completed
Push — master ( a1c62f...1f2ffc )
by Thomas
28s
created

Kit.js ➔ ... ➔ response.pipe.error   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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)
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
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))
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
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