Skip to content

Commit 6714d49

Browse files
committed
bail if new tag does not directly follow previous version
This is to try and prevent typos that could trigger an incorrect version publish into the future that cannot be undone. refs #4841
1 parent 084eefa commit 6714d49

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎scripts/set-package-version.js‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable no-console */
2+
/* eslint-env node */
13
'use strict';
24

35
const fs = require('fs');
@@ -61,6 +63,22 @@ try {
6163
`New version "${newVersion}" is not >= latest version "${latestVersion}" on this branch.`
6264
);
6365
}
66+
67+
if (hasTag(`v${newVersion}`)) {
68+
throw new Error(`Tag for "${newVersion}" already exists.`);
69+
}
70+
71+
const foundPreviousVersion = versionParser
72+
.getPotentialPreviousStableVersions(`v${newVersion}`)
73+
.every((potentialPreviousVersion) =>
74+
hasTag(`v${potentialPreviousVersion}`)
75+
);
76+
if (!foundPreviousVersion) {
77+
throw new Error(
78+
'Could not find a previous version. The tag must follow a previous stable version number.'
79+
);
80+
}
81+
6482
packageJson.version = newVersion;
6583
fs.writeFileSync('./package.json', JSON.stringify(packageJson), {
6684
encoding: 'utf8',
@@ -82,6 +100,7 @@ function getCommitHash() {
82100

83101
function getLatestVersionTag() {
84102
let commitish = '';
103+
// eslint-disable-next-line no-constant-condition
85104
while (true) {
86105
const tag = exec('git describe --tag --abbrev=0 --match="v*" ' + commitish);
87106
if (!tag) {
@@ -95,6 +114,18 @@ function getLatestVersionTag() {
95114
}
96115
}
97116

117+
function hasTag(tag) {
118+
try {
119+
exec(`git rev-parse "refs/tags/${tag}"`);
120+
return true;
121+
} catch (e) {
122+
return false;
123+
}
124+
}
125+
98126
function exec(cmd) {
99-
return require('child_process').execSync(cmd).toString().trim();
127+
return require('child_process')
128+
.execSync(cmd, { stdio: 'pipe' })
129+
.toString()
130+
.trim();
100131
}

‎scripts/version-parser.js‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const semver = require('semver');
44

5-
const VALID_VERSION_REGEX = /^v\d+\.\d+\.\d+(?:-([a-zA-Z][0-9a-zA-Z-]*))?/;
5+
const VALID_VERSION_REGEX =
6+
/^v(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z][0-9a-zA-Z-]*))?/;
67
const STABLE_VERSION_REGEX = /^v\d+\.\d+\.\d+$/;
78

89
module.exports = {
@@ -57,6 +58,22 @@ module.exports = {
5758
if (!match) {
5859
throw new Error('Invalid version.');
5960
}
60-
return match[1] || 'latest';
61+
return match[4] || 'latest';
62+
},
63+
getPotentialPreviousStableVersions: (version) => {
64+
const match = VALID_VERSION_REGEX.exec(version);
65+
if (!match) {
66+
throw new Error('Invalid version.');
67+
}
68+
69+
const major = parseInt(match[1]);
70+
const minor = parseInt(match[2]);
71+
const patch = parseInt(match[3]);
72+
73+
const versions = [];
74+
if (major > 0) versions.push(`${major - 1}.0.${major === 1 ? '1' : '0'}`);
75+
if (minor > 0) versions.push(`${major}.${minor - 1}.0`);
76+
if (patch > 0) versions.push(`${major}.${minor}.${patch - 1}`);
77+
return versions;
6178
},
6279
};

0 commit comments

Comments
 (0)