This repository has been archived by the owner on Apr 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
127 lines (109 loc) · 3.2 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Utils functions.
*/
'use strict'
const fs = require('fs-extra')
const moment = require('moment')
const spawn = require('child_process').spawn
const utils = {}
module.exports = utils
fs.ensureDir('./logs/', (err) => { if (err) throw err })
/**
* Custom logging tool. Print logs to cron and save them to file (if name !== 'onlyCron')
* @param {string} log - message which need logging.
* @param {string} name - custom file name for current log.
*/
utils._log_ = (log, name) => {
console.log(log)
if (name === 'onlyCron') return
const d = new Date()
if (!name) {
// January is 0!
const mmStartFromZero = 1
name = d.getMonth() + mmStartFromZero
if (name < 10) name = `0${name}`
}
fs.appendFile(`./logs/${d.getFullYear()}_${name}`, `${d}: ${log}\n`, (err) => {
if (err) throw err
})
}
/**
* Determination of language of the event by title of the month.
* Have exports value: lang.
* @returns {string} language.
*/
utils.locale = (mm) => {
moment.locale('ru')
if (!isNaN(moment(mm, 'MMMM').get('month'))) {
utils.lang = 'ru'
return utils.lang
}
moment.locale('uk')
if (!isNaN(moment(mm, 'MMMM').get('month'))) {
utils.lang = 'uk'
return utils.lang
}
utils.lang = 'en'
return utils.lang
}
/**
* Date generator for ain links.
* @param {number} num - what month from now we need.
* @returns {string} date in 'year-month' format.
*/
utils.ainGetMonth = (num) => {
const now = new Date()
const nowYear = now.getFullYear()
const nowMonth = now.getMonth() + 1
let month = nowMonth + num
let year = nowYear
while (month > 12) {
year += 1
month -= 12
}
if (month < 10) month = '0' + month
const date = `${year}-${month}`
return date
}
/**
* @returns {number} days in curent month.
*/
Date.prototype.daysInMonth = function () {
return 32 - new Date(this.getFullYear(), this.getMonth(), 32).getDate()
}
/**
* Constructor. Run command in shell.
* @param {string} cmd - command name.
* @param {Array of strings} args - command's arguments.
* @param {string} cbStdout - callback with returned string.
* @param {number} cbEnd - callback with exit status code.
*/
function RunInShell (cmd, args, cbStdout, cbEnd) {
const child = spawn(cmd, args)
const self = this
// Send a cb to set 1 when cmd exits
self.exit = 0
self.stdout = ''
child.stdout.on('data', (data) => { cbStdout(self, data) })
child.stdout.on('end', () => { cbEnd(self) })
}
/**
* This is wrapper function for easly work with RunInShell function.
* @param {string} cmd - command name.
* @param {Array of strings} args - command's arguments.
* @returns {Object} RunInShell.
*/
function RIS (cmd, args) {
return new RunInShell(cmd, args,
(self, data) => { self.stdout += data.toString().slice(0, -1) },
(self) => { self.exit = 1 }
)
}
/**
* Get build version based on 'git tags' and current branch.
* Reads the app version from the env var or the file as a fallback.
* Pattern: vMAJOR.MINOR.PATCH-%branch name%-%number of commits from last tag%-%commit hash%.
* Example: v0.5.0-versioning-8-g65cc3b3.
* @returns {string} version - current build version.
*/
utils.getVersion = () => process.env.APP_VERSION || RIS('cat', [process.env.APP_VERSION_FILE]).stdout