1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import R from "ramda";
|
12 | import Promise from "bluebird";
|
13 |
|
14 | const stubPromise = () => new Promise(() => {});
|
15 | let pm2;
|
16 | let connectP = stubPromise;
|
17 | let listP = stubPromise;
|
18 | let deleteP = stubPromise;
|
19 |
|
20 |
|
21 | try {
|
22 | pm2 = require("pm2");
|
23 |
|
24 |
|
25 | connectP = Promise.promisify(pm2.connect);
|
26 | listP = Promise.promisify(pm2.list);
|
27 | deleteP = Promise.promisify(pm2.delete);
|
28 |
|
29 | } catch (e) {
|
30 | if (e.code === "MODULE_NOT_FOUND") {
|
31 |
|
32 |
|
33 | } else {
|
34 | throw e;
|
35 | }
|
36 | }
|
37 |
|
38 |
|
39 | export default {
|
40 |
|
41 | connect: () => connectP(),
|
42 | list: () => listP(),
|
43 | delete: (id) => deleteP(id),
|
44 |
|
45 | |
46 |
|
47 |
|
48 | apps(filter) {
|
49 | return new Promise((resolve) => {
|
50 | Promise.coroutine(function*() {
|
51 | let list = yield listP();
|
52 | const isAppProcess = (item) => {
|
53 |
|
54 |
|
55 | const parts = item.name.split(":");
|
56 | return parts.length < 2
|
57 | ? false
|
58 | : parts[1].startsWith("5");
|
59 | };
|
60 | list = R.filter(isAppProcess, list);
|
61 | if (filter) { list = R.filter(filter, list); }
|
62 | resolve(list);
|
63 | })();
|
64 | });
|
65 | }
|
66 | };
|