aboutsummaryrefslogtreecommitdiffstats
path: root/packages/monorepo-scripts/src/utils
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-06-02 04:12:45 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-06-02 04:12:45 +0800
commit06e5fc233c512d152d61c4acbed8d9344e769abe (patch)
tree4e5f573a7e956fae82bb0e112801f924c3e74778 /packages/monorepo-scripts/src/utils
parent04a0eae241216cfe822346d02992d24082d94cab (diff)
downloaddexon-sol-tools-06e5fc233c512d152d61c4acbed8d9344e769abe.tar
dexon-sol-tools-06e5fc233c512d152d61c4acbed8d9344e769abe.tar.gz
dexon-sol-tools-06e5fc233c512d152d61c4acbed8d9344e769abe.tar.bz2
dexon-sol-tools-06e5fc233c512d152d61c4acbed8d9344e769abe.tar.lz
dexon-sol-tools-06e5fc233c512d152d61c4acbed8d9344e769abe.tar.xz
dexon-sol-tools-06e5fc233c512d152d61c4acbed8d9344e769abe.tar.zst
dexon-sol-tools-06e5fc233c512d152d61c4acbed8d9344e769abe.zip
Refactor changelog utils to a separate module
Diffstat (limited to 'packages/monorepo-scripts/src/utils')
-rw-r--r--packages/monorepo-scripts/src/utils/changelog_utils.ts55
-rw-r--r--packages/monorepo-scripts/src/utils/utils.ts64
2 files changed, 119 insertions, 0 deletions
diff --git a/packages/monorepo-scripts/src/utils/changelog_utils.ts b/packages/monorepo-scripts/src/utils/changelog_utils.ts
new file mode 100644
index 000000000..edfe65a80
--- /dev/null
+++ b/packages/monorepo-scripts/src/utils/changelog_utils.ts
@@ -0,0 +1,55 @@
+import * as _ from 'lodash';
+import * as moment from 'moment';
+
+import { Change, Changelog, VersionChangelog } from '../types';
+
+const CHANGELOG_MD_HEADER = `
+<!--
+This 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 {
+ if (_.isUndefined(versionChangelog.timestamp)) {
+ throw new Error(
+ 'All CHANGELOG.json entries must be updated to include a timestamp before generating their MD version',
+ );
+ }
+ const date = moment(`${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(currentVersion: string, changelog: Changelog): boolean {
+ if (_.isEmpty(changelog)) {
+ return true;
+ }
+ const lastEntry = changelog[0];
+ const isLastEntryCurrentVersion = lastEntry.version === currentVersion;
+ return isLastEntryCurrentVersion;
+ },
+};
diff --git a/packages/monorepo-scripts/src/utils/utils.ts b/packages/monorepo-scripts/src/utils/utils.ts
new file mode 100644
index 000000000..8f2a0bbaa
--- /dev/null
+++ b/packages/monorepo-scripts/src/utils/utils.ts
@@ -0,0 +1,64 @@
+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): Promise<void> {
+ 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): string | undefined {
+ try {
+ const 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([]);
+ fs.writeFileSync(changelogPath, emptyChangelogJSON);
+ return emptyChangelogJSON;
+ }
+ return changelogIfExists;
+ },
+};