UNPKG

1.36 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5
6const loadAggregate = (filePath, { commands = {}, events = {} }) => {
7 const aggregate = {
8 commands: {},
9 events: {},
10 path: filePath,
11 };
12
13 // commands
14 Object.entries(commands).forEach(([commandName]) => {
15 aggregate.commands[commandName] = {};
16 });
17
18 // events
19 Object.entries(events).forEach(([eventName]) => {
20 aggregate.events[eventName] = {};
21 });
22
23 return aggregate;
24};
25
26const loadContext = (contextDirectory) => {
27 const context = {};
28
29 fs.readdirSync(contextDirectory).forEach((aggregateName) => {
30 const aggregateFile = path.join(contextDirectory, aggregateName);
31
32
33 if (!fs.statSync(aggregateFile).isFile())
34 return;
35
36 if (path.extname(aggregateFile) !== '.js') return;
37
38 context[path.basename(aggregateName, '.js')] = loadAggregate(aggregateFile, require(aggregateFile)); // eslint-disable-line
39 });
40
41 return context;
42};
43
44const loadDomain = (writeModelDirectory, options) => {
45 const contexts = {};
46
47 fs.readdirSync(writeModelDirectory).forEach((contextName) => {
48 const contextDirectory = path.join(writeModelDirectory, contextName);
49
50 if (!fs.statSync(contextDirectory).isDirectory())
51 return;
52
53 contexts[contextName] = loadContext(contextDirectory, options);
54 });
55
56 return contexts;
57};
58
59module.exports = (directory, options) => loadDomain(directory, options);