-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (32 loc) 路 1.39 KB
/
Copy pathindex.js
File metadata and controls
36 lines (32 loc) 路 1.39 KB
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
'use strict';
const Forge = require('./lib/forge');
const createError = require('./lib/error');
Schema.modules = require('./modules');
module.exports = Schema;
module.exports.Schema = Schema;
module.exports.default = Schema;
const MODULE_ERROR = 'Module already exists: ';
function Schema(plan, options = {}) {
const { modules = Schema.modules, errorFormat, prototypes } = options;
const [SchemaError, warnings] = [createError({ format: errorFormat }), []];
[this.tools, this.modules] = [{ Error: SchemaError, build, warn }, new Map()];
const forge = new Forge(this, prototypes);
this.child = (a = plan, b = options) => new Schema(a, b === options ? b : { ...b, ...options });
this.register = (name, module) => {
if (this.modules.has(module)) warn({ cause: MODULE_ERROR, plan: this.modules, sample: module });
module(this, options, plan), this.modules.set(name);
return this;
};
[this.forge, this.warnings] = [forge, warnings];
for (const [name, plugin] of modules.entries()) this.register(name, plugin);
return Object.assign(this, this.tools.build(plan));
function build(plan) {
const Type = forge.get(plan.$type);
if (Type) return new Type(plan);
throw new Error('Building error: recieved wrong plan:\n' + JSON.stringify(plan));
}
function warn(options) {
const warn = new SchemaError({ path: 'BUILD', ...options });
return warnings.push(warn), warn;
}
}