Completed
Push — master ( 8a8e53...266fe4 )
by Thomas
10s
created

module.exports.downloadHelper   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 7
rs 9.4285
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 5 2
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) {
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
    return {
53
      'slug': boxName,
54
      'name': authorName + '/' + boxName,
55
      'title': this.boxNameDisplay(boxName),
56
      'description': 'Includable app for ' + this.boxNameDisplay(boxName) + '.',
57
      'authors': [{
58
        'name': authorNameFull,
59
        'email': authorEmail
60
      }],
61
      'license': 'proprietary',
62
      'controllers': {
63
        'web': 'web/index.php'
64
      }
65
    }
66
  },
67
68
  capitalizeFirstLetter: function (string) {
69
    return string.charAt(0).toUpperCase() + string.slice(1)
70
  },
71
72
  boxNameDisplay: function (boxName) {
73
    return this.capitalizeFirstLetter(boxName.replace(/[-_]/gi, ' '))
74
  },
75
76
  getHomeDir: function () {
77
    var env = process.env
78
79
    if (process.platform === 'win32') {
80
      return this.getWin32HomeDir()
81
    }
82
83
    if ('HOME' in env && env.HOME) {
84
      return env.HOME
85
    }
86
87
    var user = this.getSystemUsername()
88
89
    if (user && process.platform === 'darwin') {
90
      return '/Users/' + user
91
    }
92
93
    if (process.getuid() === 0) {
94
      return '/root'
95
    }
96
97
    if (user) {
98
      return '/home/' + user
99
    }
100
101
    return process.cwd() || __dirname
102
  },
103
104
  getWin32HomeDir: function () {
105
    return process.env.USERPROFILE ||
106
      process.env.HOMEDRIVE + process.env.HOMEPATH ||
107
      process.env.HOME ||
108
      process.cwd()
109
  },
110
111
  getSystemUsername: function () {
112
    return process.env.LOGNAME ||
113
      process.env.USER ||
114
      process.env.LNAME ||
115
      process.env.USERNAME ||
116
      process.env.SUDO_USER ||
117
      process.env.C9_USER ||
118
      false
119
  },
120
121
  getPluginsDirectory: function (pluginsDirectory) {
122
    if (!pluginsDirectory) {
123
      pluginsDirectory = path.join(this.getHomeDir(), '.inc-plugins')
124
    }
125
126
    return pluginsDirectory
127
  },
128
129
  statOrNothing: function (origPath, event, cb) {
130
    if (event !== 'add' && event !== 'change' && event !== 'change') {
131
      cb(null, true)
132
      return
133
    }
134
135
    fs.stat(origPath, function (err) {
136
      cb(null, event === 'unlink' ? !!err : !err)
137
    })
138
  },
139
140
  downloadHelper: function (cb, basepath, force) {
141
    // Determine path
142
    var helperBasePath = path.join('.inc', 'meta')
143
    if (basepath) {
144
      helperBasePath = path.join(basepath, helperBasePath)
145
    }
146
    var helperFilename = path.join(helperBasePath, '.phpstorm.meta.php')
147
148
    // Check last updated timestamp
149
    fs.stat(helperFilename, function (err, info) {
150
      if (!err && !force && info.mtime.getTime() > (new Date()).getTime() - 3600000) {
151
        return
152
      }
153
154
      // Download helper file
155
      unirest.get(global.containerEndpoint + '/ide_helper').end(function (response) {
156
        if (response.body) {
157
          mkdirp(helperBasePath, function () {
158
            fs.writeFile(helperFilename, response.body, 'utf8', function () {
159
              if (cb) {
160
                cb()
161
              }
162
            })
163
          })
164
        }
165
      })
166
    })
167
168
    // Delete old helper file
169
    var oldHelperFilename = '._scho' + 'lica_ide_helper.php'
170
    if (basepath) {
171
      oldHelperFilename = path.join(basepath, oldHelperFilename)
172
    }
173
    try {
174
      fs.unlinkSync(oldHelperFilename)
175
    } catch (e) {}
176
  }
177
178
}
179