lib/containers/Container.js   A
last analyzed

Complexity

Total Complexity 13
Complexity/F 2.6

Size

Lines of Code 88
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 13
c 1
b 0
f 0
nc 2
mnd 3
bc 15
fnc 5
dl 0
loc 88
rs 10
bpm 3
cpm 2.6
noi 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A Container.create 0 3 1
A Container.clean 0 7 2
A Container.getConfig 0 3 1
A Container.getVersion 0 10 3
B Container.performUpgrade 0 24 6
1
'use strict'
2
3
const output = require('../output')
4
const path = require('path')
5
const fs = require('fs')
6
7
const ContainerConfig = require('./ContainerConfig')
8
const Boxes = require('./Boxes')
9
10
var Container = {}
11
12
/**
13
 * Get the version of the container metadata stored in the current directory (if any).
14
 *
15
 * @returns {int|boolean}
16
 */
17
Container.getVersion = function () {
18
  if (!fs.existsSync(path.join('.inc', 'version'))) {
19
    if (fs.existsSync('.scho' + 'lica')) {
20
      return 10
21
    }
22
    return false
23
  }
24
25
  return parseInt(fs.readFileSync(path.join('.inc', 'version'), 'utf8'), 10) || false
26
}
27
28
/**
29
 * Remove the default container config file.
30
 */
31
Container.clean = function () {
32
  try {
33
    fs.unlinkSync(path.join('.inc', 'containers', 'default.json'))
34
  } catch (e) {
35
    // Do nothing
36
  }
37
}
38
39
/**
40
 * Perform a container metadata structure upgrade (if necessary).
41
 */
42
Container.performUpgrade = function () {
43
  var version = this.getVersion()
44
  if (!version) {
45
    return
46
  }
47
48
  if (version > global.containerVersion) {
49
    output.warn('Warning: project metadata is created using a newer version of the CLI, please upgrade')
50
    return
51
  }
52
53
  if (version < global.containerVersion) {
54
    var upgradeFile = path.join(global.base, 'lib', 'upgrades', version + '-' + global.containerVersion + '.js')
55
    try {
56
      if (!fs.existsSync(upgradeFile)) {
57
        throw new Error('no upgrade path from version ' + version + ' to ' + global.containerVersion)
58
      }
59
      require(upgradeFile)()
60
      output.warn('Upgraded project metadata from ' + version + ' to ' + global.containerVersion)
61
    } catch (e) {
62
      output.warn('Error upgrading project: ' + e.message)
63
    }
64
  }
65
}
66
67
/**
68
 * Read a container config file. If the file with the supplied container name doesn't exist,
69
 * this falls back to the 'default' container, if that one exists.
70
 *
71
 * @param {string} name
72
 * @param {function(Error, ContainerConfigObject)} cb
73
 */
74
Container.getConfig = function (name, cb) {
75
  ContainerConfig.read(name, cb)
76
}
77
78
/**
79
 * Create a new Includable cloud instance (dev mode).
80
 *
81
 * @param {RunOptions} options
82
 * @param {function(Error, BoxCreateResult)} callback
83
 */
84
Container.create = function (options, callback) {
85
  Boxes.create(options, callback)
86
}
87
88
module.exports = Container
89