UNPKG

1.6 kBJavaScriptView Raw
1/**
2 * Retrieve the value of a user supplied option.
3 * Falls back to `defaultValue`
4 * Order of precedence
5 * 1. User-supplied option
6 * 2. Environment variable
7 * 3. Default value
8 *
9 * @param {string} optToGet Option name
10 * @param {object} options User supplied options object
11 * @param {boolean} isBool Treat option as Boolean
12 * @param {string|boolean} defaultValue Fallback value
13 *
14 * @return {string|boolean} Option value
15 */
16function _getOption(optToGet, options, isBool, defaultValue) {
17 const envVar = `MOCHAWESOME_${optToGet.toUpperCase()}`;
18 if (options && typeof options[optToGet] !== 'undefined') {
19 return isBool && typeof options[optToGet] === 'string'
20 ? options[optToGet] === 'true'
21 : options[optToGet];
22 }
23 if (typeof process.env[envVar] !== 'undefined') {
24 return isBool ? process.env[envVar] === 'true' : process.env[envVar];
25 }
26 return defaultValue;
27}
28
29module.exports = function (opts) {
30 const reporterOpts = (opts && opts.reporterOptions) || {};
31 const code = _getOption('code', reporterOpts, true, true);
32 const noCode = _getOption('no-code', reporterOpts, true, false);
33
34 return {
35 quiet: _getOption('quiet', reporterOpts, true, false),
36 reportFilename: _getOption(
37 'reportFilename',
38 reporterOpts,
39 false,
40 'mochawesome'
41 ),
42 saveHtml: _getOption('html', reporterOpts, true, true),
43 saveJson: _getOption('json', reporterOpts, true, true),
44 consoleReporter: _getOption('consoleReporter', reporterOpts, false, 'spec'),
45 useInlineDiffs: !!opts.inlineDiffs,
46 code: noCode ? false : code,
47 };
48};