1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 |
|
6 | const loadAggregate = (filePath, { commands = {}, events = {} }) => {
|
7 | const aggregate = {
|
8 | commands: {},
|
9 | events: {},
|
10 | path: filePath,
|
11 | };
|
12 |
|
13 |
|
14 | Object.entries(commands).forEach(([commandName]) => {
|
15 | aggregate.commands[commandName] = {};
|
16 | });
|
17 |
|
18 |
|
19 | Object.entries(events).forEach(([eventName]) => {
|
20 | aggregate.events[eventName] = {};
|
21 | });
|
22 |
|
23 | return aggregate;
|
24 | };
|
25 |
|
26 | const 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));
|
39 | });
|
40 |
|
41 | return context;
|
42 | };
|
43 |
|
44 | const 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 |
|
59 | module.exports = (directory, options) => loadDomain(directory, options);
|