forked from webhintio/hint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
90 lines (69 loc) · 2.41 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
const shell = require('shelljs');
const chalk = require('chalk');
shell.config.silent = true;
const shellExec = (cmd) => {
return new Promise((resolve, reject) => {
shell.exec(cmd, (code, stdout, stderr) => {
if (code === 0) {
return resolve({ code, stderr, stdout });
}
return reject({ code, stderr, stdout });
});
});
};
const exec = async (msg, cmd) => { // eslint-disable-line consistent-return
if (typeof cmd === 'function') {
try {
const result = await cmd();
console.log(chalk.green(`${msg}`));
return result && result.trim && result.trim();
} catch (e) {
console.error(chalk.red(`${msg}`));
console.error(e);
process.exit(1); // eslint-disable-line no-process-exit
}
return '';
}
try {
const { stdout } = await shellExec(cmd);
console.log(chalk.green(`${msg}`));
console.log(chalk.green(` ${cmd}`));
return stdout.trim();
} catch ({ stderr }) {
console.error(chalk.red(`${msg}`));
console.log(chalk.green(` ${cmd}`));
console.error(stderr);
process.exit(1); // eslint-disable-line no-process-exit
}
};
const getLastCommitsWithSameMessage = async () => {
const output = await exec('Get last number of commits with the same message', `git log --pretty=format:'%s' --max-count=70`);
const messages = output.trim().split('\n');
let same = true;
let commits = 0;
while (same && commits < messages.length) {
commits++;
same = messages[commits - 1] === messages[commits];
}
return commits;
};
const getLastCommitInfo = async (message, format) => {
// See: https://git-scm.com/docs/pretty-formats#_pretty_formats.
return await exec(message, `git show --no-patch --format=%${format} -1`);
};
const getLastCommitAuthorEmail = async () => {
return await getLastCommitInfo('Get author email for the last commit.', 'ae');
};
const getLastCommitAuthorName = async () => {
return await getLastCommitInfo('Get author name for the last commit.', 'an');
};
const getLastCommitSubject = async () => {
return await getLastCommitInfo('Get subject for the last commit.', 's');
};
module.exports = {
exec,
getLastCommitAuthorEmail,
getLastCommitAuthorName,
getLastCommitSubject,
getLastCommitsWithSameMessage
};