This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (73 loc) · 2.77 KB
/
index.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
const util = require('util');
const fs = require('fs');
const path = require('path');
module.exports = async function(overwrite, logPath) {
const currentDir = process.cwd();
if (logPath) {
async function createPath() {
if (!fs.existsSync(path.join(currentDir, logPath))) {
const splitPath = logPath.split('/');
if (splitPath.length > 1) {
if (!fs.existsSync(path.join(currentDir, splitPath[0]))) splitPath.splice(0, 1);
let finishedDirectory = [];
await splitPath.forEach(file => {
fs.mkdir(path.join(currentDir, finishedDirectory.join('/'), file), () => {});
finishedDirectory.push(file);
});
} else {
fs.mkdir(path.join(currentDir, logPath));
}
}
if (!fs.existsSync(path.join(currentDir, logPath, '/stdout.log'))) {
fs.writeFile(path.join(currentDir, logPath, 'stdout.log'), '', err => {
if (err) console.error(err);
});
fs.writeFile(path.join(currentDir, logPath, 'stderr.log'), '', err => {
if (err) console.error(err);
});
}
}
await createPath();
log = message =>
fs.appendFile(path.join(currentDir, logPath, 'stdout.log'), message, err =>
err ? console.error(err) : {}
);
log(
`----------------------- New Process [${new Date().toLocaleDateString(
Date.now()
)} ${new Date().toLocaleTimeString(Date.now())}] -----------------------\n`
);
}
if (overwrite) {
/**
* @param {String | Object | Array | Number | Boolean} message The message you want to send to the console
* @return {Undefined} Nothing
*/
console.log = message => {
const logMessage = `[${new Date().toLocaleDateString(Date.now())} ${new Date().toLocaleTimeString(
Date.now()
)}]: ${
typeof message === 'object' ? '\n' + util.inspect(message, false, 2, true) + '\n\n' : message + '\n'
}`;
const logMessageStandard = `[${new Date().toLocaleDateString(Date.now())} ${new Date().toLocaleTimeString(
Date.now()
)}]: ${typeof message === 'object' ? '\n' + util.inspect(message) + '\n\n' : message + '\n'}`;
process.stdout.write(logMessage);
if (logPath) log(logMessageStandard);
};
}
/**
* @param {String | Object | Array | Number | Boolean} message The message you want to send to the console
* @return {Undefined} Nothing
*/
return message => {
const logMessage = `[${new Date().toLocaleDateString(Date.now())} ${new Date().toLocaleTimeString(
Date.now()
)}]: ${typeof message === 'object' ? '\n' + util.inspect(message, false, 2, true) + '\n\n' : message + '\n'}`;
const logMessageStandard = `[${new Date().toLocaleDateString(Date.now())} ${new Date().toLocaleTimeString(
Date.now()
)}]: ${typeof message === 'object' ? '\n' + util.inspect(message) + '\n\n' : message + '\n'}`;
process.stdout.write(logMessage);
if (logPath) log(logMessageStandard);
};
};