-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschema.js
More file actions
39 lines (32 loc) · 771 Bytes
/
schema.js
File metadata and controls
39 lines (32 loc) · 771 Bytes
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
'use strict';
const path = require('node:path');
const fs = require('node:fs');
const entities = new Map();
const loadEntity = (schemaPath, name) => {
const filePath = schemaPath + name;
const key = path.basename(filePath, '.js');
try {
const modulePath = require.resolve(filePath);
delete require.cache[modulePath];
} catch {
return;
}
try {
const entity = require(filePath);
entities.set(key, entity);
} catch {
entities.delete(key);
}
};
const schema = {};
schema.load = (schemaPath) => {
fs.readdir(schemaPath, (err, files) => {
if (err) return;
files.forEach((name) => {
loadEntity(schemaPath, name);
});
});
return schema;
};
schema.get = (name) => entities.get(name);
module.exports = schema;