-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (48 loc) · 1.71 KB
/
index.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
const fs = require('fs');
const path = require('path');
const getCommits = require('./src/getCommits');
const defaultSetting = require('./src/lib/defaultSetting');
function picklog(_args) {
const args = Object.assign({
gitLogArgs: '', // 透传给命令 git log 的参数
latest: false, // 是否只取上一个tag后的commit
}, _args);
const picklogrc = args.config || '.picklogrc.js';
let setting;
let pkg;
try {
fs.accessSync(path.resolve(picklogrc));
setting = require(path.resolve(picklogrc)); // eslint-disable-line global-require,import/no-dynamic-require
fs.accessSync(path.resolve('package.json'));
pkg = require(path.resolve('package.json')); // eslint-disable-line global-require,import/no-dynamic-require
} catch (err) {
setting = defaultSetting;
pkg = {};
}
return new Promise((resolve) => {
getCommits(args, setting).then((commits) => {
if (commits.length && !commits[0].tag) {
const pkgVersion = pkg.version;
const [firstCommit] = commits;
if (pkgVersion) {
let { previousTag } = firstCommit;
if (previousTag) {
let prefix = previousTag.match(/^([^\d]*)/);
if (prefix) [, prefix] = prefix;
previousTag = previousTag.replace(prefix, '');
if (pkgVersion !== previousTag && pkgVersion.split('.').length === previousTag.split('.').length) {
firstCommit.tag = `${prefix}${pkgVersion}`;
}
} else {
firstCommit.tag = `v${pkgVersion}`;
}
}
}
resolve(setting.parse(commits));
});
});
}
// picklog().then((commitStr) => {
// fs.writeFileSync('bear.md', commitStr);
// });
module.exports = picklog;