Skip to content

Commit ba1798c

Browse files
committed
build: add version script for npm version releases
1 parent 7e7167b commit ba1798c

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"test": "mocha --reporter spec --bail --check-leaks test/",
5353
"test-cov": "nyc --reporter=html --reporter=text npm test",
5454
"test-travis": "nyc --reporter=text npm test",
55-
"update": "npm run fetch && npm run build"
55+
"update": "npm run fetch && npm run build",
56+
"version": "node scripts/version-history.js && git add HISTORY.md"
5657
}
5758
}

scripts/version-history.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
var fs = require('fs')
4+
var path = require('path')
5+
6+
var HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md')
7+
var MD_HEADER_REGEXP = /^====*$/
8+
var VERSION = process.env.npm_package_version
9+
var VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/
10+
11+
var historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n')
12+
13+
if (!MD_HEADER_REGEXP.test(historyFileLines[1])) {
14+
console.error('Missing header in HISTORY.md')
15+
process.exit(1)
16+
}
17+
18+
if (!VERSION_PLACEHOLDER_REGEXP.test(historyFileLines[0])) {
19+
console.error('Missing placegolder version in HISTORY.md')
20+
process.exit(1)
21+
}
22+
23+
if (historyFileLines[0].indexOf('x') !== -1) {
24+
var versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$')
25+
26+
if (!versionCheckRegExp.test(VERSION)) {
27+
console.error('Version %s does not match placeholder %s', VERSION, historyFileLines[0])
28+
process.exit(1)
29+
}
30+
}
31+
32+
historyFileLines[0] = VERSION + ' / ' + getLocaleDate()
33+
historyFileLines[1] = repeat('=', historyFileLines[0].length)
34+
35+
fs.writeFileSync(HISTORY_FILE_PATH, historyFileLines.join('\n'))
36+
37+
function getLocaleDate () {
38+
var now = new Date()
39+
40+
return zeroPad(now.getFullYear(), 4) + '-' +
41+
zeroPad(now.getMonth() + 1, 2) + '-' +
42+
zeroPad(now.getDate(), 2)
43+
}
44+
45+
function repeat (str, length) {
46+
var out = ''
47+
48+
for (var i = 0; i < length; i++) {
49+
out += str
50+
}
51+
52+
return out
53+
}
54+
55+
function zeroPad (number, length) {
56+
var num = number.toString()
57+
58+
while (num.length < length) {
59+
num = '0' + num
60+
}
61+
62+
return num
63+
}

0 commit comments

Comments
 (0)