1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var fclone = require('fclone');
|
12 | var fs = require('fs');
|
13 | var cst = require('../constants.js');
|
14 | var waterfall = require('async/waterfall');
|
15 | var util = require('util');
|
16 | var url = require('url');
|
17 | var dateFns = require('date-fns')
|
18 | var findPackageJson = require('./tools/find-package-json')
|
19 |
|
20 | var Utility = module.exports = {
|
21 | findPackageVersion : function(fullpath) {
|
22 | var version
|
23 |
|
24 | try {
|
25 | version = findPackageJson(fullpath).next().value.version
|
26 | } catch(e) {
|
27 | version = 'N/A'
|
28 | }
|
29 | return version
|
30 | },
|
31 | getDate : function() {
|
32 | return Date.now();
|
33 | },
|
34 | extendExtraConfig : function(proc, opts) {
|
35 | if (opts.env && opts.env.current_conf) {
|
36 | if (opts.env.current_conf.env &&
|
37 | typeof(opts.env.current_conf.env) === 'object' &&
|
38 | Object.keys(opts.env.current_conf.env).length === 0)
|
39 | delete opts.env.current_conf.env
|
40 |
|
41 | Utility.extendMix(proc.pm2_env, opts.env.current_conf);
|
42 | delete opts.env.current_conf;
|
43 | }
|
44 | },
|
45 | formatCLU : function(process) {
|
46 | if (!process.pm2_env) {
|
47 | return process;
|
48 | }
|
49 |
|
50 | var obj = Utility.clone(process.pm2_env);
|
51 | delete obj.env;
|
52 |
|
53 | return obj;
|
54 | },
|
55 | extend : function(destination, source){
|
56 | if (!source || typeof source != 'object') return destination;
|
57 |
|
58 | Object.keys(source).forEach(function(new_key) {
|
59 | if (source[new_key] != '[object Object]')
|
60 | destination[new_key] = source[new_key];
|
61 | });
|
62 |
|
63 | return destination;
|
64 | },
|
65 |
|
66 | extendMix : function(destination, source){
|
67 | if (!source || typeof source != 'object') return destination;
|
68 |
|
69 | Object.keys(source).forEach(function(new_key) {
|
70 | if (source[new_key] == 'null')
|
71 | delete destination[new_key];
|
72 | else
|
73 | destination[new_key] = source[new_key]
|
74 | });
|
75 |
|
76 | return destination;
|
77 | },
|
78 |
|
79 | whichFileExists : function(file_arr) {
|
80 | var f = null;
|
81 |
|
82 | file_arr.some(function(file) {
|
83 | try {
|
84 | fs.statSync(file);
|
85 | } catch(e) {
|
86 | return false;
|
87 | }
|
88 | f = file;
|
89 | return true;
|
90 | });
|
91 | return f;
|
92 | },
|
93 | clone : function(obj) {
|
94 | if (obj === null || obj === undefined) return {};
|
95 | return fclone(obj);
|
96 | },
|
97 | overrideConsole : function(bus) {
|
98 | if (cst.PM2_LOG_DATE_FORMAT && typeof cst.PM2_LOG_DATE_FORMAT == 'string') {
|
99 |
|
100 | function timestamp(){
|
101 | return `${dateFns.format(Date.now(), cst.PM2_LOG_DATE_FORMAT)}:`;
|
102 | }
|
103 |
|
104 | var hacks = ['info', 'log', 'error', 'warn'], consoled = {};
|
105 |
|
106 |
|
107 | hacks.forEach(function(method){
|
108 | consoled[method] = console[method];
|
109 | });
|
110 |
|
111 | hacks.forEach(function(k){
|
112 | console[k] = function(){
|
113 | if (bus) {
|
114 | bus.emit('log:PM2', {
|
115 | process : {
|
116 | pm_id : 'PM2',
|
117 | name : 'PM2',
|
118 | rev : null
|
119 | },
|
120 | at : Utility.getDate(),
|
121 | data : util.format.apply(this, arguments) + '\n'
|
122 | });
|
123 | }
|
124 |
|
125 | arguments[0] && (arguments[0] = timestamp() + ' PM2 ' + k + ': ' + arguments[0]);
|
126 | consoled[k].apply(console, arguments);
|
127 | };
|
128 | });
|
129 | }
|
130 | },
|
131 | startLogging : function(stds, callback) {
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | var flows = [];
|
152 |
|
153 | var types = Object.keys(stds).sort(function(x, y){
|
154 | return -x.charCodeAt(0) + y.charCodeAt(0);
|
155 | });
|
156 |
|
157 |
|
158 | (function createWS(io){
|
159 | if(io.length != 1){
|
160 | return false;
|
161 | }
|
162 | io = io[0];
|
163 |
|
164 |
|
165 |
|
166 | if(typeof stds[io] == 'object' && !isNaN(stds[io].fd)){
|
167 | return createWS(types.splice(0, 1));
|
168 | }
|
169 |
|
170 | flows.push(function(next){
|
171 | var file = stds[io];
|
172 |
|
173 |
|
174 | if (!file || file.indexOf('NULL') > -1 || file.indexOf('/dev/null') > -1)
|
175 | return next();
|
176 |
|
177 | stds[io] = fs.createWriteStream(file, {flags: 'a'})
|
178 | .once('error', next)
|
179 | .on('open', function(){
|
180 | stds[io].removeListener('error', next);
|
181 |
|
182 | stds[io].on('error', function(err) {
|
183 | console.error(err);
|
184 | });
|
185 |
|
186 | next();
|
187 | });
|
188 | stds[io]._file = file;
|
189 | });
|
190 | return createWS(types.splice(0, 1));
|
191 | })(types.splice(0, 1));
|
192 |
|
193 | waterfall(flows, callback);
|
194 | },
|
195 |
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | getCanonicModuleName: function(module_name) {
|
207 | if (typeof module_name !== 'string') return null;
|
208 | var canonic_module_name = module_name;
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | if (canonic_module_name.match(/\.tgz($|\?)/)) {
|
214 | if (canonic_module_name.match(/^(.+\/)?([^\/]+)\.tgz($|\?)/)) {
|
215 | canonic_module_name = canonic_module_name.match(/^(.+\/)?([^\/]+)\.tgz($|\?)/)[2];
|
216 | if (canonic_module_name.match(/^(.+)-[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_]+\.[0-9]+)?$/)) {
|
217 | canonic_module_name = canonic_module_name.match(/^(.+)-[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_]+\.[0-9]+)?$/)[1];
|
218 | }
|
219 | }
|
220 | }
|
221 |
|
222 |
|
223 | if(canonic_module_name.indexOf('git+') !== -1) {
|
224 | canonic_module_name = canonic_module_name.split('/').pop();
|
225 | }
|
226 |
|
227 |
|
228 | if(canonic_module_name.indexOf('http') !== -1) {
|
229 | var uri = url.parse(canonic_module_name);
|
230 | canonic_module_name = uri.pathname.split('/').pop();
|
231 | }
|
232 |
|
233 |
|
234 | else if(canonic_module_name.indexOf('file://') === 0) {
|
235 | canonic_module_name = canonic_module_name.replace(/\/$/, '').split('/').pop();
|
236 | }
|
237 |
|
238 |
|
239 | else if(canonic_module_name.indexOf('/') !== -1) {
|
240 | if (canonic_module_name.charAt(0) !== "@"){
|
241 | canonic_module_name = canonic_module_name.split('/')[1];
|
242 | }
|
243 | }
|
244 |
|
245 |
|
246 | if(canonic_module_name.lastIndexOf('@') > 0) {
|
247 | canonic_module_name = canonic_module_name.substr(0,canonic_module_name.lastIndexOf("@"));
|
248 | }
|
249 |
|
250 |
|
251 | if(canonic_module_name.indexOf('#') !== -1) {
|
252 | canonic_module_name = canonic_module_name.split('#')[0];
|
253 | }
|
254 |
|
255 | if (canonic_module_name.indexOf('.git') !== -1) {
|
256 | canonic_module_name = canonic_module_name.replace('.git', '');
|
257 | }
|
258 |
|
259 | return canonic_module_name;
|
260 | },
|
261 |
|
262 | checkPathIsNull: function(path) {
|
263 | return path === 'NULL' || path === '/dev/null';
|
264 | },
|
265 |
|
266 | generateUUID: function () {
|
267 | var s = [];
|
268 | var hexDigits = "0123456789abcdef";
|
269 | for (var i = 0; i < 36; i++) {
|
270 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
|
271 | }
|
272 | s[14] = "4";
|
273 | s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
|
274 | s[8] = s[13] = s[18] = s[23] = "-";
|
275 | return s.join("");
|
276 | }
|
277 |
|
278 | };
|