aboutsummaryrefslogtreecommitdiffstats
path: root/packages/monorepo-scripts/src/convert_changelogs.ts
blob: 76ce50cedb6cf860508f422fd08ff28cda0adb1e (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
#!/usr/bin/env node

import * as fs from 'fs';
import lernaGetPackages = require('lerna-get-packages');
import * as _ from 'lodash';
import * as moment from 'moment';
import * as path from 'path';

import { Changelog, Changes, UpdatedPackage } from './types';
import { utils } from './utils';

const MONOREPO_ROOT_PATH = path.join(__dirname, '../../..');

(async () => {
    const allLernaPackages = lernaGetPackages(MONOREPO_ROOT_PATH);
    const publicLernaPackages = _.filter(allLernaPackages, pkg => !pkg.package.private);
    _.each(publicLernaPackages, lernaPackage => {
        const changelogMdIfExists = getChangelogMdIfExists(lernaPackage.package.name, lernaPackage.location);
        if (_.isUndefined(changelogMdIfExists)) {
            throw new Error(`${lernaPackage.package.name} should have CHANGELOG.md b/c it's public. Add one.`);
        }

        const lines = (changelogMdIfExists as any).split('\n');
        const changelogs: Changelog[] = [];
        let changelog: Changelog = {
            version: '',
            changes: [],
        };
        for (const line of lines) {
            if (_.startsWith(line, '## ')) {
                let version = line.substr(4).split(' - ')[0];
                if (version === '0.x.x') {
                    version = utils.getNextPatchVersion(lernaPackage.package.version);
                }
                const dateStr = line.split('_')[1];
                let date;
                if (!_.includes(dateStr, 'TBD')) {
                    date = moment(dateStr, 'MMMM D, YYYY');
                }
                changelog = {
                    version,
                    changes: [],
                };
                if (!_.isUndefined(date)) {
                    changelog.timestamp = date.unix();
                }
                if (!_.includes(dateStr, 'TBD')) {
                    changelog.isPublished = true;
                }
                (changelogs as any).push(changelog);
            } else if (_.includes(line, '* ')) {
                const note = line.split('* ')[1].split(' (#')[0];
                const prChunk = line.split(' (#')[1];
                let pr;
                if (!_.isUndefined(prChunk)) {
                    pr = prChunk.split(')')[0];
                }
                const changes = {
                    note,
                    pr,
                };
                changelog.changes.push(changes);
            }
        }
        const changelogJson = JSON.stringify(changelogs, null, '\t');
        fs.writeFileSync(`${lernaPackage.location}/CHANGELOG.json`, changelogJson);
    });
})().catch(err => {
    utils.log(err.stdout);
    process.exit(1);
});

function getChangelogMdIfExists(packageName: string, location: string): string | undefined {
    const changelogPath = path.join(location, 'CHANGELOG.md');
    let changelogMd: string;
    try {
        changelogMd = fs.readFileSync(changelogPath, 'utf-8');
        return changelogMd;
    } catch (err) {
        // If none exists, create new, empty one.
        return undefined;
    }
}