33/**
44 * Updates the version in package.json and optionally docsy.dev/hugo.yaml.
55 * Can set the entire version or add/remove a build ID suffix.
6- * The hugo.yaml file is only updated when --version is used.
6+ * The hugo.yaml file is updated when --version is used.
77 *
88 * For usage, see the usage() function below.
99 *
@@ -38,10 +38,15 @@ export function main(
3838
3939 const currentVersion = pkg . version ;
4040 let newVersion ;
41+ const removeDevSuffixOnly = version === '' ;
4142
4243 if ( version !== undefined ) {
43- // --version takes precedence: set the entire version directly
44- newVersion = version ;
44+ if ( removeDevSuffixOnly ) {
45+ newVersion = removeDevSuffix ( currentVersion ) ;
46+ } else {
47+ // --version takes precedence: set the entire version directly
48+ newVersion = version ;
49+ }
4550 } else {
4651 // Use build ID logic: add/remove build ID from base version
4752 const baseVersion = currentVersion . split ( '+' ) [ 0 ] ; // Remove existing build ID if present
@@ -57,7 +62,7 @@ export function main(
5762 updated = true ;
5863 }
5964
60- // Only update hugo.yaml when --version is used
65+ // Update hugo.yaml when --version is used.
6166 let currentHugoVersion = '' ;
6267 if ( version !== undefined ) {
6368 const baseVersion = newVersion . split ( '+' ) [ 0 ] ; // Remove build ID if present
@@ -90,16 +95,19 @@ export function main(
9095const usageText = `
9196Usage: node scripts/set-package-version/index.mjs [options]
9297 Options:
93- --silent, -s Don't log any messages
94- --help, -h Show this help message
95- --version VERS Set the entire version to VERS
98+ --silent| -s Don't log any messages
99+ --help| -h Show this help message
100+ --version|-v VERS Set the entire version to VERS
96101 --id BUILD-ID Set build ID to BUILD-ID (ignored if --version is used)
97102
98103 Behavior:
99- - If --version VERS is provided: sets the version to VERS in both package.json and hugo.yaml
100- - If --id BUILD-ID is provided: adds BUILD-ID to the base version (package.json only)
101- - If --id "" is provided: removes the build ID (package.json only)
102- - If no --version or --id is provided: auto-generates build ID from timestamp (package.json only)
104+ --version VERS : sets the version to VERS in both package.json and hugo.yaml
105+ --version [''] : strips version to be just X.Y.Z in package.json and hugo.yaml
106+ --id BUILD-ID : adds BUILD-ID to the base version (package.json only)
107+ --id [''] : removes the build ID (package.json only)
108+
109+ When neither --version nor --id is provided: auto-generates build ID from
110+ timestamp (package.json only)
103111` ;
104112
105113export function parseArgsAndResolveBuildId ( args , { logger = console } = { } ) {
@@ -126,12 +134,11 @@ export function parseArgsAndResolveBuildId(args, { logger = console } = {}) {
126134 silent = true ;
127135 break ;
128136 case '--version' :
129- if ( ++ i >= args . length ) usage ( 1 ) ;
130- version = args [ i ] ;
137+ case '-v' :
138+ version = ++ i >= args . length ? '' : args [ i ] ;
131139 break ;
132140 case '--id' :
133- if ( ++ i >= args . length ) usage ( 1 ) ;
134- buildId = args [ i ] ;
141+ buildId = ++ i >= args . length ? '' : args [ i ] ;
135142 break ;
136143 default :
137144 warn ?. ( `Unexpected argument: ${ arg } ` ) ;
@@ -205,6 +212,10 @@ export function adjustVersionForBuildId(
205212 return `${ baseVersion } -dev+${ buildId } ` ;
206213}
207214
215+ export function removeDevSuffix ( version ) {
216+ return version . replace ( / - d e v (?: [ 0 - 9 A - Z a - z . - ] * ) ? (?: \+ .* ) ? $ / , '' ) ;
217+ }
218+
208219function defaultReadPackageJson ( ) {
209220 return JSON . parse ( fs . readFileSync ( packagePath , 'utf8' ) ) ;
210221}
0 commit comments