This repository has been archived by the owner on Jun 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
147 lines (120 loc) · 3.24 KB
/
helpers.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use stict';
var execSync = require('child_process').execSync;
var fs = require('fs');
var chalk = require('chalk');
var readPkg = require('read-pkg-up').sync;
var semver = require('semver');
var stripEof = require('strip-eof');
module.exports = {
capitalize: function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
},
checkDirectory: function (dir) {
try {
fs.statSync(dir);
} catch (err) {
this.error('no such file or directory: ' + dir, 1);
}
if (!fs.statSync(dir).isDirectory()) {
this.error('not a directory: ' + dir, 1);
}
},
checkGitExec: function () {
this.exec('git --version', {stdio: 'ignore'}, 'Missing required executable: git');
},
checkGitRepo: function () {
this.log('Checking if "' + chalk.cyan(process.cwd()) + '" is a Git repository ...');
this.exec('git rev-parse', {stdio: ['ignore', 'ignore', 'pipe']});
},
error: function (msg, code) {
var err = chalk.red.bold;
console.log(err(msg));
console.log(err('Aborting.'));
/* eslint xo/no-process-exit: 0 */
process.exit(code);
},
exec: function (cmd, opts, msg) {
try {
var rs = execSync(cmd, opts);
return rs && stripEof(rs).toString();
} catch (err) {
this.error(msg || stripEof(err.stderr).toString().split(/\r?\n/)[0], err.status || 1);
}
},
format: function (commits) {
return commits.map(function (commit) {
return ' * ' + commit;
}).join('\n') + '\n\n';
},
getHeader: function (header, release) {
header = header || '@RELEASE / @DATE\n==================\n\n';
return header
.replace('@RELEASE', release)
.replace('@DATE', this.today());
},
getHomePage: function () {
var originUrl = this.exec('git config --get remote.origin.url');
var regex = /^git@[^:]+:[^\/]+\/.+/i;
if (regex.test(originUrl)) {
originUrl = originUrl
.replace(':', '/')
.replace('git@', 'https://');
}
return originUrl.replace('.git', '/');
},
getLog: function (commitish, format) {
var rs;
var tag;
format = format || '%s';
if (!commitish) {
tag = this.getTag();
commitish = tag && tag + '..HEAD';
}
this.log('Getting the list of commits...');
rs = this.exec('git log ' + commitish + ' --no-merges --pretty=format:"' + format + '"',
{stdio: ['ignore', 'pipe', 'pipe']});
if (!rs) {
this.error('No commits found', 1);
}
return rs;
},
getTag: function () {
var commithash;
try {
commithash = execSync('git rev-list --tags --max-count=1',
{stdio: ['ignore', 'pipe', 'ignore']});
} catch (err) {
return '';
}
return this.exec('git describe --tags ' + commithash);
},
getVersion: function (release) {
if (release) {
if (semver.valid(release) === null) {
this.warn('"' + release + '" is not valid semver. Using the one in `package.json`.\n');
} else {
return release;
}
}
var pkg = readPkg().pkg;
var version = pkg ? pkg.version : 'x.x.x';
return version.replace('-pre', '');
},
log: function (msg) {
if (!this.verbose) {
return;
}
console.log(msg || '');
},
section: function (sect) {
this.log();
this.log(chalk.blue.bold('→ ') + chalk.magenta(sect));
this.log();
},
today: function () {
return new Date().toISOString().replace(/T.+/, '');
},
warn: function (msg) {
console.log(chalk.yellow('WARN: ' + msg));
}
};