Skip to content

Commit eedfd3d

Browse files
popomorefengmk2
authored andcommitted
feat: support serverScope (#120)
scope is a deploy unit, you can set different config between scopes. Example: Deploy application in us and asia with different config,you can set - config.us_prod.js - config.ap_prod.js
1 parent 15f785a commit eedfd3d

21 files changed

Lines changed: 211 additions & 13 deletions

lib/loader/egg_loader.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ class EggLoader {
7878
* @since 1.0.0
7979
*/
8080
this.appInfo = this.getAppInfo();
81+
82+
/**
83+
* @member {String} EggLoader#serverScope
84+
* @see AppInfo#serverScope
85+
*/
86+
this.serverScope = this.getServerScope();
8187
}
8288

8389
/**
@@ -112,6 +118,15 @@ class EggLoader {
112118
return serverEnv;
113119
}
114120

121+
/**
122+
* Get {@link AppInfo#scope}
123+
* @return {String} serverScope
124+
* @private
125+
*/
126+
getServerScope() {
127+
return process.env.EGG_SERVER_SCOPE || '';
128+
}
129+
115130
/**
116131
* Get {@link AppInfo#name}
117132
* @return {String} appname
@@ -144,6 +159,7 @@ class EggLoader {
144159
*/
145160
getAppInfo() {
146161
const env = this.serverEnv;
162+
const scope = this.serverScope;
147163
const home = this.getHomedir();
148164
const baseDir = this.options.baseDir;
149165

@@ -183,6 +199,11 @@ class EggLoader {
183199
*/
184200
env,
185201

202+
/**
203+
* @member {String} AppInfo#scope
204+
*/
205+
scope,
206+
186207
/**
187208
* The use directory, same as `process.env.HOME`
188209
* @member {String} AppInfo#HOME
@@ -366,6 +387,15 @@ class EggLoader {
366387
return ContextLoader;
367388
}
368389

390+
getTypeFiles(filename) {
391+
const files = [ `${filename}.default.js` ];
392+
if (this.serverScope) files.push(`${filename}.${this.serverScope}.js`);
393+
if (this.serverEnv === 'default') return files;
394+
395+
files.push(`${filename}.${this.serverEnv}.js`);
396+
if (this.serverScope) files.push(`${filename}.${this.serverScope}_${this.serverEnv}.js`);
397+
return files;
398+
}
369399
}
370400

371401
/**

lib/loader/mixin/config.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ module.exports = {
2323

2424
const target = {};
2525

26-
const names = [
27-
'config.default.js',
28-
`config.${this.serverEnv}.js`,
29-
];
30-
3126
// Load Application config first
3227
const appConfig = this._preloadAppConfig();
3328

@@ -37,7 +32,7 @@ module.exports = {
3732
// plugin config.{env}
3833
// framework config.{env}
3934
// app config.{env}
40-
for (const filename of names) {
35+
for (const filename of this.getTypeFiles('config')) {
4136
for (const unit of this.getLoadUnits()) {
4237
const isApp = unit.type === 'app';
4338
const config = this._loadConfig(unit.path, filename, isApp ? undefined : appConfig, unit.type);

lib/loader/mixin/plugin.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,21 @@ module.exports = {
152152
configPaths = [ configPaths ];
153153
}
154154

155-
// read plugin.default.js and plugins.${env}.js
156-
if (this.serverEnv !== 'default') {
157-
// note: can't use for-of
158-
for (let i = 0, l = configPaths.length; i < l; i++) {
159-
const configPath = configPaths[i];
160-
configPaths.push(configPath.replace(/plugin\.default\.js$/, `plugin.${this.serverEnv}.js`));
155+
// Get all plugin configurations
156+
// plugin.default.js
157+
// plugin.${scope}.js
158+
// plugin.${env}.js
159+
// plugin.${scope}_${env}.js
160+
const newConfigPaths = [];
161+
for (const filename of this.getTypeFiles('plugin')) {
162+
for (let configPath of configPaths) {
163+
configPath = path.join(path.dirname(configPath), filename);
164+
newConfigPaths.push(configPath);
161165
}
162166
}
163167

164168
const plugins = {};
165-
for (let configPath of configPaths) {
169+
for (let configPath of newConfigPaths) {
166170
// let plugin.js compatible
167171
if (configPath.endsWith('plugin.default.js') && !fs.existsSync(configPath)) {
168172
configPath = configPath.replace(/plugin\.default\.js$/, 'plugin.js');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
from: 'default',
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
from: 'en',
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
from: 'en_prod',
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
from: 'prod',
5+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = {
4+
a: {
5+
enable: false,
6+
package: 'a',
7+
},
8+
b: {
9+
enable: true,
10+
package: 'b',
11+
},
12+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = {
4+
c: false,
5+
d: {
6+
enable: true,
7+
package: 'd',
8+
}
9+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
a: {
5+
enable: true,
6+
package: 'a',
7+
},
8+
};

0 commit comments

Comments
 (0)