Skip to content

Commit 89f55e7

Browse files
if-kenndscho
authored andcommitted
bump-version: use a proper JSON parser
Best practices of dealing with JSON highly discourage regex parsing since it is so fraught with edge cases. Since this is being done via node which has native JSON parsing it is not even necessary. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent e9aec92 commit 89f55e7

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

bump-version.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,28 @@ var autoUpdate = function() {
4747
};
4848

4949
var determineVersion = function(body) {
50-
var lines = body.replace(/\n/gm, ',').split(',');
51-
var tagName = lines.findFirst(/"tag_name": *"(.*)"/);
52-
var regex = /^v(\d+\.\d+\.\d+(\.\d+)?)\.windows\.(\d+)$/;
53-
var match = regex.exec(tagName);
54-
var version = match[1];
55-
if (parseInt(match[3]) > 1)
56-
version += '(' + match[3] + ')';
57-
var timestamp = lines.findFirst(/"published_at": *"(.*)"/);
58-
regex = /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)Z$/;
59-
match = regex.exec(timestamp);
60-
var latest = new Date(match[1], match[2] - 1, match[3],
61-
match[4], match[5], match[6], 0).toUTCString();
62-
latest = latest.replace(/GMT$/, 'UTC');
63-
var url = lines.findFirst(/"html_url": *"(.*)"/);
50+
var releases = JSON.parse(body),
51+
versionRegex = /^v(\d+\.\d+\.\d+(\.\d+)?)\.windows\.(\d+)/,
52+
timeRegex = /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)Z$/,
53+
version = false,
54+
match, latest, url;
55+
56+
for (var i = 0; i < releases.length && !version; i++) {
57+
if (match = releases[i].tag_name.match(versionRegex)) {
58+
version = match[1];
59+
60+
if (parseInt(match[3]) > 1) {
61+
version += '(' + match[3] + ')';
62+
}
63+
64+
match = releases[i].published_at.match(timeRegex);
65+
latest = new Date(match[1], match[2] - 1, match[3],
66+
match[4], match[5], match[6], 0).toUTCString();
67+
latest = latest.replace(/GMT$/, 'UTC');
68+
url = releases[i].html_url;
69+
}
70+
}
71+
6472
process.stderr.write('Auto-detected version ' + version
6573
+ ' (' + latest + ')\n');
6674
return [ version, latest, url ];

0 commit comments

Comments
 (0)