Back-port prePublish fixes

This commit is contained in:
Fabio Berger
2018-07-09 14:05:29 +02:00
parent e7e9ddf232
commit c28289e9a8
2 changed files with 40 additions and 17 deletions

View File

@@ -34,17 +34,17 @@ async function checkGitTagsForNextVersionAndDeleteIfExistAsync(
const packageLocation = lernaPackage.location;
const nextVersion = await utils.getNextPackageVersionAsync(currentVersion, packageName, packageLocation);
const localTagVersions = localTagVersionsByPackageName[packageName];
if (_.includes(localTagVersions, nextVersion)) {
const tagName = `${packageName}@${nextVersion}`;
await utils.removeLocalTagAsync(tagName);
}
const remoteTagVersions = remoteTagVersionsByPackageName[packageName];
if (_.includes(remoteTagVersions, nextVersion)) {
const tagName = `:refs/tags/${packageName}@${nextVersion}`;
await utils.removeRemoteTagAsync(tagName);
}
const localTagVersions = localTagVersionsByPackageName[packageName];
if (_.includes(localTagVersions, nextVersion)) {
const tagName = `${packageName}@${nextVersion}`;
await utils.removeLocalTagAsync(tagName);
}
}
}
@@ -166,9 +166,25 @@ async function checkPublishRequiredSetupAsync(): Promise<void> {
} catch (err) {
throw new Error('You must setup your AWS credentials by running `aws configure`. Do this and try again.');
}
utils.log('Checking that git branch is up to date with upstream...');
await execAsync('git fetch');
const res = await execAsync('git status -bs'); // s - short format, b - branch info
/**
* Possible outcomes
* ## branch_name...origin/branch_name [behind n]
* ## branch_name...origin/branch_name [ahead n]
* ## branch_name...origin/branch_name
*/
const gitShortStatusHeader = res.stdout.split('\n')[0];
if (gitShortStatusHeader.includes('behind')) {
throw new Error('Your branch is behind upstream. Please pull before publishing.');
} else if (gitShortStatusHeader.includes('ahead')) {
throw new Error('Your branch is ahead of upstream. Please push before publishing.');
}
}
prepublishChecksAsync().catch(err => {
utils.log(err);
utils.log(err.message);
process.exit(1);
});

View File

@@ -44,6 +44,9 @@ export const utils = {
nextVersionIfValid = semver.inc(currentVersion, 'patch');
}
const lastEntry = changelog[0];
if (semver.gt(currentVersion, lastEntry.version)) {
throw new Error(`Package.json version cannot be greater then last CHANGELOG entry. Check: ${packageName}`);
}
nextVersionIfValid = semver.eq(lastEntry.version, currentVersion)
? semver.inc(currentVersion, 'patch')
: lastEntry.version;
@@ -100,19 +103,23 @@ export const utils = {
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}`);
try {
await execAsync(`git tag -d ${tagName}`, {
cwd: constants.monorepoRootPath,
});
} catch (err) {
throw new Error(`Failed to delete local git tag. Got err: ${err}`);
}
this.log(`Removed local tag: ${tagName}`);
},
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}`);
try {
await execAsync(`git push origin ${tagName}`, {
cwd: constants.monorepoRootPath,
});
} catch (err) {
throw new Error(`Failed to delete remote git tag. Got err: ${err}`);
}
this.log(`Removed remote tag: ${tagName}`);
},
};