From 8633fa702436cceeafa52ec39a7fabb5b2650c38 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 18 Jun 2018 16:55:59 +0200 Subject: Add more prepublish checks --- packages/monorepo-scripts/src/publish.ts | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'packages/monorepo-scripts/src/publish.ts') diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 2efbc8bf2..637512a5a 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -119,19 +119,14 @@ async function updateChangeLogsAsync(updatedPublicLernaPackages: LernaPackage[]) const packageToVersionChange: PackageToVersionChange = {}; for (const lernaPackage of updatedPublicLernaPackages) { const packageName = lernaPackage.package.name; - const changelogJSONPath = path.join(lernaPackage.location, 'CHANGELOG.json'); - const changelogJSON = utils.getChangelogJSONOrCreateIfMissing(changelogJSONPath); - let changelog: Changelog; - try { - changelog = JSON.parse(changelogJSON); - } catch (err) { - throw new Error( - `${lernaPackage.package.name}'s CHANGELOG.json contains invalid JSON. Please fix and try again.`, - ); - } + let changelog = changelogUtils.getChangelogOrCreateIfMissing(packageName, lernaPackage.location); const currentVersion = lernaPackage.package.version; - const shouldAddNewEntry = changelogUtils.shouldAddNewChangelogEntry(currentVersion, changelog); + const shouldAddNewEntry = changelogUtils.shouldAddNewChangelogEntry( + lernaPackage.package.name, + currentVersion, + changelog, + ); if (shouldAddNewEntry) { // Create a new entry for a patch version with generic changelog entry. const nextPatchVersion = utils.getNextPatchVersion(currentVersion); @@ -160,14 +155,11 @@ async function updateChangeLogsAsync(updatedPublicLernaPackages: LernaPackage[]) } // Save updated CHANGELOG.json - fs.writeFileSync(changelogJSONPath, JSON.stringify(changelog, null, '\t')); - await utils.prettifyAsync(changelogJSONPath, constants.monorepoRootPath); + await changelogUtils.writeChangelogJsonFileAsync(lernaPackage.location, changelog); utils.log(`${packageName}: Updated CHANGELOG.json`); // Generate updated CHANGELOG.md const changelogMd = changelogUtils.generateChangelogMd(changelog); - const changelogMdPath = path.join(lernaPackage.location, 'CHANGELOG.md'); - fs.writeFileSync(changelogMdPath, changelogMd); - await utils.prettifyAsync(changelogMdPath, constants.monorepoRootPath); + await changelogUtils.writeChangelogMdFileAsync(lernaPackage.location, changelog); utils.log(`${packageName}: Updated CHANGELOG.md`); } -- cgit v1.2.3 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/src/publish.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'packages/monorepo-scripts/src/publish.ts') 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]; -- cgit v1.2.3 From dcd53c3c5bc251af242166cf758146649eb4185b Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 18 Jun 2018 19:22:31 +0200 Subject: Use semver package instead of getNextPatchVersion --- packages/monorepo-scripts/src/publish.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'packages/monorepo-scripts/src/publish.ts') diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index cdd250ec3..5f4c67e04 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -213,12 +213,16 @@ async function lernaPublishAsync(packageToVersionChange: { [name: string]: strin } function updateVersionNumberIfNeeded(currentVersion: string, proposedNextVersion: string): string { + const updatedVersionIfValid = semver.inc(currentVersion, 'patch'); + if (_.isNull(updatedVersionIfValid)) { + throw new Error(`Encountered invalid semver: ${currentVersion}`); + } if (proposedNextVersion === currentVersion) { - return utils.getNextPatchVersion(currentVersion); + return updatedVersionIfValid; } const sortedVersions = semverSort.desc([proposedNextVersion, currentVersion]); if (sortedVersions[0] !== proposedNextVersion) { - return utils.getNextPatchVersion(currentVersion); + return updatedVersionIfValid; } return proposedNextVersion; } -- cgit v1.2.3 From 880cbd88c2416c9a3281542c68affb862bd90575 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 18 Jun 2018 23:15:52 +0200 Subject: Small fixes --- packages/monorepo-scripts/src/publish.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'packages/monorepo-scripts/src/publish.ts') diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 5f4c67e04..72c6c0a71 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -1,11 +1,9 @@ #!/usr/bin/env node import * as promisify from 'es6-promisify'; -import * as fs from 'fs'; import * as _ from 'lodash'; import * as moment from 'moment'; 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'); @@ -13,7 +11,7 @@ import semverDiff = require('semver-diff'); import semverSort = require('semver-sort'); import { constants } from './constants'; -import { Changelog, PackageToVersionChange, SemVerIndex, VersionChangelog } from './types'; +import { PackageToVersionChange, SemVerIndex, VersionChangelog } from './types'; import { changelogUtils } from './utils/changelog_utils'; import { utils } from './utils/utils'; @@ -163,7 +161,7 @@ async function updateChangeLogsAsync(updatedPublicLernaPackages: LernaPackage[]) utils.log(`${packageName}: Updated CHANGELOG.json`); // Generate updated CHANGELOG.md const changelogMd = changelogUtils.generateChangelogMd(changelog); - await changelogUtils.writeChangelogMdFileAsync(lernaPackage.location, changelog); + await changelogUtils.writeChangelogMdFileAsync(lernaPackage.location, changelogMd); utils.log(`${packageName}: Updated CHANGELOG.md`); } -- cgit v1.2.3