@@ -4,9 +4,16 @@ import { $, fs, path, glob, chalk } from 'zx';
4
4
5
5
// Validate argument
6
6
const bumpType = process . argv [ 3 ] ;
7
- if ( ! bumpType || ! [ 'major' , 'minor' , 'patch' ] . includes ( bumpType ) ) {
7
+ const canaryMode = process . argv [ 4 ] ; // Optional: 'commit' or 'timestamp' (default)
8
+
9
+ if ( ! bumpType || ! [ 'major' , 'minor' , 'patch' , 'canary' ] . includes ( bumpType ) ) {
10
+ console . error (
11
+ chalk . red (
12
+ '❌ Usage: zx scripts/version.mjs <major|minor|patch|canary> [commit|timestamp]' ,
13
+ ) ,
14
+ ) ;
8
15
console . error (
9
- chalk . red ( '❌ Usage: zx scripts/version.mjs <major|minor|patch> ') ,
16
+ chalk . gray ( ' For canary: timestamp (default) or commit hash ') ,
10
17
) ;
11
18
process . exit ( 1 ) ;
12
19
}
@@ -16,11 +23,14 @@ console.log(chalk.blue(`🚀 Bumping all package versions: ${bumpType}`));
16
23
/**
17
24
* Bump semantic version
18
25
* @param {string } version - Current version (e.g., "1.2.3")
19
- * @param {string } type - Bump type ("major", "minor", "patch")
20
- * @returns {string } - New version
26
+ * @param {string } type - Bump type ("major", "minor", "patch", "canary")
27
+ * @param {string } mode - For canary: "commit" or "timestamp" (default)
28
+ * @returns {Promise<string> } - New version
21
29
*/
22
- function bumpVersion ( version , type ) {
23
- const [ major , minor , patch ] = version . split ( '.' ) . map ( Number ) ;
30
+ async function bumpVersion ( version , type , mode = 'timestamp' ) {
31
+ // Remove existing prerelease identifiers for base version calculation
32
+ const baseVersion = version . split ( '-' ) [ 0 ] ;
33
+ const [ major , minor , patch ] = baseVersion . split ( '.' ) . map ( Number ) ;
24
34
25
35
switch ( type ) {
26
36
case 'major' :
@@ -29,6 +39,26 @@ function bumpVersion(version, type) {
29
39
return `${ major } .${ minor + 1 } .0` ;
30
40
case 'patch' :
31
41
return `${ major } .${ minor } .${ patch + 1 } ` ;
42
+ case 'canary' : {
43
+ // For canary, we bump patch and add canary suffix
44
+ let identifier ;
45
+ if ( mode === 'commit' ) {
46
+ try {
47
+ // Get short commit hash
48
+ const commitHash = await $ `git rev-parse --short HEAD` ;
49
+ identifier = commitHash . stdout . trim ( ) ;
50
+ } catch ( error ) {
51
+ console . warn (
52
+ chalk . yellow ( '⚠️ Failed to get commit hash, using timestamp' ) ,
53
+ ) ;
54
+ identifier = Math . floor ( Date . now ( ) / 1000 ) ;
55
+ }
56
+ } else {
57
+ // Use timestamp (default)
58
+ identifier = Math . floor ( Date . now ( ) / 1000 ) ;
59
+ }
60
+ return `${ major } .${ minor } .${ patch + 1 } -canary.${ identifier } ` ;
61
+ }
32
62
default :
33
63
throw new Error ( `Invalid bump type: ${ type } ` ) ;
34
64
}
@@ -132,7 +162,11 @@ async function main() {
132
162
133
163
// Find the highest current version
134
164
const highestVersion = currentVersions
135
- . map ( v => v . split ( '.' ) . map ( Number ) )
165
+ . map ( v => {
166
+ // Extract base version (remove prerelease identifiers)
167
+ const baseVersion = v . split ( '-' ) [ 0 ] ;
168
+ return baseVersion . split ( '.' ) . map ( Number ) ;
169
+ } )
136
170
. reduce ( ( max , current ) => {
137
171
for ( let i = 0 ; i < 3 ; i ++ ) {
138
172
if ( current [ i ] > max [ i ] ) return current ;
@@ -152,7 +186,7 @@ async function main() {
152
186
) ;
153
187
154
188
// Calculate the new version from the highest current version
155
- const newVersion = bumpVersion ( highestVersion , bumpType ) ;
189
+ const newVersion = await bumpVersion ( highestVersion , bumpType , canaryMode ) ;
156
190
console . log ( chalk . green ( `🎯 Target version: ${ newVersion } ` ) ) ;
157
191
158
192
// First pass: bump all versions to the new unified version
@@ -198,7 +232,22 @@ async function main() {
198
232
console . log (
199
233
chalk . gray ( ' 3. Run: pnpm run test (to verify everything works)' ) ,
200
234
) ;
201
- console . log ( chalk . gray ( ' 4. Commit changes and create release' ) ) ;
235
+
236
+ if ( bumpType === 'canary' ) {
237
+ console . log (
238
+ chalk . gray ( ' 4. Publish canary: pnpm run release --tag canary' ) ,
239
+ ) ;
240
+ console . log (
241
+ chalk . yellow (
242
+ '\n⚠️ Canary versions should be published with the "canary" tag' ,
243
+ ) ,
244
+ ) ;
245
+ console . log (
246
+ chalk . yellow ( ' Or use: pnpm run release:canary (automated)' ) ,
247
+ ) ;
248
+ } else {
249
+ console . log ( chalk . gray ( ' 4. Commit changes and create release' ) ) ;
250
+ }
202
251
} catch ( error ) {
203
252
console . error ( chalk . red ( '❌ Error during version bump:' ) , error . message ) ;
204
253
process . exit ( 1 ) ;
0 commit comments