-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
92 lines (81 loc) · 2.62 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
'use strict';
const wicked = require('wicked-sdk');
const fs = require('fs');
const path = require('path');
const { debug, info, warn, error } = require('portal-env').Logger('portal-mailer:utils');
const utils = function () { };
utils.getUtc = function () {
return Math.floor((new Date()).getTime() / 1000);
};
utils.getJson = function (ob) {
if (ob instanceof String || typeof ob === "string") {
if (ob === "")
return null;
return JSON.parse(ob);
}
return ob;
};
utils.getText = function (ob) {
if (ob instanceof String || typeof ob === "string")
return ob;
return JSON.stringify(ob, null, 2);
};
utils.getIndexBy = function (anArray, predicate) {
for (let i = 0; i < anArray.length; ++i) {
if (predicate(anArray[i]))
return i;
}
return -1;
};
utils._packageVersion = null;
utils.getVersion = function () {
if (!utils._packageVersion) {
const packageFile = path.join(__dirname, 'package.json');
if (fs.existsSync(packageFile)) {
try {
const packageInfo = JSON.parse(fs.readFileSync(packageFile, 'utf8'));
if (packageInfo.version)
utils._packageVersion = packageInfo.version;
} catch (ex) {
error(ex);
}
}
if (!utils._packageVersion) // something went wrong
utils._packageVersion = "0.0.0";
}
return utils._packageVersion;
};
utils._gitLastCommit = null;
utils.getGitLastCommit = function () {
if (!utils._gitLastCommit) {
const lastCommitFile = path.join(__dirname, 'git_last_commit');
if (fs.existsSync(lastCommitFile))
utils._gitLastCommit = fs.readFileSync(lastCommitFile, 'utf8');
else
utils._gitLastCommit = '(no last git commit found - running locally?)';
}
return utils._gitLastCommit;
};
utils._gitBranch = null;
utils.getGitBranch = function () {
if (!utils._gitBranch) {
const gitBranchFile = path.join(__dirname, 'git_branch');
if (fs.existsSync(gitBranchFile))
utils._gitBranch = fs.readFileSync(gitBranchFile, 'utf8');
else
utils._gitBranch = '(unknown)';
}
return utils._gitBranch;
};
utils._buildDate = null;
utils.getBuildDate = function () {
if (!utils._buildDate) {
const buildDateFile = path.join(__dirname, 'build_date');
if (fs.existsSync(buildDateFile))
utils._buildDate = fs.readFileSync(buildDateFile, 'utf8');
else
utils._buildDate = '(unknown build date)';
}
return utils._buildDate;
};
module.exports = utils;