Skip to content

Commit fdc1ee5

Browse files
authored
feat: add 'configWillLoad' hook to lifecycle (#187)
allow application, framework or plugin to modify config at last moment. Also move the windows ci from appveyor to travis.
1 parent 136ad7b commit fdc1ee5

11 files changed

Lines changed: 52 additions & 30 deletions

File tree

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# make sure git clone linebreak keep as \n on windows
2+
# https://eslint.org/docs/rules/linebreak-style
3+
*.js text eol=lf

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
sudo: false
2+
os:
3+
- linux
4+
- osx
5+
- windows
26
language: node_js
37
node_js:
48
- '8'
@@ -7,5 +11,5 @@ install:
711
- npm i npminstall && npminstall
812
script:
913
- npm run ci
10-
after_script:
11-
- npminstall codecov && codecov
14+
after_success:
15+
- npminstall codecov && codecov --disable=gcov -f .nyc_output/*.json

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
The MIT License (MIT)
1+
MIT License
22

3-
Copyright (c) Alibaba Group Holding Limited and other contributors.
3+
Copyright (c) 2016-present Alibaba Group Holding Limited and other contributors.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

appveyor.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/lifecycle.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ class Lifecycle extends EventEmitter {
129129
this[IS_CLOSED] = true;
130130
}
131131

132+
triggerConfigWillLoad() {
133+
for (const boot of this[BOOTS]) {
134+
if (boot.configWillLoad) {
135+
boot.configWillLoad();
136+
}
137+
}
138+
this.triggerConfigDidLoad();
139+
}
140+
132141
triggerConfigDidLoad() {
133142
for (const boot of this[BOOTS]) {
134143
if (boot.configDidLoad) {

lib/loader/mixin/custom.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ module.exports = {
3434
*/
3535
loadCustomApp() {
3636
this[LOAD_BOOT_HOOK]('app');
37-
this.lifecycle.triggerConfigDidLoad();
37+
this.lifecycle.triggerConfigWillLoad();
3838
},
3939

4040
/**
4141
* Load agent.js, same as {@link EggLoader#loadCustomApp}
4242
*/
4343
loadCustomAgent() {
4444
this[LOAD_BOOT_HOOK]('agent');
45-
this.lifecycle.triggerConfigDidLoad();
45+
this.lifecycle.triggerConfigWillLoad();
4646
},
4747

4848
// FIXME: no logger used after egg removed

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@
2424
"author": "gxcsoccer <gxcsoccer@126.com>",
2525
"license": "MIT",
2626
"bugs": {
27-
"url": "https://github.com/eggjs/egg-core/issues"
27+
"url": "https://github.com/eggjs/egg/issues"
2828
},
2929
"homepage": "https://github.com/eggjs/egg-core#readme",
3030
"engines": {
3131
"node": ">= 8.0.0"
3232
},
3333
"ci": {
34-
"version": "8, 10"
34+
"type": "travis",
35+
"os": {
36+
"travis": "linux, osx, windows"
37+
},
38+
"version": "8, 10",
39+
"afterScript": "after_success:\n - npminstall codecov && codecov --disable=gcov -f .nyc_output/*.json",
40+
"license": {
41+
"year": 2016
42+
}
3543
},
3644
"devDependencies": {
3745
"autod": "^3.0.1",

test/fixtures/boot/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ module.exports = class {
88
this.app = app;
99
}
1010

11+
configWillLoad() {
12+
this.app.config.appSet = true;
13+
}
14+
1115
configDidLoad() {
1216
this.app.bootLog.push('configDidLoad in app');
1317
}

test/fixtures/boot/app/plugin/boot-plugin/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22
const sleep = require('mz-modules/sleep');
3+
const assert = require('assert');
34

45
module.exports = app => {
56
app.bootLog.push('app.js in plugin');
7+
// make sure app.js change app.config.appSet = true on configWillLoad
8+
assert(app.config.appSet === true);
69
app.beforeStart(async () => {
710
await sleep(5);
811
app.bootLog.push('beforeStart');

test/loader/load_file.test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,36 @@ const mm = require('mm');
44
const assert = require('assert');
55
const utils = require('../utils');
66

7-
describe('test/load_file.test.js', function() {
7+
describe('test/load_file.test.js', () => {
88
let app;
99
afterEach(mm.restore);
1010
afterEach(() => app.close());
1111

12-
it('should load file', function() {
12+
it('should load file', () => {
1313
app = utils.createApp('load_file');
1414
const exports = app.loader.loadFile(utils.getFilepath('load_file/obj.js'));
1515
assert.deepEqual(exports, { a: 1 });
1616
});
1717

18-
it('should load file when exports is function', function() {
18+
it('should load file when exports is function', () => {
1919
app = utils.createApp('load_file');
2020
const exports = app.loader.loadFile(utils.getFilepath('load_file/function.js'), 1, 2);
2121
assert.deepEqual(exports, [ 1, 2 ]);
2222
});
2323

24-
it('should throw with filepath when file syntax error', function() {
24+
it('should throw with filepath when file syntax error', () => {
2525
assert.throws(() => {
2626
app = utils.createApp('syntaxerror');
2727
app.loader.loadCustomApp();
2828
}, /Parse Error: Unexpected token/);
2929
});
3030

31-
it('should load custom file', function() {
31+
it('should load custom file', () => {
3232
app = utils.createApp('load_file');
33-
const result = app.loader.loadFile(utils.getFilepath('load_file/no-js.yml')).toString();
33+
let result = app.loader.loadFile(utils.getFilepath('load_file/no-js.yml')).toString();
34+
if (process.platform === 'win32') {
35+
result = result.replace(/\r\n/g, '\n');
36+
}
3437
assert(result === '---\nmap:\n a: 1\n b: 2');
3538
});
3639
});

0 commit comments

Comments
 (0)