-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (77 loc) · 2.1 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Load Module Dependencies
*/
const crypto = require("crypto");
const fs = require('fs');
const moment = require("moment");
const notifier = require('node-notifier');
// CPCYCLE Class Definition
class CPCYCLE {
// Set Random Name if not supplied
constructor(name = `#CP${crypto.randomBytes(3).toString("hex")}`) {
this.name = name;
this.begin = null;
}
trace(method) {
return (...args) => {
let child = method(...args);
if(initialize.defaults.trace) {
// start trace
this._start_trace(child)
}
return child;
}
}
_start_trace(process) {
// Bind Handlers To Child Processes LifeCycle Events
process.on("close", this._onClose.bind(this));
process.on("message", this._onMessage.bind(this));
process.on("exit", this._onExit.bind(this));
process.on("disconnect", this._onDisconnect.bind(this));
process.stdout.on("data", this._onStdout.bind(this));
process.stderr.on("data", this._onStderr.bind(this));
}
_write(message, event) {
// Notifier based on preferences
if (initialize.defaults.events.includes(event)) {
let now = moment()
notifier.notify({
title: `${this.name} [${event}] ${this.begin.from(now, true)}`,
message: message,
icon: `${__dirname}/assets/icon.png`
})
}
}
_onClose() {
this._write("Process Closed", "Close")
}
_onMessage(data) {
this._write(data.toString(), "Message");
}
_onStdout(data) {
this._write(data.toString(), "Stdout");
}
_onStderr(data) {
this._write(data.toString(), "Stderr");
}
_onExit() {
this._write("Process Exited", "Exit")
}
_onDisconnect() {
this._write("Process Disconnected", "Disconnect");
}
}
function initialize(opts = {}) {
initialize.defaults = Object.assign({}, initialize.defaults, opts)
return (method, name) => {
const cpcyle = new CPCYCLE(name);
cpcyle.begin = moment();
return cpcyle.trace(method)
}
}
// Default Configurations
initialize.defaults = {
trace: true,
events: ["Close", "Stdout"]
}
module.exports = initialize;