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