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) |
|
|
|
|
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
|
|
|
|