@@ -4,8 +4,51 @@ import { fileURLToPath } from 'node:url';
4
4
import { join } from 'node:path' ;
5
5
6
6
const cwd = join ( fileURLToPath ( import . meta. url ) , '../..' ) ;
7
+ const filepath = join ( cwd , 'src/version.ts' ) ;
7
8
8
- const version =
9
- process . argv . length === 3 ? process . argv [ 2 ] : JSON . parse ( fs . readFileSync ( join ( cwd , 'package.json' ) , 'utf8' ) ) . version ;
9
+ const version = getVersionFromArgv ( ) ?? JSON . parse ( fs . readFileSync ( join ( cwd , 'package.json' ) , 'utf8' ) ) . version ;
10
10
11
- fs . writeFileSync ( join ( cwd , 'src/version.ts' ) , `export const VERSION = '${ version } ';\n` ) ;
11
+ try {
12
+ const existingVersion = / V E R S I O N = ' ( [ ^ ' ] + ) ' ; / . exec ( fs . readFileSync ( filepath , 'utf8' ) ) ;
13
+ if ( existingVersion !== null && ! isNewerVersion ( existingVersion [ 1 ] , version ) ) {
14
+ log ( `Skipping inlining. Next version is not newer than the existing one: ${ existingVersion [ 1 ] } .` ) ;
15
+ process . exit ( 0 ) ;
16
+ }
17
+ } catch ( ex ) {
18
+ // no-op
19
+ }
20
+
21
+ fs . writeFileSync ( filepath , `export const VERSION = '${ version } ';\n` ) ;
22
+
23
+ log ( `Inlined ${ version } version.` ) ;
24
+
25
+ function isNewerVersion ( current , next ) {
26
+ const [ curMajor , curMinor , curPatch ] = current . split ( '.' ) . map ( Number ) ;
27
+ const [ nextMajor , nextMinor , nextPatch ] = next . split ( '.' ) . map ( Number ) ;
28
+
29
+ return (
30
+ nextMajor > curMajor ||
31
+ ( curMajor === nextMajor && ( nextMinor > curMinor || ( curMinor <= nextMinor && nextPatch > curPatch ) ) )
32
+ ) ;
33
+ }
34
+
35
+ function log ( message ) {
36
+ if ( process . argv . includes ( '--verbose' ) || process . env . CI ) {
37
+ process . stdout . write ( `${ message } \n` ) ;
38
+ }
39
+ }
40
+
41
+ function getVersionFromArgv ( ) {
42
+ if ( process . argv . length < 3 ) return null ;
43
+
44
+ const r = / ^ ( [ 0 - 9 ] + \. ) { 2 } [ 0 - 9 ] + $ / ;
45
+
46
+ for ( let i = 2 ; i < process . argv . length ; i ++ ) {
47
+ const value = process . argv [ i ] ;
48
+ if ( r . exec ( value ) ) {
49
+ return value ;
50
+ }
51
+ }
52
+
53
+ return null ;
54
+ }
0 commit comments