-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract handleRequires() to file
- Loading branch information
Showing
5 changed files
with
51 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const debug = require('debug')('mocha:cli:handle:requires'); | ||
const {requireOrImport} = require('../nodejs/esm-utils'); | ||
const PluginLoader = require('../plugin-loader'); | ||
|
||
/** | ||
* `require()` the modules as required by `--require <require>`. | ||
* | ||
* Returns array of `mochaHooks` exports, if any. | ||
* @param {string[]} requires - Modules to require | ||
* @returns {Promise<object>} Plugin implementations | ||
* @private | ||
*/ | ||
exports.handleRequires = async (requires = [], {ignoredPlugins = []} = {}) => { | ||
const pluginLoader = PluginLoader.create({ignore: ignoredPlugins}); | ||
for await (const mod of requires) { | ||
let modpath = mod; | ||
// this is relative to cwd | ||
if (fs.existsSync(mod) || fs.existsSync(`${mod}.js`)) { | ||
modpath = path.resolve(mod); | ||
debug('resolved required file %s to %s', mod, modpath); | ||
} | ||
const requiredModule = await requireOrImport(modpath); | ||
if (requiredModule && typeof requiredModule === 'object') { | ||
if (pluginLoader.load(requiredModule)) { | ||
debug('found one or more plugin implementations in %s', modpath); | ||
} | ||
} | ||
debug('loaded required module "%s"', mod); | ||
} | ||
const plugins = await pluginLoader.finalize(); | ||
if (Object.keys(plugins).length) { | ||
debug('finalized plugin implementations: %O', plugins); | ||
} | ||
return plugins; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters