aboutsummaryrefslogtreecommitdiffstats
path: root/packages/monorepo-scripts/src/utils.ts
blob: 3a16bf91d0c7ddfb9051b081aeca5e7a141b9226 (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
import * as fs from 'fs';
import lernaGetPackages = require('lerna-get-packages');
import * as _ from 'lodash';
import { exec as execAsync, spawn } from 'promisify-child-process';

import { constants } from './constants';
import { UpdatedPackage } from './types';

export const utils = {
    log(...args: any[]): void {
        console.log(...args); // tslint:disable-line:no-console
    },
    getNextPatchVersion(currentVersion: string): string {
        const versionSegments = currentVersion.split('.');
        const patch = _.parseInt(_.last(versionSegments) as string);
        const newPatch = patch + 1;
        const newPatchVersion = `${versionSegments[0]}.${versionSegments[1]}.${newPatch}`;
        return newPatchVersion;
    },
    async prettifyAsync(filePath: string, cwd: string) {
        await execAsync(`prettier --write ${filePath} --config .prettierrc`, {
            cwd,
        });
    },
    async getUpdatedLernaPackagesAsync(shouldIncludePrivate: boolean): Promise<LernaPackage[]> {
        const updatedPublicPackages = await this.getLernaUpdatedPackagesAsync(shouldIncludePrivate);
        const updatedPackageNames = _.map(updatedPublicPackages, pkg => pkg.name);

        const allLernaPackages = lernaGetPackages(constants.monorepoRootPath);
        const updatedPublicLernaPackages = _.filter(allLernaPackages, pkg => {
            return _.includes(updatedPackageNames, pkg.package.name);
        });
        return updatedPublicLernaPackages;
    },
    async getLernaUpdatedPackagesAsync(shouldIncludePrivate: boolean): Promise<UpdatedPackage[]> {
        const result = await execAsync(`${constants.lernaExecutable} updated --json`, {
            cwd: constants.monorepoRootPath,
        });
        const updatedPackages = JSON.parse(result.stdout);
        if (!shouldIncludePrivate) {
            const updatedPublicPackages = _.filter(updatedPackages, updatedPackage => !updatedPackage.private);
            return updatedPublicPackages;
        }
        return updatedPackages;
    },
    getChangelogJSONIfExists(changelogPath: string) {
        let changelogJSON: string;
        try {
            changelogJSON = fs.readFileSync(changelogPath, 'utf-8');
            return changelogJSON;
        } catch (err) {
            return undefined;
        }
    },
    getChangelogJSONOrCreateIfMissing(changelogPath: string): string {
        const changelogIfExists = this.getChangelogJSONIfExists(changelogPath);
        if (_.isUndefined(changelogIfExists)) {
            // If none exists, create new, empty one.
            const emptyChangelogJSON = JSON.stringify([], null, 4);
            fs.writeFileSync(changelogPath, emptyChangelogJSON);
            return emptyChangelogJSON;
        }
        return changelogIfExists;
    },
};