-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (31 loc) · 1015 Bytes
/
Copy pathindex.js
File metadata and controls
35 lines (31 loc) · 1015 Bytes
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
const API = require('pm2').custom;
const descriptors = Object.getOwnPropertyDescriptors(API.prototype);
Object.keys(descriptors).forEach(name => {
const descriptor = descriptors[name];
if (/^[a-z]/.test(name) && typeof descriptor.value === 'function') {
const method = descriptor.value;
descriptor.value = function (...args) {
// If last argument is function then we have callback
if (typeof args[args.length - 1] === 'function') {
return method.apply(this, args);
} else {
return new Promise((resolve, reject) => {
args.push((error, value) => {
if (error) {
reject(error);
} else {
resolve(value);
}
});
return method.apply(this, args);
}).catch(error => {
console.error(error);
throw error;
});
}
};
Object.defineProperty(API.prototype, name, descriptor);
}
});
module.exports = new API;
module.exports.custom = API;