diff options
author | Fabio Berger <me@fabioberger.com> | 2018-06-18 22:55:59 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-06-19 05:06:32 +0800 |
commit | 8633fa702436cceeafa52ec39a7fabb5b2650c38 (patch) | |
tree | 0702ff5b590396e86f71508a16a87b7440ffe0c0 /packages/monorepo-scripts/src/utils/changelog_utils.ts | |
parent | 19668b9b48eb08645f500dd8453b8cb2f7abc400 (diff) | |
download | dexon-0x-contracts-8633fa702436cceeafa52ec39a7fabb5b2650c38.tar.gz dexon-0x-contracts-8633fa702436cceeafa52ec39a7fabb5b2650c38.tar.zst dexon-0x-contracts-8633fa702436cceeafa52ec39a7fabb5b2650c38.zip |
Add more prepublish checks
Diffstat (limited to 'packages/monorepo-scripts/src/utils/changelog_utils.ts')
-rw-r--r-- | packages/monorepo-scripts/src/utils/changelog_utils.ts | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/packages/monorepo-scripts/src/utils/changelog_utils.ts b/packages/monorepo-scripts/src/utils/changelog_utils.ts index edfe65a80..4e09fc842 100644 --- a/packages/monorepo-scripts/src/utils/changelog_utils.ts +++ b/packages/monorepo-scripts/src/utils/changelog_utils.ts @@ -1,8 +1,15 @@ +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 semverSort = require('semver-sort'); +import { constants } from '../constants'; import { Change, Changelog, VersionChangelog } from '../types'; +import { semverUtils } from './semver_utils'; + const CHANGELOG_MD_HEADER = ` <!-- This file is auto-generated using the monorepo-scripts package. Don't edit directly. @@ -44,12 +51,58 @@ export const changelogUtils = { return changelogMd; }, - shouldAddNewChangelogEntry(currentVersion: string, changelog: Changelog): boolean { + shouldAddNewChangelogEntry(packageName: string, currentVersion: string, changelog: Changelog): boolean { if (_.isEmpty(changelog)) { return true; } const lastEntry = changelog[0]; + if (semverUtils.lessThan(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 = this.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 this.prettifyAsync(changelogJSONPath, constants.monorepoRootPath); + }, + async writeChangelogMdFileAsync(packageLocation: string, changelog: Changelog): Promise<void> { + const changelogMarkdownPath = path.join(packageLocation, 'CHANGELOG.md'); + fs.writeFileSync(changelogMarkdownPath, JSON.stringify(changelog, null, '\t')); + await this.prettifyAsync(changelogMarkdownPath, constants.monorepoRootPath); + }, + async prettifyAsync(filePath: string, cwd: string): Promise<void> { + await execAsync(`prettier --write ${filePath} --config .prettierrc`, { + cwd, + }); + }, }; |