UNPKG

2.27 kBJavaScriptView Raw
1import treeKill from 'tree-kill';
2import { StrykerError } from '@stryker-mutator/util';
3export const objectUtils = {
4 /**
5 * Calls a defined callback function on each element of a map, and returns an array that contains the results.
6 *
7 * @param subject The map to act on
8 * @param callbackFn The callback fn
9 * @returns
10 */
11 map(subject, callbackFn) {
12 const results = [];
13 subject.forEach((value, key) => results.push(callbackFn(value, key)));
14 return results;
15 },
16 /**
17 * A wrapper around `process.env` (for testability)
18 */
19 getEnvironmentVariable(nameEnvironmentVariable) {
20 return process.env[nameEnvironmentVariable];
21 },
22 undefinedEmptyString(str) {
23 if (str) {
24 return str;
25 }
26 return undefined;
27 },
28 getEnvironmentVariableOrThrow(name) {
29 const value = this.getEnvironmentVariable(name);
30 if (value === undefined) {
31 throw new StrykerError(`Missing environment variable "${name}"`);
32 }
33 else {
34 return value;
35 }
36 },
37 isWarningEnabled(warningType, warningOptions) {
38 if (typeof warningOptions === 'boolean') {
39 return warningOptions;
40 }
41 else {
42 return !!warningOptions[warningType];
43 }
44 },
45 /**
46 * A wrapper around `process.exitCode = n` (for testability)
47 */
48 setExitCode(n) {
49 process.exitCode = n;
50 },
51 kill(pid) {
52 return new Promise((res, rej) => {
53 treeKill(pid, 'SIGKILL', (err) => {
54 if (err && !canIgnore(err.code)) {
55 rej(err);
56 }
57 else {
58 res();
59 }
60 });
61 function canIgnore(code) {
62 // https://docs.microsoft.com/en-us/windows/desktop/Debug/system-error-codes--0-499-
63 // these error codes mean the program is _already_ closed.
64 return code === 255 || code === 128;
65 }
66 });
67 },
68 /**
69 * Creates a random integer number.
70 * @returns A random integer.
71 */
72 random() {
73 return Math.ceil(Math.random() * 10000000);
74 },
75};
76//# sourceMappingURL=object-utils.js.map
\No newline at end of file