UNPKG

1.58 kBJavaScriptView Raw
1/*
2 Provides access to the PM2 API in a way that won't break if
3 PM2 is not installed (globally) on the development machine.
4
5 This is useful when running tests within a CI environment.
6
7 ----------
8
9 See: http://pm2.keymetrics.io/docs/usage/pm2-api/
10*/
11import R from "ramda";
12import Promise from "bluebird";
13
14const stubPromise = () => new Promise(() => {});
15let pm2;
16let connectP = stubPromise;
17let listP = stubPromise;
18let deleteP = stubPromise;
19
20// Attempt to access PM2.
21try {
22 pm2 = require("pm2");
23
24 // Create promise versions of PM2 methods.
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 // Ignore.
32 /*eslint no-empty:0*/
33 } else {
34 throw e;
35 }
36}
37
38
39export default {
40 // PM2 API.
41 connect: () => connectP(),
42 list: () => listP(),
43 delete: (id) => deleteP(id),
44
45 /**
46 * Retrieves processes of running apps.
47 */
48 apps(filter) {
49 return new Promise((resolve) => {
50 Promise.coroutine(function*() {
51 let list = yield listP();
52 const isAppProcess = (item) => {
53 // Apps can be identified by having a port in the 5000 range,
54 // eg: <name>:5001
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};