1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const fs = require('fs');
|
5 |
|
6 | const constants = require('../constants/index');
|
7 |
|
8 | const { replaceRootDirInPath } = require('./replaceRootDirInPath');
|
9 |
|
10 | function getEnvOptions() {
|
11 | const options = {};
|
12 |
|
13 | for (let name in constants.ENVIRONMENT_CONFIG_MAP) {
|
14 | if (process.env[name]) {
|
15 | options[constants.ENVIRONMENT_CONFIG_MAP[name]] = process.env[name];
|
16 | }
|
17 | }
|
18 |
|
19 | return options;
|
20 | }
|
21 |
|
22 | function getAppOptions(pathToResolve) {
|
23 | const initialPath = pathToResolve;
|
24 |
|
25 | let traversing = true;
|
26 |
|
27 |
|
28 | while(traversing) {
|
29 | traversing = pathToResolve !== path.sep;
|
30 |
|
31 | const pkgpath = path.join(pathToResolve, 'package.json');
|
32 |
|
33 | if (fs.existsSync(pkgpath)) {
|
34 | let options = (require(pkgpath) || {})['jest-junit'];
|
35 |
|
36 | if (Object.prototype.toString.call(options) !== '[object Object]') {
|
37 | options = {};
|
38 | }
|
39 |
|
40 | return options;
|
41 | } else {
|
42 | pathToResolve = path.dirname(pathToResolve);
|
43 | }
|
44 | }
|
45 |
|
46 | return {};
|
47 | }
|
48 |
|
49 | function replaceRootDirInOutput(rootDir, output) {
|
50 | return rootDir !== null ? replaceRootDirInPath(rootDir, output) : output;
|
51 | }
|
52 |
|
53 | module.exports = {
|
54 | options: (reporterOptions = {}) => {
|
55 | return Object.assign({}, constants.DEFAULT_OPTIONS, reporterOptions, getAppOptions(process.cwd()), getEnvOptions());
|
56 | },
|
57 | getAppOptions: getAppOptions,
|
58 | getEnvOptions: getEnvOptions,
|
59 | replaceRootDirInOutput: replaceRootDirInOutput
|
60 | };
|