Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
{
"extends": "eslint-config-egg",
"rules": {
"no-console": "off",
"no-magic-numbers": "off",
"generator-star-spacing": "off",
"prefer-template": "off",
"max-len": "off",
"dot-notation": "off"
}
"extends": "eslint-config-egg"
}
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/node_modules
coverage
.logs
test
npm-debug.log
npm-debug.log
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ install:
script:
- npm run ci
after_script:
- npm i codecov && codecov
- npminstall codecov && codecov
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ environment:

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && npminstall
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
Expand Down
5 changes: 2 additions & 3 deletions lib/base_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const loading = require('loading');
const interopRequire = require('interop-require');
const debug = require('debug')('egg:loader');

/**
* Loader 基类
*/
class EggLoader {

/**
Expand Down Expand Up @@ -248,6 +245,8 @@ class EggLoader {
const eggPath = this.eggPath;
const frameworkPaths = [];

addEggPath(this.options.customEgg);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个应该加回来

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个主要是给单测时候用的?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是,默认入口要加上,会去重的。


// 遍历整个原型链,获取原型链上所有的 eggPath
// 越核心的优先级越高
let proto = this.app;
Expand Down
6 changes: 4 additions & 2 deletions lib/plugin_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,18 @@ module.exports = {
}
}

const logger = this.options.logger;
if (!config) {
logger.warn(`[egg:loader] pkg.eggPlugin is missing in ${pluginPackage}`);
return;
}

if (config.name && config.name !== plugin.name) {
// TODO: 兼容提示,1.1 改成必须配置 name
// pluginName 为 config/plugin.js 配置的插件名
// pluginConfigName 为 pkg.eggPath.name
this.options.logger.warn(`[egg:loader] pluginName(${plugin.name}) is different from pluginConfigName(${config.name})`);
logger.warn(`[egg:loader] pluginName(${plugin.name}) is different from pluginConfigName(${config.name})`);
}

for (const key of [ 'dep', 'env' ]) {
if (!plugin[key].length && Array.isArray(config[key])) {
plugin[key] = config[key];
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"devDependencies": {
"egg-bin": "1",
"egg-ci": "1",
"eslint": "3",
"eslint": "~3.1.0",
"eslint-config-egg": "3",
"koa": "1",
"koa-router": "4",
Expand All @@ -48,4 +48,4 @@
"is-type-of": "^1.0.0",
"loading": "^1.12.0"
}
}
}
118 changes: 118 additions & 0 deletions test/egg_loader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict';

require('should');
const path = require('path');
const mm = require('mm');
const utils = require('./utils');
const BaseLoader = require('../index');

describe('test/egg_loader.test.js', function() {

afterEach(mm.restore);

describe('.getServerEnv', function() {

it('should get from env EGG_SERVER_ENV', function() {
mm(process.env, 'EGG_SERVER_ENV', 'prod');
const app = utils.createApp('serverenv');
app.loader.serverEnv.should.equal('prod');
});

it('should use unittest when NODE_ENV = test', function() {
mm(process.env, 'NODE_ENV', 'test');
const app = utils.createApp('serverenv');
app.loader.serverEnv.should.equal('unittest');
});

it('should use default when NODE_ENV = production', function() {
mm(process.env, 'NODE_ENV', 'production');
const app = utils.createApp('serverenv');
app.loader.serverEnv.should.equal('default');
});

it('should use local when NODE_ENV is other', function() {
mm(process.env, 'NODE_ENV', 'development');
const app = utils.createApp('serverenv');
app.loader.serverEnv.should.equal('local');
});

it('should get from config/serverEnv', function() {
mm(process.env, 'NODE_ENV', 'production');
mm(process.env, 'EGG_SERVER_ENV', 'test');
const app = utils.createApp('serverenv-file');
app.loader.serverEnv.should.equal('prod');
});
});


describe('eggPaths', function() {

it('should get from paramter', function() {
const app = utils.createApp('eggpath');
app.loader.eggPath.should.equal(utils.getFilepath('egg'));
});

it('should get from framework', function() {
const Application = require('./fixtures/framework');
const app = new Application();
app.coreLogger = console;
app.loader = new utils.Loader('eggpath', { app });
app.loader.loadConfig();
app.loader.eggPaths.should.eql([
utils.getFilepath('egg'),
utils.getFilepath('framework/node_modules/framework2'),
utils.getFilepath('framework'),
]);
return app;
});

it('should get from framework using symbol', function() {
const Application = require('./fixtures/framework-symbol');
const app = new Application();
app.coreLogger = console;
app.loader = new utils.Loader('eggpath', { app });
app.loader.loadConfig();
app.loader.eggPaths.should.eql([
utils.getFilepath('egg'),
utils.getFilepath('framework-symbol/node_modules/framework2'),
utils.getFilepath('framework-symbol'),
]);
return app;
});

it('frameworkPaths should not container eggPath', function() {
const eggPath = path.join(__dirname, 'fixtures/egg');
const loader = new BaseLoader({
baseDir: path.join(__dirname, 'fixtures/eggpath'),
eggPath,
customEgg: eggPath,
});
loader.frameworkPaths.should.not.containEql(eggPath);
});
});


describe('loadDirs', function() {

it('should get plugin dir', function() {
const app = utils.createApp('plugin');
const dirs = app.loader.loadDirs();
dirs.length.should.eql(10);
});

it('should not get plugin dir', function() {
const loader = new utils.Loader('plugin');
const dirs = loader.loadDirs();
dirs.length.should.eql(2);
});
});


describe('loadFile', function() {
it('should throw with filepath when file syntax error', function() {
(function() {
utils.createApp('syntaxerror');
}).should.throw(/test\/fixtures\/syntaxerror\/app\.js error:/);
});
});
});
6 changes: 6 additions & 0 deletions test/fixtures/agent/app/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
foo: 'agent bar',
bar: 'foo'
};
3 changes: 3 additions & 0 deletions test/fixtures/agent/app/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = {};
6 changes: 6 additions & 0 deletions test/fixtures/agent/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
a: true,
b: true,
};
6 changes: 6 additions & 0 deletions test/fixtures/agent/node_modules/a/app/agent.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/fixtures/agent/node_modules/a/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/fixtures/agent/node_modules/b/app/agent.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/fixtures/agent/node_modules/b/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/agent/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "application"
}
13 changes: 13 additions & 0 deletions test/fixtures/ali-plugin/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const path = require('path');

exports.foo = {
enable: true,
type: ['ali'],
path: path.join(__dirname, '../../../plugins/test-me')
};

exports.fooalipay = {
enable: true,
type: ['alipay'],
path: path.join(__dirname, '../../../plugins/test-me2')
};
6 changes: 6 additions & 0 deletions test/fixtures/ali-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ali-plugin",
"chair": {
"type": "ali"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/application/app/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
foo: 'app bar',
bar: 'foo'
};
3 changes: 3 additions & 0 deletions test/fixtures/application/app/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = {};
6 changes: 6 additions & 0 deletions test/fixtures/application/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
a: true,
b: true,
};
6 changes: 6 additions & 0 deletions test/fixtures/application/node_modules/a/app/application.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/fixtures/application/node_modules/b/app/application.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/application/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "application"
}
5 changes: 5 additions & 0 deletions test/fixtures/config-env/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
egg: 'config-default',
};
3 changes: 3 additions & 0 deletions test/fixtures/config-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "config-env"
}
11 changes: 11 additions & 0 deletions test/fixtures/config/app/controller/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var util = require('./util/a');
module.exports = function() {
return {
a: function*() {
util.b();
this.body = 'hello';
}
};
};
5 changes: 5 additions & 0 deletions test/fixtures/config/app/controller/util/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
a: 1,
b: function() {
}
};
10 changes: 10 additions & 0 deletions test/fixtures/config/app/services/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
var util = require('./util/bar');

module.exports = function() {
return {
bar: function*(ctx) {
console.log(ctx);
}
};
};
1 change: 1 addition & 0 deletions test/fixtures/config/app/services/util/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.a = 1;
14 changes: 14 additions & 0 deletions test/fixtures/config/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

exports.loader = {
service: { ignore: 'util/**' },
controller: { ignore: 'util/**' }
};

exports.name = 'config-test';

exports.test = 1;

exports.urllib = {
keepAlive: false
};
3 changes: 3 additions & 0 deletions test/fixtures/config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "loader_config"
}
13 changes: 13 additions & 0 deletions test/fixtures/custom-app/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

module.exports = function(app) {
app.get('/', function*() {
this.body = {
customFoo: app.customFoo,
env: app.config.env,
eggPaths: app.loader.eggPaths,
frameworkPaths: app.loader.frameworkPaths,
eggPath: app.loader.eggPath,
};
});
};
Loading