Skip to content
This repository was archived by the owner on Apr 7, 2020. It is now read-only.

Commit 5be6630

Browse files
author
caizhenxing
committed
单元测试通过
1 parent 607e0fe commit 5be6630

22 files changed

+349
-280
lines changed

src/app.js

Lines changed: 164 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,164 @@
1-
import path from 'path';
2-
3-
import config from './config';
4-
import {
5-
fxData,
6-
alias,
7-
_require
8-
} from './core';
9-
import {
10-
safeRequire,
11-
log,
12-
isDir,
13-
isFile,
14-
sep,
15-
exists,
16-
getDirs,
17-
getFiles,
18-
isString,
19-
isFunction,
20-
merge
21-
} from './utils';
22-
import WatchCompile from './util/watch_compile';
23-
import AutoReload from './util/auto_reload';
24-
import i18n from './i18n';
25-
import * as bus from './bus';
26-
27-
export default class App {
28-
_modules = []
29-
_types = ['command', 'domain', 'event', 'config']
30-
_dirname = {
31-
command: 'command',
32-
config: 'config',
33-
event: 'event',
34-
domain: 'domain',
35-
}
36-
37-
constructor(options) {
38-
config.init(options || {});
39-
if (!config.appPath || typeof config.appPath !== 'string') {
40-
throw new Error(i18n.t('appPath无效无法加载CRQS应用'));
41-
}
42-
}
43-
44-
loadSubModule(name) {
45-
let dir = path.join(config.appPath, name);
46-
if (isDir(dir)) {
47-
var dirs = getDirs(dir);
48-
if (dirs.length <= 0) return; // 空模块
49-
let isModule = false;
50-
for (let subdir of dirs) {
51-
if (this._types.indexOf(subdir) > -1) {
52-
isModule = true;
53-
break;
54-
}
55-
}
56-
if (!isModule) {
57-
for (let subdir of dirs) {
58-
this.loadSubModule(path.join(name, subdir));
59-
}
60-
} else if (name === '') {
61-
throw new Error(i18n.t('appPath级别错误,需要设置到当前模块文件夹的上一级'));
62-
} else {
63-
this._modules.push(name);
64-
}
65-
}
66-
}
67-
68-
loadModule() {
69-
this._modules = [];
70-
this.loadSubModule('');
71-
}
72-
73-
// 加载cqrs
74-
loadCQRS() {
75-
for (let itemType of this._types) {
76-
this._modules.forEach(module => {
77-
let name = module.replace(/\\/g, '/');
78-
let moduleType = name + '/' + itemType;
79-
let filepath = `${config.appPath}${sep}${module}${sep}${this._dirname[itemType]}`;
80-
alias(moduleType, filepath);
81-
});
82-
}
83-
}
84-
85-
checkEnv() {
86-
if (!exists(config.appPath)) {
87-
throw Error(`appPath "${config.appPath || ''}" not found.`);
88-
}
89-
}
90-
91-
autoReload() {
92-
//it auto reload by watch compile
93-
if (this.compileCallback) {
94-
return;
95-
}
96-
let instance = this.getReloadInstance();
97-
instance.run();
98-
}
99-
100-
getReloadInstance(srcPath) {
101-
srcPath = srcPath || config.appPath;
102-
let instance = new AutoReload(srcPath, () => {
103-
this.clearData();
104-
this.load();
105-
});
106-
return instance;
107-
}
108-
109-
compile(srcPath, outPath, options = {}) {
110-
srcPath = srcPath || `${config.appPath}${sep}..${sep}src`;
111-
outPath = outPath || config.appPath;
112-
if (isDir(srcPath)) {
113-
let reloadInstance = this.getReloadInstance(outPath);
114-
this.compileCallback = changedFiles => {
115-
reloadInstance.clearFilesCache(changedFiles);
116-
};
117-
let instance = new WatchCompile(srcPath, outPath, options, this.compileCallback);
118-
instance.run();
119-
console.log(`watch ${srcPath} for compile...`);
120-
}
121-
}
122-
123-
clearData() {
124-
if (this._modules) {
125-
fxData.alias = {};
126-
fxData.export = {};
127-
}
128-
}
129-
130-
load() {
131-
this.checkEnv();
132-
this.loadModule();
133-
this.loadCQRS();
134-
}
135-
136-
preload() {
137-
let startTime = Date.now();
138-
for (let name in fxData.alias) {
139-
_require(name);
140-
}
141-
log('cqrs preload packages finished', 'PRELOAD', startTime);
142-
}
143-
144-
run(preload) {
145-
this.clearData();
146-
this.load();
147-
this.autoReload();
148-
if (preload) {
149-
this.preload();
150-
}
151-
}
152-
153-
publishCommand(...messages) {
154-
bus.publishCommand(...messages);
155-
}
156-
157-
}
1+
import path from 'path';
2+
3+
import config from './config';
4+
import {fxData, alias, _require} from './core';
5+
import {
6+
safeRequire,
7+
log,
8+
isDir,
9+
isFile,
10+
sep,
11+
exists,
12+
getDirs,
13+
getFiles,
14+
isString,
15+
isFunction,
16+
merge
17+
} from './utils';
18+
import WatchCompile from './util/watch_compile';
19+
import AutoReload from './util/auto_reload';
20+
import i18n from './i18n';
21+
import * as bus from './bus';
22+
23+
export default class App {
24+
_modules = []
25+
_types = ['command', 'domain', 'event', 'config']
26+
_dirname = {
27+
command: 'command',
28+
config: 'config',
29+
event: 'event',
30+
domain: 'domain'
31+
}
32+
33+
constructor(options) {
34+
config.init(options || {});
35+
if (!config.appPath || typeof config.appPath !== 'string') {
36+
throw new Error(i18n.t('appPath无效无法加载CRQS应用'));
37+
}
38+
}
39+
40+
loadSubModule(name) {
41+
let dir = path.join(config.appPath, name);
42+
if (isDir(dir)) {
43+
var dirs = getDirs(dir);
44+
if (dirs.length <= 0)
45+
return; // 空模块
46+
let isModule = false;
47+
for (let subdir of dirs) {
48+
if (this._types.indexOf(subdir) > -1) {
49+
isModule = true;
50+
break;
51+
}
52+
}
53+
if (!isModule) {
54+
for (let subdir of dirs) {
55+
this.loadSubModule(path.join(name, subdir));
56+
}
57+
} else if (name === '') {
58+
throw new Error(i18n.t('appPath级别错误,需要设置到当前模块文件夹的上一级'));
59+
} else {
60+
this._modules.push(name);
61+
}
62+
}
63+
}
64+
65+
loadModule() {
66+
this._modules = [];
67+
this.loadSubModule('');
68+
}
69+
70+
// 加载cqrs
71+
loadCQRS() {
72+
for (let itemType of this._types) {
73+
this._modules.forEach(module => {
74+
let name = module.replace(/\\/g, '/');
75+
let moduleType = name + '/' + itemType;
76+
let filepath = `${config.appPath}${sep}${module}${sep}${this._dirname[itemType]}`;
77+
alias(moduleType, filepath);
78+
});
79+
}
80+
}
81+
82+
checkEnv() {
83+
if (!exists(config.appPath)) {
84+
throw Error(`appPath "${config.appPath || ''}" not found.`);
85+
}
86+
}
87+
88+
autoReload() {
89+
//it auto reload by watch compile
90+
if (this.compileCallback) {
91+
return;
92+
}
93+
let instance = this.getReloadInstance();
94+
instance.run();
95+
}
96+
97+
getReloadInstance(srcPath) {
98+
srcPath = srcPath || config.appPath;
99+
let instance = new AutoReload(srcPath, () => {
100+
this.clearData();
101+
this.load();
102+
});
103+
return instance;
104+
}
105+
106+
compile(srcPath, outPath, options = {}) {
107+
srcPath = srcPath || `${config.appPath}${sep}..${sep}src`;
108+
outPath = outPath || config.appPath;
109+
if (isDir(srcPath)) {
110+
let reloadInstance = this.getReloadInstance(outPath);
111+
this.compileCallback = changedFiles => {
112+
reloadInstance.clearFilesCache(changedFiles);
113+
};
114+
let instance = new WatchCompile(srcPath, outPath, options, this.compileCallback);
115+
instance.run();
116+
console.log(`watch ${srcPath} for compile...`);
117+
}
118+
}
119+
120+
clearData() {
121+
if (this._modules) {
122+
// 只能有一个实例
123+
fxData.alias = {};
124+
fxData.export = {};
125+
fxData.container = {};
126+
}
127+
}
128+
129+
load() {
130+
this.checkEnv();
131+
this.loadModule();
132+
this.loadCQRS();
133+
}
134+
135+
preload() {
136+
let startTime = Date.now();
137+
for (let name in fxData.alias) {
138+
_require(name);
139+
}
140+
log('cqrs preload packages finished', 'PRELOAD', startTime);
141+
}
142+
143+
run(...args) {
144+
this.clearData();
145+
this.load();
146+
this.autoReload();
147+
if (isFunction(args[0])) {
148+
if (args[1]){
149+
this.preload();
150+
}
151+
args[0](this);
152+
} else {
153+
if (args[0]) {
154+
this.preload();
155+
}
156+
}
157+
return this;
158+
}
159+
160+
publishCommand(...messages) {
161+
bus.publishCommand(...messages);
162+
}
163+
164+
}

src/bus/direct_bus.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import Bus from './bus';
22
import {getDispatcher} from './index';
3+
import {log} from '../utils';
4+
import i18n from '../i18n';
35

46
export default class DirectBus extends Bus {
57
async commit() {
8+
log(i18n.t('提交分发'));
69
const dispatcher = getDispatcher(this.type);
710
this.messageQueue.forEach(async(msg) => {
811
await dispatcher.dispatch({type: this.type, module: msg.module, name: msg.name, data: msg.data});
@@ -12,5 +15,6 @@ export default class DirectBus extends Bus {
1215

1316
async rollback() {
1417
await getRepository().rollback();
18+
log(i18n.t('回滚存储'));
1519
}
1620
}

0 commit comments

Comments
 (0)