Completed
Pull Request — master (#2)
by Thomas
25s
created

Container.getVersion   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 4
dl 0
loc 10
rs 9.4285
nop 0
1
'use strict'
2
3
const output = require('./output')
4
const jsonfile = require('jsonfile')
5
const mkdirp = require('mkdirp')
6
const path = require('path')
7
const fs = require('fs')
8
9
var Container = {}
10
11
Container.getVersion = function () {
12
  if (!fs.existsSync(path.join('.inc', 'version'))) {
13
    if (fs.existsSync('.scho' + 'lica')) {
14
      return 10
15
    }
16
    return false
17
  }
18
19
  return parseInt(fs.readFileSync(path.join('.inc', 'version'), 'utf8')) || false
20
}
21
22
Container.clean = function () {
23
  try {
24
    fs.unlinkSync(path.join('.inc', 'containers', 'default.json'))
25
  } catch (e) {}
1 ignored issue
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
26
}
27
28
Container.performUpgrade = function () {
29
  var version = this.getVersion()
30
  if (!version) {
31
    return
32
  }
33
34
  if (version > global.containerVersion) {
35
    output.warn('Warning: project metadata is created using a newer version of the CLI, please upgrade')
36
    return
37
  }
38
39
  if (version < global.containerVersion) {
40
    var upgradeFile = path.join(global.base, 'lib', 'upgrades', version + '-' + global.containerVersion + '.js')
41
    try {
42
      if (!fs.existsSync(upgradeFile)) {
43
        throw new Error('no upgrade path from version ' + version + ' to ' + global.containerVersion)
44
      }
45
      require(upgradeFile)()
46
      output.warn('Upgraded project metadata from ' + version + ' to ' + global.containerVersion)
47
    } catch (e) {
48
      output.warn('Error upgrading project: ' + e.message)
49
    }
50
  }
51
}
52
53
Container.validateContainerContent = function (config) {
54
  if (!('containerToken' in config) && 'container_token' in config) {
55
    config.containerToken = config.container_token
56
  }
57
58
  if ('containerToken' in config) {
59
    Container.applyConfig(config)
60
    return true
61
  }
62
63
  return false
64
}
65
66
Container.applyConfig = function (config) {
67
  if ('containerEndpoint' in config) {
68
    global.containerEndpoint = config.containerEndpoint
69
  }
70
  if ('version' in config) {
71
    global.apiVersion = config.version
72
  }
73
  if ('stability' in config) {
74
    global.apiStability = config.stability
75
  }
76
  if ('containerEndpointCheckSSL' in config && !config.containerEndpointCheckSSL) {
77
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
78
  }
79
}
80
81
Container.pathForConfig = function (name) {
82
  return path.join('.inc', 'containers', name + '.json')
83
}
84
85
Container.getConfig = function (name, cb) {
86
  jsonfile.readFile(Container.pathForConfig(name), function (err, config) {
87
    // Use existing container
88
    if (!err && typeof config === 'object' && Container.validateContainerContent(config)) {
89
      return cb(null, config)
90
    }
91
92
    // Try default file if in `beta` mode
93
    if (name === 'beta') {
94
      Container.getConfig('default', cb)
95
    }
96
97
    cb(err || new Error('Container config not found or invalid'), 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...
98
  })
99
}
100
101
Container.writeConfig = function (name, content) {
102
  mkdirp(path.join('.inc', 'containers'), function () {
103
    jsonfile.writeFile(Container.pathForConfig(name), content)
104
    if (!fs.existsSync(path.join('.inc', 'version'))) {
105
      fs.writeFileSync(path.join('.inc', 'version'), '11', 'utf8')
106
    }
107
  })
108
}
109
110
module.exports = Container
111