-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadSync.js
160 lines (126 loc) · 4.21 KB
/
loadSync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const
yaml = require('yamljs'),
_ = require('lodash'),
path = require('path'),
fs = require('fs-extra')
;
const fillPlaceholder = require('./fillPlaceholder')
;
module.exports = loadSync;
function loadSync (file) {
let config;
const resolvedImports = [];
config = resolveYamlImportSync(file);
config = fillPlaceholder(config || {});
if (config.hasOwnProperty('parameters') && !_.isPlainObject(config.parameters)) {
throw typeException('parameters', 'plain Object', config.parameters);
}
return config;
function resolveYamlImport(yamlFile, {optional = false}){
try{
return resolveYamlImportSync(yamlFile);
} catch(err){
if(optional === true && ['ENOENT', 'MODULE_NOT_FOUND'].includes(err.code)){
console.warn('ConfigComponent: Skipped import of ' + yamlFile);
return {};
}
throw err;
}
}
function resolveYamlImportSync (yamlFile)/* use (resolvedImports)*/ {
let json,
dirname = path.dirname(yamlFile)
;
json = _import(yamlFile) || {};
resolvedImports.push(yamlFile);
if(!_.isPlainObject(json)){
throw typeException('Imported file '+ yamlFile, 'plain Object', json);
}
if (json.hasOwnProperty('parameters') && !_.isPlainObject(json.parameters)) {
throw typeException('parameters', 'plain Object', json.parameters);
}
if (json.hasOwnProperty('env')) {
throw forbidenPropertyException('env');
}
if (!json.imports) {
resolvedImports.pop();
return json;
}
let results = json.imports.map(
(jsonImport) => {
let resource =
path.isAbsolute(jsonImport.resource)
? jsonImport.resource
: path.join(dirname, jsonImport.resource),
resolvedYamlImport
;
if (resolvedImports.includes(resource)) {
throw importCircularReferenceException(resource, yamlFile);
}
resolvedYamlImport = resolveYamlImport(resource, {optional: jsonImport.optional});
if (!_.has(jsonImport, 'pick')) return resolvedYamlImport;
return _.chain(jsonImport)
.get('pick', {})
.transform((accu, destinationPath, sourcePath) => {
if (sourcePath === '*') {
_.assign(accu, _.pick(resolvedYamlImport, 'parameters'));
if (_.isNil(destinationPath)) {_.assign(accu, _.omit(resolvedYamlImport, 'parameters')); return;}
_.set(accu, destinationPath, _.omit(resolvedYamlImport, 'parameters'));
return;
}
if (!_.has(resolvedYamlImport, sourcePath)) return;
if (_.isNil(destinationPath)) {destinationPath = sourcePath;}
const get = _.get(resolvedYamlImport, sourcePath);
const set = destinationPath;
_.set(accu, set, get);
},
{}
)
.value();
}
);
results.reverse().unshift(json);
json =
_(_.spread(_.defaultsDeep)(results))
.omit('imports')
.value();
resolvedImports.pop();
return json;
}
}
function _import (file) {
// How do we handle file by type
const mapping = {
'.json': fs.readJsonSync,
'.yml' : _loadYml,
'.js' : require,
'.node': require
};
const func = _.get(mapping, path.extname(file), mapping['.yml']);
if (path.extname(file) === '') {
file = file + '.yml';
}
return func(file);
}
function _loadYml(file){
return yaml.parse(fs.readFileSync(file, {encoding:'utf8'}));
}
function importCircularReferenceException (importedYamlFile, yamlFile) {
let err = new Error(`Circular reference detected importing ${importedYamlFile} in ${yamlFile}`);
err.name = 'importCircularReferenceException';
return err;
}
function typeException (name, expectedType, value) {
let err = new Error(`${name} must be a ${expectedType}, it is presently: ${_toType(value)} => ${value}`);
err.name = 'typeException';
return err;
}
function forbidenPropertyException (property) {
let err = new Error(`this property is forbiden: ${property}`);
err.name = 'forbidenPropertyException';
return err;
}
function _toType (obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}