-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchangelog.js
93 lines (86 loc) · 2.76 KB
/
changelog.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
const { writeFileSync } = require('fs');
const { EOL } = require('os');
const { join } = require('path');
const spawn = require('child_process').spawn;
/*
* Generate CHANGELOG.md from commits and tags
* repo: https://github.com/dimaslanjaka/nodejs-package-types/blob/main/changelog.js
* raw: https://github.com/dimaslanjaka/nodejs-package-types/raw/main/changelog.js
* update: curl -L https://github.com/dimaslanjaka/nodejs-package-types/raw/main/changelog.js > changelog.js
*/
/**
* git
* @param {string[]} command
* @returns {Promise<string>}
*/
const gitExec = (command) =>
new Promise((resolve, reject) => {
const thread = spawn('git', command, {
stdio: ['inherit', 'pipe', 'pipe']
});
const stdOut = [];
const stdErr = [];
thread.stdout.on('data', (data) => {
stdOut.push(data.toString('utf8'));
});
thread.stderr.on('data', (data) => {
stdErr.push(data.toString('utf8'));
});
thread.on('close', () => {
if (stdErr.length) {
reject(stdErr.join(''));
return;
}
resolve(stdOut.join());
});
});
let markdown = ``;
// git log reference https://www.edureka.co/blog/git-format-commit-history/
// git log date format reference https://stackoverflow.com/questions/7853332/how-to-change-git-log-date-formats
// custom --pretty=format:"%h %ad | %s %d [%an]" --date=short v1.1.4...v1.1.8
// default --pretty=oneline v1.1.4...v1.1.8
gitExec([
'log',
'--pretty=format:%h !|! %ad !|! %s %d',
`--date=format:%Y-%m-%d %H:%M:%S`
]).then(function (commits) {
commits
.split(/\r?\n/gm)
.slice()
.reverse()
.forEach((str, index, all) => {
const splitx = str.split('!|!').map((str) => str.trim());
const o = {
hash: splitx[0],
date: splitx[1],
message: splitx[2]
};
if (o.message.includes('tag: v')) {
const regex =
/(.+)\s+\(tag:.+((0|[1-9][0-9]*).(0|[1-9][0-9]*).(0|[1-9][0-9]*))\)/im;
const m = o.message.match(regex);
let versionTitle;
let versionMessage;
if (m && m.length > 0) {
versionTitle = m[2];
versionMessage = m[1];
markdown += `\n**${versionTitle}**\n\n${versionMessage}\n` + EOL;
} else {
markdown +=
`\n**${o.message.replace(/\(.*\),?/, '').trim()}**\n` + EOL;
}
} else {
markdown +=
`- [ _${o.date}_ ] [${o.hash}](https://github.com/dimaslanjaka/safelink/commit/${o.hash}) ${o.message.replace(
/,$/,
''
)}` + EOL;
}
if (index === all.length - 1) {
if (!markdown.trim().startsWith('**')) {
markdown = '**0.0.1** - _init project_\n' + markdown;
}
writeFileSync(join(__dirname, 'CHANGELOG.md'), markdown);
}
});
});