-
Notifications
You must be signed in to change notification settings - Fork 280
/
configuration.js
162 lines (144 loc) · 5.02 KB
/
configuration.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const clone = require('clone');
const { EventEmitter } = require('events');
const logger = require('./logger');
const reporterOutputLogger = require('./reporters/reporterOutputLogger');
function coerceToArray(value) {
if (Array.isArray(value)) {
return value;
} if (typeof value === 'string') {
return [value];
} if ((value == null)) {
return [];
}
return value;
}
function applyLoggingOptions(options) {
if (options.color === false) {
logger.transports.console.colorize = false;
reporterOutputLogger.transports.console.colorize = false;
}
// Handling the 'loglevel' value
if (options.loglevel) {
const loglevel = options.loglevel.toLowerCase();
if (loglevel === 'silent') {
logger.transports.console.silent = true;
} else if (loglevel === 'warning') {
logger.transports.console.level = 'warn';
} else if (loglevel === 'debug') {
logger.transports.console.level = 'debug';
logger.transports.console.timestamp = true;
} else if (['warn', 'error'].includes(loglevel)) {
logger.transports.console.level = loglevel;
} else {
throw new Error(`Unsupported logging level: '${loglevel}'`);
}
} else {
logger.transports.console.level = 'warn';
}
}
function errorOnRemovedOptions(config = {}) {
if (config.options) {
if (config.options.c) {
throw new Error('Dredd does not support '
+ '-c anymore, use --color/--no-color instead');
}
if (typeof config.options.color === 'string') {
throw new Error('Dredd does not support '
+ `--color=${config.options.color} anymore, use --color/--no-color instead`);
}
if (config.options.level) {
throw new Error('Dredd does not support '
+ '--level anymore, use --loglevel instead');
}
if (config.options.timestamp || config.options.t) {
throw new Error('Dredd does not support '
+ '--timestamp anymore, use --loglevel=debug instead');
}
if (config.options.silent || config.options.q) {
throw new Error('Dredd does not support '
+ '-q/--silent anymore, use --loglevel=silent instead');
}
if (config.options.sandbox || config.options.b) {
throw new Error('Dredd does not support '
+ 'sandboxed JS hooks anymore, use standard JS hooks instead');
}
}
if (config.hooksData) {
throw new Error('Dredd does not support '
+ 'sandboxed JS hooks anymore, use standard JS hooks instead');
}
if (config.blueprintPath) {
throw new Error('Dredd does not support '
+ "the 'blueprintPath' option anymore, use 'path' instead");
}
}
function applyConfiguration(config) {
const configuration = {
server: null,
emitter: new EventEmitter(),
custom: { // Used for custom settings of various APIs or reporters
// Keep commented-out, so these values are actually set by CLI
// cwd: process.cwd()
},
options: {
'dry-run': false,
reporter: null,
output: null,
debug: false,
header: null,
user: null,
'inline-errors': false,
details: false,
method: [],
only: [],
color: true,
loglevel: 'warning',
sorted: false,
names: false,
hookfiles: null,
language: 'nodejs',
'hooks-worker-timeout': 5000,
'hooks-worker-connect-timeout': 1500,
'hooks-worker-connect-retry': 500,
'hooks-worker-after-connect-wait': 100,
'hooks-worker-term-timeout': 5000,
'hooks-worker-term-retry': 500,
'hooks-worker-handler-host': '127.0.0.1',
'hooks-worker-handler-port': 61321,
},
};
// Normalize options and config
for (const key of Object.keys(config || {})) {
// Copy anything except "custom" hash
const value = config[key];
if (key !== 'custom') {
configuration[key] = value;
} else {
if (!configuration.custom) { configuration.custom = {}; }
const object = config.custom || {};
for (const customKey of Object.keys(object || {})) {
const customVal = object[customKey];
configuration.custom[customKey] = clone(customVal, false);
}
}
}
// Coerce single/multiple options into an array
configuration.options.reporter = coerceToArray(configuration.options.reporter);
configuration.options.output = coerceToArray(configuration.options.output);
configuration.options.header = coerceToArray(configuration.options.header);
configuration.options.method = coerceToArray(configuration.options.method);
configuration.options.only = coerceToArray(configuration.options.only);
configuration.options.path = coerceToArray(configuration.options.path);
configuration.options.method = configuration.options.method.map(method => method.toUpperCase());
if (configuration.options.user) {
const authHeader = `Authorization:Basic ${Buffer.from(configuration.options.user).toString('base64')}`;
configuration.options.header.push(authHeader);
}
applyLoggingOptions(configuration.options);
errorOnRemovedOptions(config);
return configuration;
}
module.exports = {
applyConfiguration,
applyLoggingOptions,
};