summaryrefslogtreecommitdiff
path: root/platform/ios/scripts/release-notes.js
blob: dc2b296e21dce729c6b41f9dcb38a50fbcddd502 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env node

const fs = require('fs');
const execSync = require('child_process').execSync;
const ejs = require('ejs');
const _ = require('lodash');
const semver = require('semver');

const changelog = fs.readFileSync('platform/ios/CHANGELOG.md', 'utf8');

let outputMode = {};
switch(process.argv[2]) {
    case "jazzy":
        outputMode.isJazzy = true; break;
    case "github":
    default:
        outputMode.isGitHub = true;
}

/*
  Find current and immediately previous releases by parsing git tags.
*/
let currentVersion = execSync('git describe --tags --match=ios-v*.*.* --abbrev=0')
    .toString()
    .trim()
    .replace('ios-v', '');

let gitTags = execSync('git tag --list ios-v*.*.*')
    .toString()
    .split('\n')
    .map(function (tag) {
        tag = tag.replace('ios-v', '').trim();
        return semver.clean(tag);
    });
let previousVersion = semver.maxSatisfying(gitTags, "<" + currentVersion);

/*
  Parse the raw changelog text and split it into individual releases.

  This regular expression:
    - Matches lines starting with "## x.x.x".
    - Groups the version number.
    - Skips the (optional) release date.
    - Groups the changelog content.
    - Ends when another "## x.x.x" is found.
*/
const regex = /^## (\d+\.\d+\.\d+).*?\n(.+?)(?=\n^## \d+\.\d+\.\d+.*?\n)/gms;

let releaseNotes = [];
while (match = regex.exec(changelog)) {
    releaseNotes.push({
        'version': match[1],
        'changelog': match[2].trim(),
    });
}

/*
  Match the current tag with the most appropriate release notes.
*/
const versionsInReleaseNotes = _.map(releaseNotes, 'version');
const bestReleaseNotesForCurrentVersion = semver.minSatisfying(versionsInReleaseNotes, ">=" + currentVersion);
const currentReleaseNotes = _.find(releaseNotes, { version: bestReleaseNotesForCurrentVersion });

/*
  Fill and print the release notes template.
*/
let templatedReleaseNotes;

if (outputMode.isGitHub) {
    templatedReleaseNotes = ejs.render(fs.readFileSync('platform/ios/scripts/release-notes-github.md.ejs', 'utf8'), {
        'CURRENTVERSION': currentVersion,
        'PREVIOUSVERSION': previousVersion,
        'CHANGELOG': currentReleaseNotes.changelog,
        'isPrerelease': semver.prerelease(currentVersion)
    });
}

if (outputMode.isJazzy) {
    const minorReleaseSeries = semver.major(currentVersion) + "." + semver.minor(currentVersion) + ".0";
    const range = ">=" + minorReleaseSeries + " <" + currentVersion;
    const otherReleasesInSeries = _.filter(releaseNotes, function(release) {
        return semver.satisfies(release.version, range);
    });

    templatedReleaseNotes = ejs.render(fs.readFileSync('platform/ios/scripts/release-notes-jazzy.md.ejs', 'utf8'), {
        'CURRENTVERSION': currentVersion,
        'CURRENTCHANGELOG': currentReleaseNotes.changelog,
        'OTHERRELEASES': otherReleasesInSeries
    });
}

process.stdout.write(templatedReleaseNotes);