summaryrefslogtreecommitdiffhomepage
path: root/gui/src/main/changelog.ts
blob: 978b0b28e66d191a04e70fe60a3ce5f5db72d79d (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
import fs from 'fs';
import path from 'path';

import { IChangelog } from '../shared/ipc-types';
import log from '../shared/logging';

// Reads and parses the changelog file.
export function readChangelog(): IChangelog {
  try {
    const changelogPath = path.join(__dirname, '..', '..', '..', 'changes.txt');
    const contents = fs.readFileSync(changelogPath).toString();
    return parseChangelog(contents);
  } catch (e) {
    const error = e as Error;
    log.error('Failed to read changelog.txt', error.message);
    return [];
  }
}

// Parses the contents of the changelog file and returns all relevant items.
export function parseChangelog(changelog: string): IChangelog {
  const items = changelog
    .split('\n')
    .map((item) => item.trim())
    .filter((item) => item !== '');
  return filterForPlatform(items);
}

// Filters the changelog items based on platform
function filterForPlatform(items: Array<string>): IChangelog {
  return items
    .map((item) => {
      // Extracts the platforms from from the string if there are any specified. Platforms are
      // specified within brackets with separated with a comma.
      const platforms = item
        .match(/^\[.*?\]/)
        ?.flatMap((match) => match.slice(1, -1).split(','))
        .map((platform) => platform.trim());
      if (!platforms || isPlatform(platforms)) {
        // If there are no platforms specified or if the current platform matches one of the
        // specified, then the item is included.
        return item.replace(/^\[.*?\]/, '').trim();
      } else {
        return undefined;
      }
    })
    .filter((item): item is string => item !== undefined);
}

// Checks if an OS name corresponds to the current platform.
function isPlatform(platformNames: Array<string>): boolean {
  const platforms = platformNames.map((platformName) => {
    switch (platformName.toLowerCase()) {
      case 'windows':
        return 'win32';
      case 'macos':
        return 'darwin';
      case 'linux':
        return 'linux';
      default:
        return platformName;
    }
  });

  return platforms.includes(process.platform);
}