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

Container.getConfig   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
nc 4
dl 0
loc 13
rs 8.8571
nop 2
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) {
26
    // Do nothing
27
  }
28
}
29
30
Container.performUpgrade = function () {
31
  var version = this.getVersion()
32
  if (!version) {
33
    return
34
  }
35
36
  if (version > global.containerVersion) {
37
    output.warn('Warning: project metadata is created using a newer version of the CLI, please upgrade')
38
    return
39
  }
40
41
  if (version < global.containerVersion) {
42
    var upgradeFile = path.join(global.base, 'lib', 'upgrades', version + '-' + global.containerVersion + '.js')
43
    try {
44
      if (!fs.existsSync(upgradeFile)) {
45
        throw new Error('no upgrade path from version ' + version + ' to ' + global.containerVersion)
46
      }
47
      require(upgradeFile)()
48
      output.warn('Upgraded project metadata from ' + version + ' to ' + global.containerVersion)
49
    } catch (e) {
50
      output.warn('Error upgrading project: ' + e.message)
51
    }
52
  }
53
}
54
55
Container.validateContainerContent = function (config) {
56
  if (!('containerToken' in config) && 'container_token' in config) {
57
    config.containerToken = config.container_token
58
  }
59
60
  if ('containerToken' in config) {
61
    Container.applyConfig(config)
62
    return true
63
  }
64
65
  return false
66
}
67
68
Container.applyConfig = function (config) {
69
  if ('containerEndpoint' in config) {
70
    global.containerEndpoint = config.containerEndpoint
71
  }
72
  if ('version' in config) {
73
    global.apiVersion = config.version
74
  }
75
  if ('stability' in config) {
76
    global.apiStability = config.stability
77
  }
78
  if ('containerEndpointCheckSSL' in config && !config.containerEndpointCheckSSL) {
79
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
80
  }
81
}
82
83
Container.pathForConfig = function (name) {
84
  return path.join('.inc', 'containers', name + '.json')
85
}
86
87
Container.getConfig = function (name, cb) {
88
  jsonfile.readFile(Container.pathForConfig(name), function (err, config) {
89
    // Use existing container
90
    if (!err && typeof config === 'object' && Container.validateContainerContent(config)) {
91
      return cb(null, config)
92
    }
93
94
    // Try default file if in `beta` mode
95
    if (name === 'beta') {
96
      return Container.getConfig('default', cb)
97
    }
98
99
    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...
100
  })
101
}
102
103
Container.writeConfig = function (name, content) {
104
  mkdirp(path.join('.inc', 'containers'), function () {
105
    jsonfile.writeFile(Container.pathForConfig(name), content)
106
    if (!fs.existsSync(path.join('.inc', 'version'))) {
107
      fs.writeFileSync(path.join('.inc', 'version'), '11', 'utf8')
108
    }
109
  })
110
}
111
112
module.exports = Container
113