-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.js
47 lines (42 loc) · 1.02 KB
/
common.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
const chalk = require('chalk');
const validator = require('validator');
const config = require('../config');
/**
* Output an exception
* @param {Exception} e
*/
const handleError = e => {
console.log(chalk.red(`Error: ${e.message}\n`), e);
};
const outputNotRunned = program => {
console.log(chalk.red('check required arguments.'));
program.outputHelp();
};
const mainDbEndFinally = async () => {
const {
mainDb,
checkIsConnected,
} = require('../server/database/main_connection');
try {
if (await checkIsConnected()) {
await mainDb.close();
console.log(chalk.green('Database closed!'));
}
} catch (err) {
console.error(chalk.red('Database close error: ' + `${err}`));
}
};
/**
* Generates random int
* @param {number} max inclusive
*/
const rand = max => {
max = max || Number.MAX_SAFE_INTEGER;
return Math.floor(Math.random() * max);
};
module.exports = {
handleError: handleError,
rand: rand,
mainDbEndFinally: mainDbEndFinally,
outputNotRunned: outputNotRunned,
};