aboutsummaryrefslogtreecommitdiffstats
path: root/development/version-bump.js
blob: fa20e9f75b97b1ef933879ce1709283eb4814e40 (plain) (blame)
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
const clone = require('clone')

async function versionBump (bumpType, changelog, oldManifest) {
  const manifest = clone(oldManifest)
  const newVersion = newVersionFrom(manifest, bumpType)

  manifest.version = newVersion
  const date = (new Date()).toDateString()

  const logHeader = `\n## ${newVersion} ${date}`
  const logLines = changelog.split('\n')
  for (let i = 0; i < logLines.length; i++) {
    if (logLines[i].includes('Current Master')) {
      logLines.splice(i + 1, 0, logHeader)
      break
    }
  }

  return {
    version: newVersion,
    manifest: manifest,
    changelog: logLines.join('\n'),
  }
}

function newVersionFrom (manifest, bumpType) {
  const string = manifest.version
  const segments = string.split('.').map((str) => parseInt(str))

  switch (bumpType) {
    case 'major':
      segments[0] += 1
      segments[1] = 0
      segments[2] = 0
      break
    case 'minor':
      segments[1] += 1
      segments[2] = 0
      break
    case 'patch':
      segments[2] += 1
      break
  }

  return segments.map(String).join('.')
}

module.exports = versionBump