File: version-history.js

package info (click to toggle)
node-serve-static 2.1.0%2B~1.15.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 272 kB
  • sloc: javascript: 897; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 1,535 bytes parent folder | download | duplicates (13)
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
'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
}