-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadSync.js
142 lines (109 loc) · 3.63 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
'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 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 = resolveYamlImportSync(resource);
if (!_.has(jsonImport, 'pick')) return resolvedYamlImport;
return _.chain(jsonImport)
.get('pick', {})
.transform((accu, destinationPath, sourcePath) => {
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();
}