Skip to content

Commit

Permalink
feat: change assert to warning (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes authored and popomore committed Apr 9, 2018
1 parent 063185d commit 7f087e7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
4 changes: 4 additions & 0 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class EggLoader {
assert(this.options.app, 'options.app is required');
assert(this.options.logger, 'options.logger is required');

if (options.typescript && !require.extensions['.ts']) {
this.options.logger.warn('[egg:loader] `require.extensions` should contains `.ts` while `options.typescript` was true');
}

this.app = this.options.app;

/**
Expand Down
5 changes: 1 addition & 4 deletions lib/loader/file_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class FileLoader {
constructor(options) {
assert(options.directory, 'options.directory is required');
assert(options.target, 'options.target is required');
if (options.typescript) {
assert(require.extensions['.ts'], '`require.extensions` should contains `.ts` while `options.typescript` was true');
}
this.options = Object.assign({}, defaults, options);

// compatible old options _lowercaseFirst_
Expand Down Expand Up @@ -127,7 +124,7 @@ class FileLoader {
parse() {
let files = this.options.match;
if (!files) {
files = this.options.typescript
files = (this.options.typescript && require.extensions['.ts'])
? [ '**/*.(js|ts)', '!**/*.d.ts' ]
: [ '**/*.js' ];
} else {
Expand Down
15 changes: 13 additions & 2 deletions test/egg-ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const utils = require('./utils');
describe('test/egg-ts.test.js', () => {
let app;

before(() => {
beforeEach(() => {
require.extensions['.ts'] = require.extensions['.js'];
});

after(() => {
afterEach(() => {
delete require.extensions['.ts'];
});

Expand Down Expand Up @@ -85,4 +85,15 @@ describe('test/egg-ts.test.js', () => {
assert(app.serviceClasses.lord);
assert(!app.serviceClasses.test);
});

it('should not load ts files while typescript was true but no extensions', async () => {
delete require.extensions['.ts'];
app = utils.createApp('egg-ts-js', { typescript: true });

app.loader.loadApplicationExtend();
app.loader.loadService();
assert(!app.appExtend);
assert(app.serviceClasses.lord);
assert(!app.serviceClasses.test);
});
});
8 changes: 8 additions & 0 deletions test/loader/egg_loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,13 @@ describe('test/loader/egg_loader.test.js', () => {
mm(process.env, 'EGG_HOME', '/path/to/home');
assert(app.loader.getHomedir() === '/path/to/home');
});

it('should warning when typescript was true but no ts extension', done => {
mm(process.stdout, 'write', msg => {
assert(msg.includes('`require.extensions` should contains `.ts` while `options.typescript` was true'));
done();
});
utils.createApp('nothing', { typescript: true });
});
});
});
11 changes: 0 additions & 11 deletions test/loader/file_loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,6 @@ describe('test/loader/file_loader.test.js', () => {
}, /_private is not match 'a-z0-9_-' in _private.js/);
});

it('should throw when typescript was true but no ts extension', () => {
const mod = {};
assert.throws(() => {
new FileLoader({
directory: path.join(dirBase, 'error/dotdir'),
target: mod,
typescript: true,
}).load();
}, /`require.extensions` should contains `.ts` while `options.typescript` was true/);
});

describe('caseStyle', () => {
it('should load when caseStyle = upper', () => {
const target = {};
Expand Down

0 comments on commit 7f087e7

Please sign in to comment.