helpers.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const chalk = require('chalk')
  2. const Helpers = {}
  3. /**
  4. * Converts Byte to Human readable size
  5. * @method bytesToSize
  6. * @param {} bytes
  7. * @param {} precision
  8. * @return
  9. */
  10. Helpers.bytesToSize = function(bytes, precision) {
  11. var kilobyte = 1024
  12. var megabyte = kilobyte * 1024
  13. var gigabyte = megabyte * 1024
  14. var terabyte = gigabyte * 1024
  15. if ((bytes >= 0) && (bytes < kilobyte)) {
  16. return bytes + 'b '
  17. } else if ((bytes >= kilobyte) && (bytes < megabyte)) {
  18. return (bytes / kilobyte).toFixed(precision) + 'kb '
  19. } else if ((bytes >= megabyte) && (bytes < gigabyte)) {
  20. return (bytes / megabyte).toFixed(precision) + 'mb '
  21. } else if ((bytes >= gigabyte) && (bytes < terabyte)) {
  22. return (bytes / gigabyte).toFixed(precision) + 'gb '
  23. } else if (bytes >= terabyte) {
  24. return (bytes / terabyte).toFixed(precision) + 'tb '
  25. } else {
  26. return bytes + 'b '
  27. }
  28. }
  29. /**
  30. * Color Process state
  31. * @method colorStatus
  32. * @param {} status
  33. * @return
  34. */
  35. Helpers.colorStatus = function(status) {
  36. switch (status) {
  37. case 'online':
  38. return chalk.green.bold('online')
  39. break
  40. case 'running':
  41. return chalk.green.bold('online')
  42. break
  43. case 'restarting':
  44. return chalk.yellow.bold('restart')
  45. break
  46. case 'created':
  47. return chalk.yellow.bold('created')
  48. break
  49. case 'launching':
  50. return chalk.blue.bold('launching')
  51. break
  52. default:
  53. return chalk.red.bold(status)
  54. }
  55. }
  56. /**
  57. * Safe Push
  58. */
  59. Helpers.safe_push = function() {
  60. var argv = arguments
  61. var table = argv[0]
  62. for (var i = 1; i < argv.length; ++i) {
  63. var elem = argv[i]
  64. if (elem[Object.keys(elem)[0]] === undefined
  65. || elem[Object.keys(elem)[0]] === null) {
  66. elem[Object.keys(elem)[0]] = 'N/A'
  67. }
  68. else if (Array.isArray(elem[Object.keys(elem)[0]])) {
  69. elem[Object.keys(elem)[0]].forEach(function(curr, j) {
  70. if (curr === undefined || curr === null)
  71. elem[Object.keys(elem)[0]][j] = 'N/A'
  72. })
  73. }
  74. table.push(elem)
  75. }
  76. }
  77. /**
  78. * Description
  79. * @method timeSince
  80. * @param {} date
  81. * @return BinaryExpression
  82. */
  83. Helpers.timeSince = function(date) {
  84. var seconds = Math.floor((new Date() - date) / 1000)
  85. var interval = Math.floor(seconds / 31536000)
  86. if (interval > 1) {
  87. return interval + 'Y'
  88. }
  89. interval = Math.floor(seconds / 2592000)
  90. if (interval > 1) {
  91. return interval + 'M'
  92. }
  93. interval = Math.floor(seconds / 86400)
  94. if (interval > 1) {
  95. return interval + 'D'
  96. }
  97. interval = Math.floor(seconds / 3600)
  98. if (interval > 1) {
  99. return interval + 'h'
  100. }
  101. interval = Math.floor(seconds / 60)
  102. if (interval > 1) {
  103. return interval + 'm'
  104. }
  105. return Math.floor(seconds) + 's'
  106. }
  107. /**
  108. * Colorize Metrics
  109. *
  110. * @param {Number} value current value
  111. * @param {Number} warn value threshold
  112. * @param {Number} alert value threshold
  113. * @param {String} prefix value prefix
  114. * @return {String} value
  115. */
  116. Helpers.colorizedMetric = function(value, warn, alert, prefix) {
  117. var inverted = false
  118. if (alert < warn)
  119. inverted = true
  120. if (!prefix) prefix = ''
  121. if (isNaN(value) === true)
  122. return 'N/A'
  123. if (value == 0)
  124. return 0 + prefix
  125. if (inverted == true) {
  126. if (value > warn)
  127. return chalk.green(value + prefix)
  128. if (value <= warn && value >= alert)
  129. return chalk.bold.yellow(value + prefix)
  130. return chalk.bold.red(value + prefix)
  131. }
  132. if (value < warn)
  133. return chalk.green(value + prefix)
  134. if (value >= warn && value <= alert)
  135. return chalk.bold.yellow(value + prefix)
  136. return chalk.bold.red(value + prefix)
  137. }
  138. /**
  139. * Get nested property
  140. *
  141. * @param {String} propertyName
  142. * @param {Object} obj
  143. * @returns {String} property value
  144. */
  145. Helpers.getNestedProperty = function(propertyName, obj) {
  146. var parts = propertyName.split('.'),
  147. length = parts.length,
  148. property = obj || {}
  149. for (var i = 0; i < length; i++ ) {
  150. property = property[parts[i]]
  151. }
  152. return property
  153. }
  154. Helpers.openEditor = function (file, opts, cb) {
  155. var spawn = require('child_process').spawn
  156. if (typeof opts === 'function') {
  157. cb = opts
  158. opts = {}
  159. }
  160. if (!opts) opts = {}
  161. var ed = /^win/.test(process.platform) ? 'notepad' : 'vim'
  162. var editor = opts.editor || process.env.VISUAL || process.env.EDITOR || ed
  163. var args = editor.split(/\s+/)
  164. var bin = args.shift()
  165. var ps = spawn(bin, args.concat([ file ]), {
  166. windowsHide: true,
  167. stdio: 'inherit'
  168. })
  169. ps.on('exit', function (code, sig) {
  170. if (typeof cb === 'function') cb(code, sig)
  171. })
  172. }
  173. Helpers.dispKeys = function(kv, target_module) {
  174. Object.keys(kv).forEach(function(key) {
  175. if (target_module != null && target_module != key)
  176. return false
  177. if (typeof(kv[key]) == 'object') {
  178. var obj = {}
  179. console.log(chalk.bold('Module: ') + chalk.bold.blue(key))
  180. Object.keys(kv[key]).forEach(function(sub_key) {
  181. console.log(`$ pm2 set ${key}:${sub_key} ${kv[key][sub_key]}`)
  182. })
  183. }
  184. })
  185. }
  186. module.exports = Helpers