From 7dd3b2d38b4e4ee2f1daa24c90f503b2e7ad0422 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 19 Apr 2018 11:40:22 +0900 Subject: Add removeGitTags script that can be run after a failed Lerna publish --- packages/monorepo-scripts/src/remove_tags.ts | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 packages/monorepo-scripts/src/remove_tags.ts (limited to 'packages/monorepo-scripts/src/remove_tags.ts') diff --git a/packages/monorepo-scripts/src/remove_tags.ts b/packages/monorepo-scripts/src/remove_tags.ts new file mode 100644 index 000000000..a91c6ec39 --- /dev/null +++ b/packages/monorepo-scripts/src/remove_tags.ts @@ -0,0 +1,56 @@ +#!/usr/bin/env node + +import lernaGetPackages = require('lerna-get-packages'); +import * as _ from 'lodash'; +import * as path from 'path'; +import { exec as execAsync } from 'promisify-child-process'; +import semverSort = require('semver-sort'); + +import { constants } from './constants'; +import { Changelog } from './types'; +import { utils } from './utils'; + +(async () => { + const shouldIncludePrivate = true; + const updatedPublicLernaPackages = await utils.getUpdatedLernaPackagesAsync(shouldIncludePrivate); + + for (const lernaPackage of updatedPublicLernaPackages) { + const packageName = lernaPackage.package.name; + const currentVersion = lernaPackage.package.version; + const changelogJSONPath = path.join(lernaPackage.location, 'CHANGELOG.json'); + const changelogJSONIfExists = utils.getChangelogJSONIfExists(changelogJSONPath); + + let latestChangelogVersion: string; + if (!_.isUndefined(changelogJSONIfExists)) { + let changelogs: Changelog[]; + try { + changelogs = JSON.parse(changelogJSONIfExists); + } catch (err) { + throw new Error( + `${lernaPackage.package.name}'s CHANGELOG.json contains invalid JSON. Please fix and try again.`, + ); + } + latestChangelogVersion = changelogs[0].version; + } else { + latestChangelogVersion = utils.getNextPatchVersion(currentVersion); + } + + const sortedVersions = semverSort.desc([latestChangelogVersion, currentVersion]); + if (sortedVersions[0] === latestChangelogVersion && latestChangelogVersion !== currentVersion) { + const tagName = `${packageName}@${latestChangelogVersion}`; + try { + await execAsync(`git tag -d ${tagName}`, { cwd: constants.monorepoRootPath }); + utils.log(`removed tag: ${tagName}`); + } catch (err) { + if (_.includes(err.message, 'not found')) { + utils.log(`Could not find tag: ${tagName}`); + } else { + throw err; + } + } + } + } +})().catch(err => { + utils.log(err); + process.exit(1); +}); -- cgit v1.2.3