123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- var fclone = require('fclone');
- var fs = require('fs');
- var cst = require('../constants.js');
- var waterfall = require('async/waterfall');
- var util = require('util');
- var url = require('url');
- var dayjs = require('dayjs');
- var findPackageJson = require('./tools/find-package-json')
- var Utility = module.exports = {
- findPackageVersion : function(fullpath) {
- var version
- try {
- version = findPackageJson(fullpath).next().value.version
- } catch(e) {
- version = 'N/A'
- }
- return version
- },
- getDate : function() {
- return Date.now();
- },
- extendExtraConfig : function(proc, opts) {
- if (opts.env && opts.env.current_conf) {
- if (opts.env.current_conf.env &&
- typeof(opts.env.current_conf.env) === 'object' &&
- Object.keys(opts.env.current_conf.env).length === 0)
- delete opts.env.current_conf.env
- Utility.extendMix(proc.pm2_env, opts.env.current_conf);
- delete opts.env.current_conf;
- }
- },
- formatCLU : function(process) {
- if (!process.pm2_env) {
- return process;
- }
- var obj = Utility.clone(process.pm2_env);
- delete obj.env;
- return obj;
- },
- extend : function(destination, source){
- if (!source || typeof source != 'object') return destination;
- Object.keys(source).forEach(function(new_key) {
- if (source[new_key] != '[object Object]')
- destination[new_key] = source[new_key];
- });
- return destination;
- },
-
- extendMix : function(destination, source){
- if (!source || typeof source != 'object') return destination;
- Object.keys(source).forEach(function(new_key) {
- if (source[new_key] == 'null')
- delete destination[new_key];
- else
- destination[new_key] = source[new_key]
- });
- return destination;
- },
- whichFileExists : function(file_arr) {
- var f = null;
- file_arr.some(function(file) {
- try {
- fs.statSync(file);
- } catch(e) {
- return false;
- }
- f = file;
- return true;
- });
- return f;
- },
- clone : function(obj) {
- if (obj === null || obj === undefined) return {};
- return fclone(obj);
- },
- overrideConsole : function(bus) {
- if (cst.PM2_LOG_DATE_FORMAT && typeof cst.PM2_LOG_DATE_FORMAT == 'string') {
-
- function timestamp(){
- return `${dayjs(Date.now()).format(cst.PM2_LOG_DATE_FORMAT)}:`;
- }
- var hacks = ['info', 'log', 'error', 'warn'], consoled = {};
-
- hacks.forEach(function(method){
- consoled[method] = console[method];
- });
- hacks.forEach(function(k){
- console[k] = function(){
- if (bus) {
- bus.emit('log:PM2', {
- process : {
- pm_id : 'PM2',
- name : 'PM2',
- rev : null
- },
- at : Utility.getDate(),
- data : util.format.apply(this, arguments) + '\n'
- });
- }
-
- arguments[0] && (arguments[0] = timestamp() + ' PM2 ' + k + ': ' + arguments[0]);
- consoled[k].apply(console, arguments);
- };
- });
- }
- },
- startLogging : function(stds, callback) {
-
-
-
-
-
-
-
-
-
-
-
-
-
- var flows = [];
-
- var types = Object.keys(stds).sort(function(x, y){
- return -x.charCodeAt(0) + y.charCodeAt(0);
- });
-
- (function createWS(io){
- if(io.length != 1){
- return false;
- }
- io = io[0];
-
-
- if(typeof stds[io] == 'object' && !isNaN(stds[io].fd)){
- return createWS(types.splice(0, 1));
- }
- flows.push(function(next){
- var file = stds[io];
-
- if (!file || file.indexOf('NULL') > -1 || file.indexOf('/dev/null') > -1)
- return next();
- stds[io] = fs.createWriteStream(file, {flags: 'a'})
- .once('error', next)
- .on('open', function(){
- stds[io].removeListener('error', next);
- stds[io].on('error', function(err) {
- console.error(err);
- });
- next();
- });
- stds[io]._file = file;
- });
- return createWS(types.splice(0, 1));
- })(types.splice(0, 1));
- waterfall(flows, callback);
- },
-
- getCanonicModuleName: function(module_name) {
- if (typeof module_name !== 'string') return null;
- var canonic_module_name = module_name;
-
-
-
- if (canonic_module_name.match(/\.tgz($|\?)/)) {
- if (canonic_module_name.match(/^(.+\/)?([^\/]+)\.tgz($|\?)/)) {
- canonic_module_name = canonic_module_name.match(/^(.+\/)?([^\/]+)\.tgz($|\?)/)[2];
- if (canonic_module_name.match(/^(.+)-[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_]+\.[0-9]+)?$/)) {
- canonic_module_name = canonic_module_name.match(/^(.+)-[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_]+\.[0-9]+)?$/)[1];
- }
- }
- }
-
- if(canonic_module_name.indexOf('git+') !== -1) {
- canonic_module_name = canonic_module_name.split('/').pop();
- }
-
- if(canonic_module_name.indexOf('http') !== -1) {
- var uri = url.parse(canonic_module_name);
- canonic_module_name = uri.pathname.split('/').pop();
- }
-
- else if(canonic_module_name.indexOf('file://') === 0) {
- canonic_module_name = canonic_module_name.replace(/\/$/, '').split('/').pop();
- }
-
- else if(canonic_module_name.indexOf('/') !== -1) {
- if (canonic_module_name.charAt(0) !== "@"){
- canonic_module_name = canonic_module_name.split('/')[1];
- }
- }
-
- if(canonic_module_name.lastIndexOf('@') > 0) {
- canonic_module_name = canonic_module_name.substr(0,canonic_module_name.lastIndexOf("@"));
- }
-
- if(canonic_module_name.indexOf('#') !== -1) {
- canonic_module_name = canonic_module_name.split('#')[0];
- }
- if (canonic_module_name.indexOf('.git') !== -1) {
- canonic_module_name = canonic_module_name.replace('.git', '');
- }
- return canonic_module_name;
- },
- checkPathIsNull: function(path) {
- return path === 'NULL' || path === '/dev/null' || path === '\\\\.\\NUL';
- },
- generateUUID: function () {
- var s = [];
- var hexDigits = "0123456789abcdef";
- for (var i = 0; i < 36; i++) {
- s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
- }
- s[14] = "4";
- s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
- s[8] = s[13] = s[18] = s[23] = "-";
- return s.join("");
- }
- };
|