1+ /* eslint-disable no-console */
2+ /* eslint-env node */
13'use strict' ;
24
35const fs = require ( 'fs' ) ;
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
83101function 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+
98126function 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}
0 commit comments