forked from jesstelford/version-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (132 loc) · 4.4 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
#!/usr/bin/env node
var fs = require('fs');
var spawn = require('cross-spawn');
var githubUrlFromGit = require('github-url-from-git');
var bitbucketUrlFromGit = require('bitbucket-url-from-git');
var gitlabUrlFromGit = require('giturl').parse;
var hasYarn = require('has-yarn');
var gitRemote = require('./util/git-remote');
function dateWithLeadingZero(date) {
return ('0' + date).slice(-2);
}
function insertHeading(data, versionString) {
var date = new Date();
return data.replace(
/(## \[Unreleased\].*)/g,
'$1\n\n## [' + versionString + '][] - ' + date.getFullYear() + '-' + dateWithLeadingZero(date.getMonth() + 1) + '-' + dateWithLeadingZero(date.getDate())
);
}
function getVersionPrefix() {
var output = null;
var versionPrefix = 'v';
if (hasYarn()) {
output = spawn.sync('yarn', ['config', 'get', 'version-tag-prefix']);
} else {
output = spawn.sync('npm', ['config', 'get', 'tag-version-prefix']);
}
if (output.stdout && !output.error) {
versionPrefix = output.stdout.toString().trim();
}
return versionPrefix;
}
function githubUris(data, newVersion) {
var remoteUrl;
var previousVersion;
var previousLink;
var prefix = getVersionPrefix();
// Try get previous version and remote from unreleased tag if it exists
var match = /\[Unreleased\]: (.*)\/compare\/(.*)\.\.\.HEAD/g.exec(data);
if (match) {
remoteUrl = match[1];
previousVersion = match[2];
}
if (!remoteUrl) {
remoteUrl = githubUrlFromGit(gitRemote());
}
if (previousVersion) {
previousLink = '[' + newVersion + ']: ' + remoteUrl + '/compare/' + previousVersion + '...' + prefix + newVersion;
} else {
previousLink = '[' + newVersion + ']: ' + remoteUrl + '/tree/' + prefix + newVersion;
}
return {
unreleased: '[Unreleased]: ' + remoteUrl + '/compare/' + prefix + newVersion + '...HEAD',
previous: previousLink
};
}
function bitbucketUris(data, newVersion) {
var remoteUrl;
var previousVersion;
var previousLink;
var prefix = getVersionPrefix();
// Try get previous version and remote from unreleased tag if it exists
var match = /\[Unreleased\]: (.*)\/branches\/compare\/master%0D(.*)/g.exec(data);
if (match) {
remoteUrl = match[1];
previousVersion = match[2];
}
if (!remoteUrl) {
remoteUrl = bitbucketUrlFromGit(gitRemote());
}
if (previousVersion) {
previousLink = '[' + newVersion + ']: ' + remoteUrl + '/branches/compare/' + prefix + newVersion + '%0D' + previousVersion;
} else {
previousLink = '[' + newVersion + ']: ' + remoteUrl + '/commits/tag/' + prefix + newVersion;
}
return {
unreleased: '[Unreleased]: ' + remoteUrl + '/branches/compare/master%0D' + prefix + newVersion,
previous: previousLink
};
}
function gitlabUris(data, newVersion) {
var remoteUrl;
var previousVersion;
var previousLink;
var prefix = getVersionPrefix();
// Try get previous version and remote from unreleased tag if it exists
var match = /\[Unreleased\]: (.*)\/compare\/master...(.*)/g.exec(data);
if (match) {
remoteUrl = match[1];
previousVersion = match[2];
}
if (!remoteUrl) {
var remote = gitRemote();
remoteUrl = gitlabUrlFromGit(gitRemote());
}
if (previousVersion) {
previousLink = '[' + newVersion + ']: ' + remoteUrl + '/compare/' + prefix + newVersion + '...' + previousVersion;
} else {
previousLink = '[' + newVersion + ']: ' + remoteUrl + '/tags/' + prefix + newVersion;
}
return {
unreleased: '[Unreleased]: ' + remoteUrl + '/compare/master...' + prefix + newVersion,
previous: previousLink
};
}
const UriFunctions = {
'github': githubUris,
'gitlab': gitlabUris,
'bitbucket': bitbucketUris,
}
function updateCompareUri(data, versionString, remote) {
var remoteUrl;
var uris = UriFunctions[remote](data, versionString);
if (data.match(/\[Unreleased\]:/)) {
return data.replace(/\[Unreleased\]:.*/, uris.unreleased + '\n' + uris.previous);
}
return data
+ '\n\n' + uris.unreleased
+ '\n' + uris.previous;
}
module.exports = function versionChangelog(data, { version, remote}, done) {
try {
// Add the heading for the new version number
data = insertHeading(data, version);
// Update the [Unreleased] URI
// and add the new version URI
data = updateCompareUri(data, version, remote ? remote : 'github');
done(null, data);
} catch (error) {
done(error);
}
}
module.exports.getVersionPrefix = getVersionPrefix;