-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgit.js
64 lines (59 loc) · 1.55 KB
/
git.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
const git = require('git-last-commit');
const config = require('config');
const fs = require('fs');
const path = require('path');
const logger = require('./logger');
const FILE_NAME = 'lastCommit.json';
/**
* get last commit information from git command
* @return {Promise<*>}
*/
async function getLastCommit() {
return new Promise(((resolve, reject) => {
git.getLastCommit(function (err, commit) {
if (err) {
return reject(err);
}
resolve({
commitSha: commit.hash,
author: commit.author,
committedOn: new Date(commit.committedOn * 1000),
subject: commit.subject,
message: commit.body,
notes: commit.notes,
});
});
}))
}
/**
* create last commit json file
* @return {Promise<void>}
*/
async function generateLastCommitJson() {
try {
const lastCommit = await getLastCommit();
fs.writeFileSync(path.join(config.WORK_ROOT, FILE_NAME), JSON.stringify(lastCommit, null, 2));
} catch (e) {
logger.error('generateLastCommitJson failed');
logger.error(e);
}
}
/**
* get last commit from file, if not exist, it will try to create one
* @return {Promise<*>}
*/
async function getLastCommitFromFile() {
try {
if (!fs.existsSync(path.join(config.WORK_ROOT, FILE_NAME))) {
await generateLastCommitJson();
}
const content = fs.readFileSync(path.join(config.WORK_ROOT, FILE_NAME));
return JSON.parse(content.toString());
} catch (e) {
logger.error(e);
return e;
}
}
module.exports = {
generateLastCommitJson, getLastCommitFromFile
};