Skip to content

Commit

Permalink
feat: add 'configWillLoad' hook to lifecycle (#187)
Browse files Browse the repository at this point in the history
allow application, framework or plugin
to modify config at last moment.

Also move the windows ci from appveyor to travis.
  • Loading branch information
fengmk2 committed Oct 19, 2018
1 parent 136ad7b commit fdc1ee5
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# make sure git clone linebreak keep as \n on windows
# https://eslint.org/docs/rules/linebreak-style
*.js text eol=lf
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
sudo: false
os:
- linux
- osx
- windows
language: node_js
node_js:
- '8'
Expand All @@ -7,5 +11,5 @@ install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
after_success:
- npminstall codecov && codecov --disable=gcov -f .nyc_output/*.json
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)
MIT License

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

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
15 changes: 0 additions & 15 deletions appveyor.yml

This file was deleted.

9 changes: 9 additions & 0 deletions lib/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ class Lifecycle extends EventEmitter {
this[IS_CLOSED] = true;
}

triggerConfigWillLoad() {
for (const boot of this[BOOTS]) {
if (boot.configWillLoad) {
boot.configWillLoad();
}
}
this.triggerConfigDidLoad();
}

triggerConfigDidLoad() {
for (const boot of this[BOOTS]) {
if (boot.configDidLoad) {
Expand Down
4 changes: 2 additions & 2 deletions lib/loader/mixin/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ module.exports = {
*/
loadCustomApp() {
this[LOAD_BOOT_HOOK]('app');
this.lifecycle.triggerConfigDidLoad();
this.lifecycle.triggerConfigWillLoad();
},

/**
* Load agent.js, same as {@link EggLoader#loadCustomApp}
*/
loadCustomAgent() {
this[LOAD_BOOT_HOOK]('agent');
this.lifecycle.triggerConfigDidLoad();
this.lifecycle.triggerConfigWillLoad();
},

// FIXME: no logger used after egg removed
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@
"author": "gxcsoccer <gxcsoccer@126.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/eggjs/egg-core/issues"
"url": "https://github.com/eggjs/egg/issues"
},
"homepage": "https://github.com/eggjs/egg-core#readme",
"engines": {
"node": ">= 8.0.0"
},
"ci": {
"version": "8, 10"
"type": "travis",
"os": {
"travis": "linux, osx, windows"
},
"version": "8, 10",
"afterScript": "after_success:\n - npminstall codecov && codecov --disable=gcov -f .nyc_output/*.json",
"license": {
"year": 2016
}
},
"devDependencies": {
"autod": "^3.0.1",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/boot/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module.exports = class {
this.app = app;
}

configWillLoad() {
this.app.config.appSet = true;
}

configDidLoad() {
this.app.bootLog.push('configDidLoad in app');
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/boot/app/plugin/boot-plugin/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';
const sleep = require('mz-modules/sleep');
const assert = require('assert');

module.exports = app => {
app.bootLog.push('app.js in plugin');
// make sure app.js change app.config.appSet = true on configWillLoad
assert(app.config.appSet === true);
app.beforeStart(async () => {
await sleep(5);
app.bootLog.push('beforeStart');
Expand Down
15 changes: 9 additions & 6 deletions test/loader/load_file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@ const mm = require('mm');
const assert = require('assert');
const utils = require('../utils');

describe('test/load_file.test.js', function() {
describe('test/load_file.test.js', () => {
let app;
afterEach(mm.restore);
afterEach(() => app.close());

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

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

it('should throw with filepath when file syntax error', function() {
it('should throw with filepath when file syntax error', () => {
assert.throws(() => {
app = utils.createApp('syntaxerror');
app.loader.loadCustomApp();
}, /Parse Error: Unexpected token/);
});

it('should load custom file', function() {
it('should load custom file', () => {
app = utils.createApp('load_file');
const result = app.loader.loadFile(utils.getFilepath('load_file/no-js.yml')).toString();
let result = app.loader.loadFile(utils.getFilepath('load_file/no-js.yml')).toString();
if (process.platform === 'win32') {
result = result.replace(/\r\n/g, '\n');
}
assert(result === '---\nmap:\n a: 1\n b: 2');
});
});
5 changes: 4 additions & 1 deletion test/utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ describe('test/utils/index.test.js', () => {
});

it('should load no js file', () => {
const result = utils.loadFile(path.join(baseDir, 'no-js.yml')).toString();
let result = utils.loadFile(path.join(baseDir, 'no-js.yml')).toString();
if (process.platform === 'win32') {
result = result.replace(/\r\n/g, '\n');
}
assert(result === '---\nmap:\n a: 1\n b: 2');
});
});
Expand Down

0 comments on commit fdc1ee5

Please sign in to comment.