Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0e43e3a

Browse files
committedAug 2, 2016
refactor: loadServerEnv -> getServerEnv
1 parent 3c7e0b3 commit 0e43e3a

File tree

3 files changed

+85
-80
lines changed

3 files changed

+85
-80
lines changed
 

‎lib/base_loader.js

+43-45
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,52 @@ class EggLoader {
6969
this.eggPaths = [ this.eggPath ].concat(this.frameworkPaths);
7070
debug('Loaded eggPaths %j', this.eggPaths);
7171

72-
/**
73-
* 获取当前应用所在的机器环境,统一 serverEnv
74-
* ```
75-
* serverEnv | 说明
76-
* --- | ---
77-
* default | 默认环境
78-
* test | 交付测试
79-
* prod | 主站生产环境,包括预发,线上服务器
80-
* local | 本地开发环境,就是你的电脑本地启动
81-
* unittest | 单元测试环境,tnpm test, NODE_ENV=test
82-
* ```
83-
*
84-
* @member {String} EggLoader#serverEnv
85-
*/
86-
this.serverEnv = this.loadServerEnv();
72+
this.serverEnv = this.getServerEnv();
8773
debug('Loaded serverEnv %j', this.serverEnv);
8874
}
8975

76+
/**
77+
* Get environment of Egg, it's not NODE_ENV
78+
*
79+
* 1. from `$baseDir/config/serverEnv`
80+
* 2. from EGG_SERVER_ENV
81+
* 3. from NODE_ENV
82+
*
83+
* serverEnv | description
84+
* --- | ---
85+
* default | default environment
86+
* test | system integration testing
87+
* prod | production
88+
* local | local on your own computer
89+
* unittest | unit test
90+
*
91+
* @return {String} serverEnv
92+
*/
93+
getServerEnv() {
94+
let serverEnv;
95+
96+
const envPath = path.join(this.options.baseDir, 'config/serverEnv');
97+
if (fs.existsSync(envPath)) {
98+
serverEnv = fs.readFileSync(envPath, 'utf8').trim();
99+
}
100+
101+
if (!serverEnv) {
102+
serverEnv = process.env.EGG_SERVER_ENV;
103+
}
104+
105+
if (!serverEnv) {
106+
if (process.env.NODE_ENV === 'test') {
107+
serverEnv = 'unittest';
108+
} else if (process.env.NODE_ENV === 'production') {
109+
serverEnv = 'default';
110+
} else {
111+
serverEnv = 'local';
112+
}
113+
}
114+
115+
return serverEnv;
116+
}
117+
90118
/**
91119
* 加载自定义的 app.js,**在 app.js 可做任何操作,但建议尽量减少此操作,做该做的事**。
92120
*
@@ -203,36 +231,6 @@ class EggLoader {
203231
return dirs;
204232
}
205233

206-
/**
207-
* 获取环境变量
208-
*
209-
* 1. 从 EGG_SERVER_ENV 获取,一般用于测试
210-
* 2. 从 `$baseDir/config/serverEnv` 读取,框架可根据实际情况自行设置
211-
* 3. 默认值
212-
*
213-
* @return {String} serverEnv
214-
* @see EggLoader#serverEnv
215-
*/
216-
loadServerEnv() {
217-
let serverEnv = process.env.EGG_SERVER_ENV;
218-
219-
const envPath = path.join(this.options.baseDir, 'config/serverEnv');
220-
if (fs.existsSync(envPath)) {
221-
serverEnv = fs.readFileSync(envPath, 'utf8').trim();
222-
}
223-
224-
if (!serverEnv) {
225-
if (process.env.NODE_ENV === 'test') {
226-
serverEnv = 'unittest';
227-
} else if (process.env.NODE_ENV === 'production') {
228-
serverEnv = 'default';
229-
} else {
230-
serverEnv = 'local';
231-
}
232-
}
233-
234-
return serverEnv;
235-
}
236234

237235
/**
238236
* 获取 {@link EggLoader#frameworkPaths}

‎test/egg_loader.test.js

-35
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,6 @@ describe('test/egg_loader.test.js', function() {
1010

1111
afterEach(mm.restore);
1212

13-
describe('.getServerEnv', function() {
14-
15-
it('should get from env EGG_SERVER_ENV', function() {
16-
mm(process.env, 'EGG_SERVER_ENV', 'prod');
17-
const app = utils.createApp('serverenv');
18-
app.loader.serverEnv.should.equal('prod');
19-
});
20-
21-
it('should use unittest when NODE_ENV = test', function() {
22-
mm(process.env, 'NODE_ENV', 'test');
23-
const app = utils.createApp('serverenv');
24-
app.loader.serverEnv.should.equal('unittest');
25-
});
26-
27-
it('should use default when NODE_ENV = production', function() {
28-
mm(process.env, 'NODE_ENV', 'production');
29-
const app = utils.createApp('serverenv');
30-
app.loader.serverEnv.should.equal('default');
31-
});
32-
33-
it('should use local when NODE_ENV is other', function() {
34-
mm(process.env, 'NODE_ENV', 'development');
35-
const app = utils.createApp('serverenv');
36-
app.loader.serverEnv.should.equal('local');
37-
});
38-
39-
it('should get from config/serverEnv', function() {
40-
mm(process.env, 'NODE_ENV', 'production');
41-
mm(process.env, 'EGG_SERVER_ENV', 'test');
42-
const app = utils.createApp('serverenv-file');
43-
app.loader.serverEnv.should.equal('prod');
44-
});
45-
});
46-
47-
4813
describe('eggPaths', function() {
4914

5015
it('should get from paramter', function() {

‎test/get_server_env.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
require('should');
4+
const mm = require('mm');
5+
const utils = require('./utils');
6+
7+
describe('test/get_server_env.test.js', function() {
8+
9+
afterEach(mm.restore);
10+
11+
it('should get from env EGG_SERVER_ENV', function() {
12+
mm(process.env, 'EGG_SERVER_ENV', 'prod');
13+
const app = utils.createApp('serverenv');
14+
app.loader.serverEnv.should.equal('prod');
15+
});
16+
17+
it('should use unittest when NODE_ENV = test', function() {
18+
mm(process.env, 'NODE_ENV', 'test');
19+
const app = utils.createApp('serverenv');
20+
app.loader.serverEnv.should.equal('unittest');
21+
});
22+
23+
it('should use default when NODE_ENV = production', function() {
24+
mm(process.env, 'NODE_ENV', 'production');
25+
const app = utils.createApp('serverenv');
26+
app.loader.serverEnv.should.equal('default');
27+
});
28+
29+
it('should use local when NODE_ENV is other', function() {
30+
mm(process.env, 'NODE_ENV', 'development');
31+
const app = utils.createApp('serverenv');
32+
app.loader.serverEnv.should.equal('local');
33+
});
34+
35+
it('should get from config/serverEnv', function() {
36+
mm(process.env, 'NODE_ENV', 'production');
37+
mm(process.env, 'EGG_SERVER_ENV', 'test');
38+
const app = utils.createApp('serverenv-file');
39+
app.loader.serverEnv.should.equal('prod');
40+
});
41+
42+
});

0 commit comments

Comments
 (0)
Please sign in to comment.