This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·189 lines (164 loc) · 4.25 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env node
/*
* 1. Generates a changelog and opens it in the default editor.
* 2. Commits a changelog as a commit message.
*
* Usage:
* tamia-changelog [commit]
*
* Author: Artem Sapegin, sapegin.me
* License: MIT
* https://github.com/sapegin/dotfiles
*/
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const opn = require('opn');
const through = require('through2');
const shellEscape = require('shell-escape');
const gitLatestSemverTag = require('git-latest-semver-tag');
const commitsBetween = require('commits-between');
const getPkgRepo = require('get-pkg-repo');
const conventionalCommitsParser = require('conventional-commits-parser').sync;
const conventionalChangelogWriter = require('conventional-changelog-writer');
const parserOpts = require('semantic-release-tamia/lib/parser-opts');
const writerOpts = require('./writer-opts');
const CHANGELOG_FILE = 'Changelog.md';
const TYPE_FEATURE = 'Feat';
const TYPE_FIX = 'Fix';
const TYPE_CHANGELOG = 'Changelog';
const is = (a, b) => (a || '').toUpperCase() === (b || '').toUpperCase();
const hasBreakingChanges = commit =>
commit.notes &&
!!commit.notes.find(c => parserOpts.noteKeywords.includes(c.title));
function error(message) {
console.error(message);
process.exit(1);
}
function usage() {
const appName = path.basename(process.argv[1]);
return `
Usage:
${appName} [commit]
`.trim();
}
function parseCommits(commits, cb) {
const changes = commits
.map(parseCommit)
.filter(commit => commit && commit.type);
cb(null, changes);
}
function parseCommit(commit) {
const parsed = conventionalCommitsParser(
`${commit.subject}\n\n${commit.body}`,
parserOpts
);
return Object.assign({}, parsed, {
type: getCommitType(parsed),
});
}
function getCommitType(commit) {
if (hasBreakingChanges(commit)) {
return 'breaking';
} else if (is(commit.type, TYPE_FEATURE) || is(commit.type, TYPE_FIX)) {
return commit.type.toLowerCase();
}
return null;
}
function generateChangelog(changes, done) {
const chunks = [];
getChangesStream(changes)
.pipe(getWriterStream())
.on('error', err => {
console.error(`Cannot generate change log: ${err}`);
process.exit(1);
})
.on('end', () => {
done(chunks.join('\n\n').trim());
})
.pipe(
through((chunk, enc, cb) => {
chunks.push(chunk.toString());
cb();
})
);
}
function getChangesStream(changes) {
const stream = through.obj();
changes.forEach(x => stream.write(x));
stream.end();
return stream;
}
function getWriterStream() {
try {
return conventionalChangelogWriter(getTemplateContext(), writerOpts);
} catch (err) {
console.error(err.toString());
process.exit(1);
return false;
}
}
function getTemplateContext() {
const pkg = require(path.resolve(process.cwd(), 'package.json'));
const repo = getPkgRepo(pkg);
return {
host: `${repo.default}://${repo.domain}`,
owner: repo.user,
repository: repo.project,
};
}
function buildChangelog() {
gitLatestSemverTag((err, tag) => {
if (err) {
error(err);
}
console.log(`Generating changelog since ${tag}...`);
commitsBetween({ from: tag }).then(commits => {
console.log(`${commits.length} commits found.`);
if (!commits.length) {
return;
}
parseCommits(commits, (err, changes) => {
if (err) {
error(err);
}
generateChangelog(changes, changelog => {
fs.writeFileSync(CHANGELOG_FILE, changelog);
opn(CHANGELOG_FILE, { wait: false });
});
});
}, error);
});
}
function commitChangelog() {
if (!fs.existsSync(CHANGELOG_FILE)) {
error(`Changelog file not found: "${CHANGELOG_FILE}".`);
}
console.log('Commiting changelog...');
const changelog = fs.readFileSync(CHANGELOG_FILE, 'utf8');
gitCommit(`${TYPE_CHANGELOG}: 🚀`, changelog, '--allow-empty', err => {
if (err) {
console.log('Cannot commit', err);
return;
}
console.log('Done.');
console.log('');
console.log('Don’t forget to push!');
});
}
function gitCommit(head, body, options, callback) {
exec(
'git commit ' + shellEscape([options, '-m', `${head}\n\n${body}`]),
callback
);
}
const command = process.argv[2];
if (command) {
if (command === 'commit') {
commitChangelog();
} else {
error(`Unknown command "${command}".\n\n${usage()}`);
}
} else {
buildChangelog();
}