Use semver library instead of semverUtils
This commit is contained in:
		| @@ -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": { | ||||
|   | ||||
| @@ -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<void> { | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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]; | ||||
|   | ||||
| @@ -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 = ` | ||||
| <!-- | ||||
| This file is auto-generated using the monorepo-scripts package. Don't edit directly. | ||||
| @@ -56,7 +55,7 @@ export const changelogUtils = { | ||||
|             return true; | ||||
|         } | ||||
|         const lastEntry = changelog[0]; | ||||
|         if (semverUtils.lessThan(lastEntry.version, currentVersion)) { | ||||
|         if (semver.lt(lastEntry.version, currentVersion)) { | ||||
|             throw new Error( | ||||
|                 `Found CHANGELOG version lower then current package version. ${packageName} current: ${currentVersion}, Changelog: ${ | ||||
|                     lastEntry.version | ||||
|   | ||||
| @@ -1,56 +0,0 @@ | ||||
| import * as _ from 'lodash'; | ||||
| import semverSort = require('semver-sort'); | ||||
|  | ||||
| // Regex that matches semantic versions only including digits and dots. | ||||
| const SEM_VER_REGEX = /^(\d+\.){1}(\d+\.){1}(\d+){1}$/gm; | ||||
|  | ||||
| export const semverUtils = { | ||||
|     /** | ||||
|      * Checks whether version a is lessThan version b. Supplied versions must be | ||||
|      * Semantic Versions containing only numbers and dots (e.g 1.4.0). | ||||
|      * @param a version of interest | ||||
|      * @param b version to compare a against | ||||
|      * @return Whether version a is lessThan version b | ||||
|      */ | ||||
|     lessThan(a: string, b: string): boolean { | ||||
|         this.assertValidSemVer('a', a); | ||||
|         this.assertValidSemVer('b', b); | ||||
|         if (a === b) { | ||||
|             return false; | ||||
|         } | ||||
|         const sortedVersions = semverSort.desc([a, b]); | ||||
|         const isALessThanB = sortedVersions[0] === b; | ||||
|         return isALessThanB; | ||||
|     }, | ||||
|     /** | ||||
|      * Checks whether version a is greaterThan version b. Supplied versions must be | ||||
|      * Semantic Versions containing only numbers and dots (e.g 1.4.0). | ||||
|      * @param a version of interest | ||||
|      * @param b version to compare a against | ||||
|      * @return Whether version a is greaterThan version b | ||||
|      */ | ||||
|     greaterThan(a: string, b: string): boolean { | ||||
|         this.assertValidSemVer('a', a); | ||||
|         this.assertValidSemVer('b', b); | ||||
|         if (a === b) { | ||||
|             return false; | ||||
|         } | ||||
|         const sortedVersions = semverSort.desc([a, b]); | ||||
|         const isAGreaterThanB = sortedVersions[0] === a; | ||||
|         return isAGreaterThanB; | ||||
|     }, | ||||
|     assertValidSemVer(variableName: string, version: string): void { | ||||
|         if (!version.match(SEM_VER_REGEX)) { | ||||
|             throw new Error( | ||||
|                 `SemVer versions should only contain numbers and dots. Encountered: ${variableName} = ${version}`, | ||||
|             ); | ||||
|         } | ||||
|     }, | ||||
|     getLatestVersion(versions: string[]): string { | ||||
|         _.each(versions, version => { | ||||
|             this.assertValidSemVer('version', version); | ||||
|         }); | ||||
|         const sortedVersions = semverSort.desc(versions); | ||||
|         return sortedVersions[0]; | ||||
|     }, | ||||
| }; | ||||
| @@ -11,13 +11,6 @@ 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 getUpdatedLernaPackagesAsync(shouldIncludePrivate: boolean): Promise<LernaPackage[]> { | ||||
|         const updatedPublicPackages = await this.getLernaUpdatedPackagesAsync(shouldIncludePrivate); | ||||
|         const updatedPackageNames = _.map(updatedPublicPackages, pkg => pkg.name); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user