UNPKG

685 BJavaScriptView Raw
1const debug = require('util').debuglog('egg-mock:prerequire');
2const path = require('path');
3const { existsSync } = require('fs');
4const globby = require('globby');
5
6const cwd = process.cwd();
7const dirs = [];
8if (existsSync(path.join(cwd, 'app'))) {
9 dirs.push('app/**/*.js');
10}
11// avoid Error: ENOENT: no such file or directory, scandir
12if (existsSync(path.join(cwd, 'config'))) {
13 dirs.push('config/**/*.js');
14}
15const files = globby.sync(dirs, { cwd });
16
17for (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}