-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
47 lines (46 loc) · 2.13 KB
/
utils.js
File metadata and controls
47 lines (46 loc) · 2.13 KB
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
const path = require('path'),
fs = require('fs'),
Promise = require('bluebird');
module.exports = {
buildLogGroupDir: (logGroupName) => {
const dir = path.join(__dirname, 'logs', logGroupName.replace('/', '-').replace(/[^a-z0-9\-]/ig, ''))
console.debug(`output directory = ${dir}`);
// Create directories if they doesnt exist.
if (!fs.existsSync(path.join(__dirname, 'logs'))) {
fs.mkdirSync(path.join(__dirname, 'logs'));
}
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir;
},
stamp: t => `${("000" + t.year()).slice(-4)}-${("00" + (t.month() + 1)).slice(-2)}-${("00" + (t.date())).slice(-2)}-${("00" + (t.hour())).slice(-2)}:${("00" + (t.minute())).slice(-2)}:${("00" + (t.second())).slice(-2)}`,
buildRangedFileName: (startTimeStamp, endTimeStamp, extension = 'json') => `${startTimeStamp.replace(/[^a-z0-9\-]/g, '')}_to_${endTimeStamp.replace(/[^a-z0-9\-]/g, '')}.${extension}`,
extractTime: timestamp => new Date(timestamp.value).getTime(),
extractMessage: m = message => message.value,
compareTimeStamps: (a, b) => {
if (a.timestamp < b.timestamp) { // a is less than b by some ordering criterion
return -1;
}
if (a.timestamp > b.timestamp) { // a is greater than b by the ordering criterion
return 1;
}
// a must be equal to b
return 0;
},
promiseWhile: async function promiseWhile(condition, action) {
return new Promise(function (resolve, reject) {
const loop = function () {
if (!condition()) {
return resolve();
}
return Promise.cast(action()) // A way of casting incoming thenables or promise subclasses to promises of the exact class specified, so that the resulting object's then is ensured to have the behavior of the constructor you are calling cast on.
.then(loop)
.catch(function (e) {
reject(e);
});
};
process.nextTick(loop);
});
}
}