-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathhelper.js
52 lines (44 loc) · 1.05 KB
/
helper.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
const fs = require('fs');
const yaml = require('js-yaml');
const mkdirp = require('mkdirp');
function random() {
return Math.random();
}
function now() {
return Date.now();
}
function isString(value) {
return typeof value === 'string' || value instanceof String;
}
function isObject(value) {
return typeof value === 'object' || value instanceof Object;
}
function environment() {
const environmentString = fs.readFileSync("./private/environment.yaml", "utf8")
return yaml.safeLoad(environmentString);
}
function logError(error) {
console.log(error.stack);
const dir = "./private/errors/" + environment['version'] + "/";
mkdirp(dir);
fs.writeFileSync(dir + now() + ".txt", error.stack, "utf8");
}
function setIntervalSafely(f, seconds) {
setInterval(() => {
try {
f()
} catch(e) {
logError(e)
}
}, seconds * 1000);
}
module.exports = {
random: random,
now: now,
isString: isString,
isObject: isObject,
environment : environment,
log: console.log,
logError:logError,
setIntervalSafely : setIntervalSafely
}