aboutsummaryrefslogtreecommitdiffstats
path: root/packages/monorepo-scripts/src/utils/changelog_utils.ts
blob: 0b46bf670d85a454fb5de780e17b59c509bd7915 (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
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
93
94
95
96
97
98
99
100
101
102
import * as fs from 'fs';
import * as _ from 'lodash';
import * as moment from 'moment';
import * as path from 'path';
import { exec as execAsync } from 'promisify-child-process';
import semver = require('semver');

import { constants } from '../constants';
import { Change, Changelog, VersionChangelog } from '../types';

const CHANGELOG_MD_HEADER = `
<!--
changelogUtils.file is auto-generated using the monorepo-scripts package. Don't edit directly.
Edit the package's CHANGELOG.json file only.
-->

CHANGELOG
`;

export const changelogUtils = {
    getChangelogMdTitle(versionChangelog: VersionChangelog): string {
        // Use UTC rather than the local machines time (formatted date time is +0:00)
        const date = moment.utc(`${versionChangelog.timestamp}`, 'X').format('MMMM D, YYYY');
        const title = `\n## v${versionChangelog.version} - _${date}_\n\n`;
        return title;
    },
    getChangelogMdChange(change: Change): string {
        let line = `    * ${change.note}`;
        if (!_.isUndefined(change.pr)) {
            line += ` (#${change.pr})`;
        }
        return line;
    },
    generateChangelogMd(changelog: Changelog): string {
        let changelogMd = CHANGELOG_MD_HEADER;
        _.each(changelog, versionChangelog => {
            const title = changelogUtils.getChangelogMdTitle(versionChangelog);
            changelogMd += title;
            const changelogVersionLines = _.map(
                versionChangelog.changes,
                changelogUtils.getChangelogMdChange.bind(changelogUtils),
            );
            changelogMd += `${_.join(changelogVersionLines, '\n')}`;
        });

        return changelogMd;
    },
    shouldAddNewChangelogEntry(packageName: string, currentVersion: string, changelog: Changelog): boolean {
        if (_.isEmpty(changelog)) {
            return true;
        }
        const lastEntry = changelog[0];
        if (semver.lt(lastEntry.version, currentVersion)) {
            throw new Error(
                `Found CHANGELOG version lower then current package version. ${packageName} current: ${currentVersion}, Changelog: ${
                    lastEntry.version
                }`,
            );
        }
        const isLastEntryCurrentVersion = lastEntry.version === currentVersion;
        return isLastEntryCurrentVersion;
    },
    getChangelogJSONIfExists(changelogPath: string): string | undefined {
        try {
            const changelogJSON = fs.readFileSync(changelogPath, 'utf-8');
            return changelogJSON;
        } catch (err) {
            return undefined;
        }
    },
    getChangelogOrCreateIfMissing(packageName: string, packageLocation: string): Changelog {
        const changelogJSONPath = path.join(packageLocation, 'CHANGELOG.json');
        let changelogJsonIfExists = changelogUtils.getChangelogJSONIfExists(changelogJSONPath);
        if (_.isUndefined(changelogJsonIfExists)) {
            // If none exists, create new, empty one.
            changelogJsonIfExists = '[]';
            fs.writeFileSync(changelogJSONPath, changelogJsonIfExists);
        }
        let changelog: Changelog;
        try {
            changelog = JSON.parse(changelogJsonIfExists);
        } catch (err) {
            throw new Error(`${packageName}'s CHANGELOG.json contains invalid JSON. Please fix and try again.`);
        }
        return changelog;
    },
    async writeChangelogJsonFileAsync(packageLocation: string, changelog: Changelog): Promise<void> {
        const changelogJSONPath = path.join(packageLocation, 'CHANGELOG.json');
        fs.writeFileSync(changelogJSONPath, JSON.stringify(changelog, null, '\t'));
        await changelogUtils.prettifyAsync(changelogJSONPath, constants.monorepoRootPath);
    },
    async writeChangelogMdFileAsync(packageLocation: string, changelogMdString: string): Promise<void> {
        const changelogMarkdownPath = path.join(packageLocation, 'CHANGELOG.md');
        fs.writeFileSync(changelogMarkdownPath, changelogMdString);
        await changelogUtils.prettifyAsync(changelogMarkdownPath, constants.monorepoRootPath);
    },
    async prettifyAsync(filePath: string, cwd: string): Promise<void> {
        await execAsync(`prettier --write ${filePath} --config .prettierrc`, {
            cwd,
        });
    },
};