UNPKG

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