lib/util.js   B
last analyzed

Complexity

Total Complexity 49
Complexity/F 3.27

Size

Lines of Code 185
Function Count 15

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 0
wmc 49
c 6
b 0
f 0
nc 64512
mnd 3
bc 39
fnc 15
dl 0
loc 185
rs 8.5454
bpm 2.6
cpm 3.2666
noi 0

How to fix   Complexity   

Complexity

Complex classes like lib/util.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict'
2
3
const unirest = require('unirest')
4
const path = require('path')
5
const fs = require('fs')
6
const pkg = require('../package.json')
7
const mkdirp = require('mkdirp')
8
const output = require('./output')
9
10
module.exports = {
11
  output: output,
12
  request: function (method, fields, headers) {
13
    var req = unirest.post(global.containerEndpoint + method)
14
      .header('Accept', 'application/json')
15
      .header('User-Agent', 'IncludableCLI ' + pkg.name + '/' + pkg.version)
16
      .header('X-Client', pkg.name + '/' + pkg.version)
17
      .header('X-Client-Version', pkg.version)
18
      .header('X-API-Version', global.apiVersion)
19
      .header('X-API-Stability', global.apiStability)
20
21
    if (fields) {
22
      for (var x in fields) {
23
        if (!fields.hasOwnProperty(x)) {
24
          continue
25
        }
26
        req.field(x, fields[x])
27
      }
28
    }
29
30
    if (headers) {
31
      for (var y in headers) {
32
        if (!headers.hasOwnProperty(y)) {
33
          continue
34
        }
35
        req.header(y, headers[y])
36
      }
37
    }
38
39
    return req
40
  },
41
42
  moduleJSON: function (boxName, authorName, authorNameFull, authorEmail, baseJSON) {
43
    if (!authorName || authorName === '') {
44
      authorName = '*author*'
45
    }
46
    if (!authorNameFull || authorNameFull === '') {
47
      authorNameFull = authorName
48
    }
49
    if (!authorEmail || authorEmail === '') {
50
      authorEmail = '[email protected]'
51
    }
52
53
    if (!baseJSON) {
54
      baseJSON = {
55
        'license': 'proprietary',
56
        'controllers': {
57
          'web': 'web/index.php'
58
        }
59
      }
60
    }
61
62
    return Object.assign(baseJSON, {
63
      'slug': boxName,
64
      'name': authorName + '/' + boxName,
65
      'title': this.boxNameDisplay(boxName),
66
      'description': 'Includable app for ' + this.boxNameDisplay(boxName) + '.',
67
      'authors': [{
68
        'name': authorNameFull,
69
        'email': authorEmail
70
      }]
71
    })
72
  },
73
74
  capitalizeFirstLetter: function (string) {
75
    return string.charAt(0).toUpperCase() + string.slice(1)
76
  },
77
78
  boxNameDisplay: function (boxName) {
79
    return this.capitalizeFirstLetter(boxName.replace(/[-_]/gi, ' '))
80
  },
81
82
  getHomeDir: function () {
83
    var env = process.env
84
85
    if (process.platform === 'win32') {
86
      return this.getWin32HomeDir()
87
    }
88
89
    if ('HOME' in env && env.HOME) {
90
      return env.HOME
91
    }
92
93
    var user = this.getSystemUsername()
94
95
    if (user && process.platform === 'darwin') {
96
      return '/Users/' + user
97
    }
98
99
    if (process.getuid() === 0) {
100
      return '/root'
101
    }
102
103
    if (user) {
104
      return '/home/' + user
105
    }
106
107
    return process.cwd() || __dirname
108
  },
109
110
  getWin32HomeDir: function () {
111
    return process.env.USERPROFILE ||
112
      process.env.HOMEDRIVE + process.env.HOMEPATH ||
113
      process.env.HOME ||
114
      process.cwd()
115
  },
116
117
  getSystemUsername: function () {
118
    return process.env.LOGNAME ||
119
      process.env.USER ||
120
      process.env.LNAME ||
121
      process.env.USERNAME ||
122
      process.env.SUDO_USER ||
123
      process.env.C9_USER ||
124
      false
125
  },
126
127
  getPluginsDirectory: function (pluginsDirectory) {
128
    if (!pluginsDirectory) {
129
      pluginsDirectory = path.join(this.getHomeDir(), '.inc-plugins')
130
    }
131
132
    return pluginsDirectory
133
  },
134
135
  statOrNothing: function (origPath, event, cb) {
136
    if (event !== 'add' && event !== 'change' && event !== 'change') {
137
      cb(null, true)
138
      return
139
    }
140
141
    fs.stat(origPath, function (err) {
142
      cb(null, event === 'unlink' ? !!err : !err)
143
    })
144
  },
145
146
  downloadHelper: function (cb, basepath, force) {
147
    // Determine path
148
    var helperBasePath = path.join('.inc', 'meta')
149
    if (basepath) {
150
      helperBasePath = path.join(basepath, helperBasePath)
151
    }
152
    var helperFilename = path.join(helperBasePath, '.phpstorm.meta.php')
153
154
    // Check last updated timestamp
155
    fs.stat(helperFilename, function (err, info) {
156
      if (!err && !force && info.mtime.getTime() > (new Date()).getTime() - 3600000) {
157
        cb ? cb() : null
158
        return
159
      }
160
161
      // Download helper file
162
      unirest.get(global.containerEndpoint + '/ide_helper').end(function (response) {
163
        if (response.body) {
164
          mkdirp(helperBasePath, function () {
165
            fs.writeFile(helperFilename, response.body, 'utf8', function () {
166
              if (cb) {
167
                cb()
168
              }
169
            })
170
          })
171
        }
172
      })
173
    })
174
175
    // Delete old helper file
176
    var oldHelperFilename = '._scho' + 'lica_ide_helper.php'
177
    if (basepath) {
178
      oldHelperFilename = path.join(basepath, oldHelperFilename)
179
    }
180
    try {
181
      fs.unlinkSync(oldHelperFilename)
182
    } catch (e) {}
183
  }
184
185
}
186