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