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/utils.ts | |
parent | 19668b9b48eb08645f500dd8453b8cb2f7abc400 (diff) | |
download | dexon-sol-tools-8633fa702436cceeafa52ec39a7fabb5b2650c38.tar dexon-sol-tools-8633fa702436cceeafa52ec39a7fabb5b2650c38.tar.gz dexon-sol-tools-8633fa702436cceeafa52ec39a7fabb5b2650c38.tar.bz2 dexon-sol-tools-8633fa702436cceeafa52ec39a7fabb5b2650c38.tar.lz dexon-sol-tools-8633fa702436cceeafa52ec39a7fabb5b2650c38.tar.xz dexon-sol-tools-8633fa702436cceeafa52ec39a7fabb5b2650c38.tar.zst dexon-sol-tools-8633fa702436cceeafa52ec39a7fabb5b2650c38.zip |
Add more prepublish checks
Diffstat (limited to 'packages/monorepo-scripts/src/utils/utils.ts')
-rw-r--r-- | packages/monorepo-scripts/src/utils/utils.ts | 98 |
1 files changed, 77 insertions, 21 deletions
diff --git a/packages/monorepo-scripts/src/utils/utils.ts b/packages/monorepo-scripts/src/utils/utils.ts index 0b8ac4c0b..93de0d940 100644 --- a/packages/monorepo-scripts/src/utils/utils.ts +++ b/packages/monorepo-scripts/src/utils/utils.ts @@ -1,10 +1,11 @@ -import * as fs from 'fs'; import lernaGetPackages = require('lerna-get-packages'); import * as _ from 'lodash'; import { exec as execAsync } from 'promisify-child-process'; import { constants } from '../constants'; -import { UpdatedPackage } from '../types'; +import { GitTagsByPackageName, UpdatedPackage } from '../types'; + +import { changelogUtils } from './changelog_utils'; export const utils = { log(...args: any[]): void { @@ -17,11 +18,6 @@ export const utils = { const newPatchVersion = `${versionSegments[0]}.${versionSegments[1]}.${newPatch}`; return newPatchVersion; }, - async prettifyAsync(filePath: string, cwd: string): Promise<void> { - await execAsync(`prettier --write ${filePath} --config .prettierrc`, { - cwd, - }); - }, async getUpdatedLernaPackagesAsync(shouldIncludePrivate: boolean): Promise<LernaPackage[]> { const updatedPublicPackages = await this.getLernaUpdatedPackagesAsync(shouldIncludePrivate); const updatedPackageNames = _.map(updatedPublicPackages, pkg => pkg.name); @@ -43,22 +39,82 @@ export const utils = { } return updatedPackages; }, - getChangelogJSONIfExists(changelogPath: string): string | undefined { - try { - const changelogJSON = fs.readFileSync(changelogPath, 'utf-8'); - return changelogJSON; - } catch (err) { - return undefined; + async getNextPackageVersionAsync( + currentVersion: string, + packageName: string, + packageLocation: string, + ): Promise<string> { + let nextVersion; + const changelog = changelogUtils.getChangelogOrCreateIfMissing(packageName, packageLocation); + if (_.isEmpty(changelog)) { + nextVersion = this.getNextPatchVersion(currentVersion); } + const lastEntry = changelog[0]; + nextVersion = + lastEntry.version === currentVersion ? this.getNextPatchVersion(currentVersion) : lastEntry.version; + return nextVersion; + }, + async getRemoteGitTagsAsync(): Promise<string[]> { + const result = await execAsync(`git ls-remote --tags`, { + cwd: constants.monorepoRootPath, + }); + const tagsString = result.stdout; + const tagOutputs: string[] = tagsString.split('\n'); + const tags = _.compact( + _.map(tagOutputs, tagOutput => { + const tag = tagOutput.split('refs/tags/')[1]; + // Tags with `^{}` are duplicateous so we ignore them + // Source: https://stackoverflow.com/questions/15472107/when-listing-git-ls-remote-why-theres-after-the-tag-name + if (_.endsWith(tag, '^{}')) { + return undefined; + } + return tag; + }), + ); + return tags; + }, + async getLocalGitTagsAsync(): Promise<string[]> { + const result = await execAsync(`git tags`, { + cwd: constants.monorepoRootPath, + }); + const tagsString = result.stdout; + const tags = tagsString.split('\n'); + return tags; }, - getChangelogJSONOrCreateIfMissing(changelogPath: string): string { - const changelogIfExists = this.getChangelogJSONIfExists(changelogPath); - if (_.isUndefined(changelogIfExists)) { - // If none exists, create new, empty one. - const emptyChangelogJSON = JSON.stringify([]); - fs.writeFileSync(changelogPath, emptyChangelogJSON); - return emptyChangelogJSON; + async getGitTagsByPackageNameAsync(packageNames: string[], gitTags: string[]): Promise<GitTagsByPackageName> { + const tagVersionByPackageName: GitTagsByPackageName = {}; + _.each(gitTags, tag => { + const packageNameIfExists = _.find(packageNames, name => { + return _.includes(tag, `${name}@`); + }); + if (_.isUndefined(packageNameIfExists)) { + return; // ignore tags not related to a package we care about. + } + const splitTag = tag.split(`${packageNameIfExists}@`); + if (splitTag.length !== 2) { + throw new Error(`Unexpected tag name found: ${tag}`); + } + const version = splitTag[1]; + (tagVersionByPackageName[packageNameIfExists] || (tagVersionByPackageName[packageNameIfExists] = [])).push( + version, + ); + }); + return tagVersionByPackageName; + }, + async removeLocalTagAsync(tagName: string): Promise<void> { + const result = await execAsync(`git tag -d ${tagName}`, { + cwd: constants.monorepoRootPath, + }); + if (!_.isEmpty(result.stderr)) { + throw new Error(`Failed to delete local git tag. Got err: ${result.stderr}`); + } + }, + async removeRemoteTagAsync(tagName: string): Promise<void> { + const result = await execAsync(`git push origin ${tagName}`, { + cwd: constants.monorepoRootPath, + }); + if (!_.isEmpty(result.stderr)) { + throw new Error(`Failed to delete remote git tag. Got err: ${result.stderr}`); } - return changelogIfExists; }, }; |