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

import * as _ from 'lodash';
import * as path from 'path';
import { exec as execAsync } from 'promisify-child-process';
import semverSort = require('semver-sort');

import { constants } from './constants';
import { Changelog } from './types';
import { utils } from './utils/utils';

(async () => {
    const shouldIncludePrivate = true;
    const updatedPublicLernaPackages = await utils.getUpdatedLernaPackagesAsync(shouldIncludePrivate);

    for (const lernaPackage of updatedPublicLernaPackages) {
        const packageName = lernaPackage.package.name;
        const currentVersion = lernaPackage.package.version;
        const changelogJSONPath = path.join(lernaPackage.location, 'CHANGELOG.json');
        // Private packages don't have changelogs, and their versions are always incremented
        // by a patch version.
        const changelogJSONIfExists = utils.getChangelogJSONIfExists(changelogJSONPath);

        let latestChangelogVersion: string;
        if (!_.isUndefined(changelogJSONIfExists)) {
            let changelogs: Changelog;
            try {
                changelogs = JSON.parse(changelogJSONIfExists);
            } catch (err) {
                throw new Error(
                    `${lernaPackage.package.name}'s CHANGELOG.json contains invalid JSON. Please fix and try again.`,
                );
            }
            latestChangelogVersion = changelogs[0].version;
        } else {
            latestChangelogVersion = utils.getNextPatchVersion(currentVersion);
        }

        const sortedVersions = semverSort.desc([latestChangelogVersion, currentVersion]);
        if (sortedVersions[0] === latestChangelogVersion && latestChangelogVersion !== currentVersion) {
            const tagName = `${packageName}@${latestChangelogVersion}`;
            try {
                await execAsync(`git tag -d ${tagName}`, { cwd: constants.monorepoRootPath });
                utils.log(`removed tag: ${tagName}`);
            } catch (err) {
                if (_.includes(err.message, 'not found')) {
                    utils.log(`Could not find tag: ${tagName}`);
                } else {
                    throw err;
                }
            }
        }
    }
})().catch(err => {
    utils.log(err);
    process.exit(1);
});