forked from humanmade/trafficator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
33 lines (32 loc) · 833 Bytes
/
utils.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
const fs = require("fs");
const chalk = require("chalk");
module.exports = {
getConfig: file => {
const defaultConfig = require("./config");
try {
fs.accessSync(file, fs.constants.R_OK);
return Object.assign({}, defaultConfig, require(file));
} catch (err) {
console.log(chalk.red(`Unable to locate the config file "${file}"`));
process.exit(1);
}
},
getRandom: value => {
if (Array.isArray(value)) {
if (value.length) {
return value[Math.floor(Math.random() * value.length)];
}
return null;
}
return value;
},
resolve: async (value, page = null) => {
if (typeof value === "function" && page) {
return await value(page);
}
return value;
},
log: function() {
console.log(...arguments, new Date().toISOString());
}
};