1 | const debug = require('util').debuglog('egg-mock:prerequire');
|
2 | const path = require('path');
|
3 | const { existsSync } = require('fs');
|
4 | const globby = require('globby');
|
5 |
|
6 | const cwd = process.cwd();
|
7 | const dirs = [];
|
8 | if (existsSync(path.join(cwd, 'app'))) {
|
9 | dirs.push('app/**/*.js');
|
10 | }
|
11 |
|
12 | if (existsSync(path.join(cwd, 'config'))) {
|
13 | dirs.push('config/**/*.js');
|
14 | }
|
15 | const files = globby.sync(dirs, { cwd });
|
16 |
|
17 | for (const file of files) {
|
18 | const filepath = path.join(cwd, file);
|
19 | try {
|
20 | debug('%s prerequire %s', process.pid, filepath);
|
21 | require(filepath);
|
22 | } catch (err) {
|
23 | debug('prerequire error %s', err.message);
|
24 | }
|
25 | }
|