| 54 | class API { |
| 55 | |
| 56 | constructor (opts) { |
| 57 | if (!opts) opts = {}; |
| 58 | var that = this; |
| 59 | |
| 60 | this.daemon_mode = typeof(opts.daemon_mode) == 'undefined' ? true : opts.daemon_mode; |
| 61 | this.pm2_home = conf.PM2_ROOT_PATH; |
| 62 | this.public_key = conf.PUBLIC_KEY || opts.public_key || null; |
| 63 | this.secret_key = conf.SECRET_KEY || opts.secret_key || null; |
| 64 | this.machine_name = conf.MACHINE_NAME || opts.machine_name || null |
| 65 | |
| 66 | /** |
| 67 | * CWD resolution |
| 68 | */ |
| 69 | this.cwd = process.cwd(); |
| 70 | if (opts.cwd) { |
| 71 | this.cwd = path.resolve(opts.cwd); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * PM2 HOME resolution |
| 76 | */ |
| 77 | if (opts.pm2_home && opts.independent == true) |
| 78 | throw new Error('You cannot set a pm2_home and independent instance in same time'); |
| 79 | |
| 80 | if (opts.pm2_home) { |
| 81 | // Override default conf file |
| 82 | this.pm2_home = opts.pm2_home; |
| 83 | conf = Object.assign(conf, path_structure(this.pm2_home)); |
| 84 | } |
| 85 | else if (opts.independent == true && conf.IS_WINDOWS === false) { |
| 86 | // Create an unique pm2 instance |
| 87 | const crypto = require('crypto'); |
| 88 | var random_file = crypto.randomBytes(8).toString('hex'); |
| 89 | this.pm2_home = path.join('/tmp', random_file); |
| 90 | |
| 91 | // If we dont explicitly tell to have a daemon |
| 92 | // It will go as in proc |
| 93 | if (typeof(opts.daemon_mode) == 'undefined') |
| 94 | this.daemon_mode = false; |
| 95 | conf = Object.assign(conf, path_structure(this.pm2_home)); |
| 96 | } |
| 97 | |
| 98 | this._conf = conf; |
| 99 | |
| 100 | if (conf.IS_WINDOWS) { |
| 101 | // Weird fix, may need to be dropped |
| 102 | // @todo windows connoisseur double check |
| 103 | if (process.stdout._handle && process.stdout._handle.setBlocking) |
| 104 | process.stdout._handle.setBlocking(true); |
| 105 | } |
| 106 | |
| 107 | this.Client = new Client({ |
| 108 | pm2_home: that.pm2_home, |
| 109 | conf: this._conf, |
| 110 | secret_key: this.secret_key, |
| 111 | public_key: this.public_key, |
| 112 | daemon_mode: this.daemon_mode, |
| 113 | machine_name: this.machine_name |