-
Notifications
You must be signed in to change notification settings - Fork 249
/
Schema.js
193 lines (177 loc) · 5.82 KB
/
Schema.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const _ = require('lodash');
const fs = require('fs-extra');
/**
* @typedef {import('./Framework')} Framework
* @typedef {import('./JSONFileItem')} JSONFileItem
* @typedef {import('../plugins/Plugin')} Plugin
*/
class Schema {
/**
* @param {Object} options
* @param {Framework} options.framework
* @param {string} options.name
* @param {Plugin} options.plugin
* @param {Object} options.json
* @param {string} options.filePath
* @param {string} options.globalsType
*/
constructor({
framework = null,
name = '',
plugin = null,
json = null,
filePath = '',
globalsType = ''
} = {}) {
/** @type {Framework} */
this.framework = framework;
/** @type {Plugin} */
this.plugin = plugin;
/** @type {string} */
this.name = name;
/** @type {Object} */
this.json = json;
/** @type {string} */
this.filePath = filePath;
/** @type {string} */
this.globalsType = globalsType;
}
/** @returns {Schema} */
load() {
this.json = fs.readJSONSync(this.filePath);
return this;
}
/**
* Walk through schema properties and an object's attributes calling an
* iterator function with the attributeName, attributeType and schema
* description along with the framework object, the current object attribute
* node and any other pass-through arguments.
* @param {string} schemaPath The attribute path from which to start in the schema
* @param {SchemaTraverseIterator} iterator
* @param {...any} args pass-through arguments
* @returns {Schema}
*/
traverse(schemaPath, iterator, ...args) {
let shouldStop = false;
const json = schemaPath ? _.get(this.json.properties, schemaPath) : this.json;
const recursiveSchemaNodeProperties = (properties, ...args) => {
let rtnValue = false;
// process properties
for (const attributeName in properties) {
let description = properties[attributeName];
if (description.hasOwnProperty('editorOnly') || !description.hasOwnProperty('type')) {
// go to next attribute
continue;
}
description = { framework: this.framework, name: attributeName, ...description };
// process current properties attribute
const returned = iterator({
description,
next: (...args) => {
// continue with recursion if able
switch (description.type) {
case 'object':
return recursiveSchemaNodeProperties(description.properties, ...args);
case 'array':
if (description.items.type === 'object') {
return recursiveSchemaNodeProperties(description.items.properties, ...args);
}
const next = {};
next[attributeName] = description.items;
return recursiveSchemaNodeProperties(next, ...args);
}
},
stop: () => {
shouldStop = true;
}
}, ...args);
rtnValue = rtnValue || returned;
if (shouldStop) {
return;
}
}
};
recursiveSchemaNodeProperties(json.properties, ...args);
return this;
}
/**
* Applies schema defaults to the given object.
* @param {Object} output
* @param {string} schemaPath
* @param {Object} options
* @param {boolean} options.fillObjects Infer array or object default objects
* @returns {Schema}
*/
applyDefaults(output = {}, schemaPath, options = { fillObjects: true }) {
function sortKeys(object) {
const keys = Object.keys(object).sort((a, b) => {
return a.localeCompare(b);
});
keys.forEach(name => {
const value = object[name];
delete object[name];
object[name] = value;
});
return object;
}
this.traverse(schemaPath, ({ description, next }, output) => {
let hasChanged = false;
let haveChildenChanged = false;
let defaultValue;
if (description === null || output === null) return;
switch (description.type) {
case 'object':
defaultValue = description.hasOwnProperty('default') && options.fillObjects ? description.default : {};
if (!output.hasOwnProperty(description.name)) {
output[description.name] = defaultValue;
hasChanged = true;
}
haveChildenChanged = next(output[description.name]);
if (haveChildenChanged) {
sortKeys(output[description.name]);
}
break;
case 'array':
defaultValue = description.hasOwnProperty('default') && options.fillObjects ? description.default : [];
if (!output.hasOwnProperty(description.name)) {
output[description.name] = defaultValue;
hasChanged = true;
}
haveChildenChanged = next(output[description.name]);
if (haveChildenChanged) {
sortKeys(output[description.name]);
}
break;
default:
defaultValue = description.default;
if (description.hasOwnProperty('default') && !output.hasOwnProperty(description.name)) {
output[description.name] = defaultValue;
hasChanged = true;
}
break;
}
return hasChanged;
}, output);
return output;
}
}
/**
* @typedef SchemaNodeDescription
* @property {Framework} framework Schema properties node
* @property {string} name Attribute name
* @property {string} type Attribute type
* @property {boolean} editorOnly
*/
/**
* @typedef SchemaTraverseIteratorParam0
* @property {SchemaNodeDescription} description
* @property {function} next
* @property {function} stop
*/
/**
* Iterator function for schema.traverse.
* @callback SchemaTraverseIterator
* @param {SchemaTraverseIteratorParam0} config
* @param {...any} args pass-through arguments
*/
module.exports = Schema;