Skip to content

Commit

Permalink
build: add version script for npm version releases
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 16, 2019
1 parent b223a34 commit a1949cb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -34,6 +34,7 @@
"lint": "eslint --plugin markdown --ext js,md .",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/"
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
"version": "node scripts/version-history.js && git add HISTORY.md"
}
}
63 changes: 63 additions & 0 deletions scripts/version-history.js
@@ -0,0 +1,63 @@
'use strict'

var fs = require('fs')
var path = require('path')

var HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md')
var MD_HEADER_REGEXP = /^====*$/
var VERSION = process.env.npm_package_version
var VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/

var historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n')

if (!MD_HEADER_REGEXP.test(historyFileLines[1])) {
console.error('Missing header in HISTORY.md')
process.exit(1)
}

if (!VERSION_PLACEHOLDER_REGEXP.test(historyFileLines[0])) {
console.error('Missing placegolder version in HISTORY.md')
process.exit(1)
}

if (historyFileLines[0].indexOf('x') !== -1) {
var versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$')

if (!versionCheckRegExp.test(VERSION)) {
console.error('Version %s does not match placeholder %s', VERSION, historyFileLines[0])
process.exit(1)
}
}

historyFileLines[0] = VERSION + ' / ' + getLocaleDate()
historyFileLines[1] = repeat('=', historyFileLines[0].length)

fs.writeFileSync(HISTORY_FILE_PATH, historyFileLines.join('\n'))

function getLocaleDate () {
var now = new Date()

return zeroPad(now.getFullYear(), 4) + '-' +
zeroPad(now.getMonth() + 1, 2) + '-' +
zeroPad(now.getDate(), 2)
}

function repeat (str, length) {
var out = ''

for (var i = 0; i < length; i++) {
out += str
}

return out
}

function zeroPad (number, length) {
var num = number.toString()

while (num.length < length) {
num = '0' + num
}

return num
}

0 comments on commit a1949cb

Please sign in to comment.