From 3e64b3da398a90e6ddfc287ebf28ec780b64b56f Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 18 Jun 2018 19:00:39 +0200 Subject: Use semver library instead of semverUtils --- packages/monorepo-scripts/package.json | 2 + packages/monorepo-scripts/src/prepublish_checks.ts | 10 ++-- packages/monorepo-scripts/src/publish.ts | 10 ++-- .../monorepo-scripts/src/utils/changelog_utils.ts | 5 +- .../monorepo-scripts/src/utils/semver_utils.ts | 56 ---------------------- packages/monorepo-scripts/src/utils/utils.ts | 7 --- 6 files changed, 17 insertions(+), 73 deletions(-) delete mode 100644 packages/monorepo-scripts/src/utils/semver_utils.ts (limited to 'packages/monorepo-scripts') diff --git a/packages/monorepo-scripts/package.json b/packages/monorepo-scripts/package.json index 6ce889907..5fbf7dbdf 100644 --- a/packages/monorepo-scripts/package.json +++ b/packages/monorepo-scripts/package.json @@ -38,6 +38,7 @@ "make-promises-safe": "^1.1.0", "npm-run-all": "^4.1.2", "shx": "^0.2.2", + "@types/semver": "5.5.0", "tslint": "5.8.0", "typescript": "2.7.1" }, @@ -56,6 +57,7 @@ "publish-release": "0xproject/publish-release", "rimraf": "^2.6.2", "semver-diff": "^2.1.0", + "semver": "5.5.0", "semver-sort": "0.0.4" }, "publishConfig": { diff --git a/packages/monorepo-scripts/src/prepublish_checks.ts b/packages/monorepo-scripts/src/prepublish_checks.ts index 0bd3bb0c1..5848cea8d 100644 --- a/packages/monorepo-scripts/src/prepublish_checks.ts +++ b/packages/monorepo-scripts/src/prepublish_checks.ts @@ -2,12 +2,13 @@ import * as fs from 'fs'; import * as _ from 'lodash'; import * as path from 'path'; import { exec as execAsync } from 'promisify-child-process'; +import semver = require('semver'); +import semverSort = require('semver-sort'); import { constants } from './constants'; import { Changelog, PackageRegistryJson } from './types'; import { changelogUtils } from './utils/changelog_utils'; import { npmUtils } from './utils/npm_utils'; -import { semverUtils } from './utils/semver_utils'; import { utils } from './utils/utils'; async function prepublishChecksAsync(): Promise { @@ -63,7 +64,8 @@ async function checkCurrentVersionMatchesLatestPublishedNPMPackageAsync( continue; // noop for packages not yet published to NPM } const allVersionsIncludingUnpublished = npmUtils.getPreviouslyPublishedVersions(packageRegistryJsonIfExists); - const latestNPMVersion = semverUtils.getLatestVersion(allVersionsIncludingUnpublished); + const sortedVersions = semverSort.desc(allVersionsIncludingUnpublished); + const latestNPMVersion = sortedVersions[0]; if (packageVersion !== latestNPMVersion) { versionMismatches.push({ packageJsonVersion: packageVersion, @@ -96,13 +98,13 @@ async function checkChangelogFormatAsync(updatedPublicLernaPackages: LernaPackag if (!_.isEmpty(changelog)) { const lastEntry = changelog[0]; const doesLastEntryHaveTimestamp = !_.isUndefined(lastEntry.timestamp); - if (semverUtils.lessThan(lastEntry.version, currentVersion)) { + if (semver.lt(lastEntry.version, currentVersion)) { changeLogInconsistencies.push({ packageJsonVersion: currentVersion, changelogVersion: lastEntry.version, packageName, }); - } else if (semverUtils.greaterThan(lastEntry.version, currentVersion) && doesLastEntryHaveTimestamp) { + } else if (semver.gt(lastEntry.version, currentVersion) && doesLastEntryHaveTimestamp) { // Remove incorrectly added timestamp delete changelog[0].timestamp; // Save updated CHANGELOG.json diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 637512a5a..cdd250ec3 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -8,6 +8,7 @@ import opn = require('opn'); import * as path from 'path'; import { exec as execAsync, spawn } from 'promisify-child-process'; import * as prompt from 'prompt'; +import semver = require('semver'); import semverDiff = require('semver-diff'); import semverSort = require('semver-sort'); @@ -129,10 +130,13 @@ async function updateChangeLogsAsync(updatedPublicLernaPackages: LernaPackage[]) ); if (shouldAddNewEntry) { // Create a new entry for a patch version with generic changelog entry. - const nextPatchVersion = utils.getNextPatchVersion(currentVersion); + const nextPatchVersionIfValid = semver.inc(currentVersion, 'patch'); + if (_.isNull(nextPatchVersionIfValid)) { + throw new Error(`Encountered invalid semver version: ${currentVersion} for package: ${packageName}`); + } const newChangelogEntry: VersionChangelog = { timestamp: TODAYS_TIMESTAMP, - version: nextPatchVersion, + version: nextPatchVersionIfValid, changes: [ { note: 'Dependencies updated', @@ -140,7 +144,7 @@ async function updateChangeLogsAsync(updatedPublicLernaPackages: LernaPackage[]) ], }; changelog = [newChangelogEntry, ...changelog]; - packageToVersionChange[packageName] = semverDiff(currentVersion, nextPatchVersion); + packageToVersionChange[packageName] = semverDiff(currentVersion, nextPatchVersionIfValid); } else { // Update existing entry with timestamp const lastEntry = changelog[0]; diff --git a/packages/monorepo-scripts/src/utils/changelog_utils.ts b/packages/monorepo-scripts/src/utils/changelog_utils.ts index 4e09fc842..a589fe985 100644 --- a/packages/monorepo-scripts/src/utils/changelog_utils.ts +++ b/packages/monorepo-scripts/src/utils/changelog_utils.ts @@ -3,13 +3,12 @@ import * as _ from 'lodash'; import * as moment from 'moment'; import * as path from 'path'; import { exec as execAsync } from 'promisify-child-process'; +import semver = require('semver'); import semverSort = require('semver-sort'); import { constants } from '../constants'; import { Change, Changelog, VersionChangelog } from '../types'; -import { semverUtils } from './semver_utils'; - const CHANGELOG_MD_HEADER = `