@@ -19,6 +19,20 @@ const {
1919const packageJsonPath = path . resolve ( __dirname , '../package.json' ) ;
2020const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf-8' ) ) ;
2121
22+ function parseBuildEnv ( args ) {
23+ const index = args . indexOf ( '--build-env' ) ;
24+ const buildEnv = index >= 0 ? args [ index + 1 ] : undefined ;
25+ if ( ! [ 'development' , 'production' , 'preview' ] . includes ( buildEnv ) ) {
26+ throw new Error ( 'Expected --build-env development|production|preview' ) ;
27+ }
28+ return buildEnv ;
29+ }
30+
31+ function readArg ( args , name ) {
32+ const index = args . indexOf ( name ) ;
33+ return index >= 0 ? args [ index + 1 ] : undefined ;
34+ }
35+
2236function getGitInfo ( ) {
2337 try {
2438 const gitCommitFull = execSync ( 'git rev-parse HEAD' , { encoding : 'utf-8' } ) . trim ( ) ;
@@ -40,11 +54,10 @@ function getGitInfo() {
4054 }
4155}
4256
43- function generateVersionInfo ( ) {
57+ function generateVersionInfo ( buildEnv ) {
4458 const gitInfo = getGitInfo ( ) ;
4559 const buildDate = new Date ( ) . toISOString ( ) ;
4660 const buildTimestamp = Date . now ( ) ;
47- const buildEnv = process . env . NODE_ENV || 'development' ;
4861 const isDev = buildEnv === 'development' ;
4962
5063 const versionInfo = {
@@ -60,8 +73,8 @@ function generateVersionInfo() {
6073 return versionInfo ;
6174}
6275
63- function saveVersionInfoToJson ( versionInfo ) {
64- const outputPath = path . resolve ( __dirname , '../ src/web-ui/public/version.json' ) ;
76+ function saveVersionInfoToJson ( versionInfo , outputRoot ) {
77+ const outputPath = path . resolve ( outputRoot , 'src/web-ui/public/version.json' ) ;
6578
6679 const dir = path . dirname ( outputPath ) ;
6780 if ( ! fs . existsSync ( dir ) ) {
@@ -75,8 +88,8 @@ function saveVersionInfoToJson(versionInfo) {
7588 ) ;
7689}
7790
78- function saveVersionInfoToTS ( versionInfo ) {
79- const outputPath = path . resolve ( __dirname , '../ src/web-ui/src/generated/version.ts' ) ;
91+ function saveVersionInfoToTS ( versionInfo , outputRoot ) {
92+ const outputPath = path . resolve ( outputRoot , 'src/web-ui/src/generated/version.ts' ) ;
8093
8194 const dir = path . dirname ( outputPath ) ;
8295 if ( ! fs . existsSync ( dir ) ) {
@@ -104,13 +117,16 @@ function generateHtmlInjectionScript(versionInfo) {
104117}
105118
106119function main ( ) {
107- const versionInfo = generateVersionInfo ( ) ;
120+ const args = process . argv . slice ( 2 ) ;
121+ const buildEnv = parseBuildEnv ( args ) ;
122+ const outputRoot = path . resolve ( readArg ( args , '--output-root' ) || path . resolve ( __dirname , '..' ) ) ;
123+ const versionInfo = generateVersionInfo ( buildEnv ) ;
108124
109- saveVersionInfoToJson ( versionInfo ) ;
110- saveVersionInfoToTS ( versionInfo ) ;
125+ saveVersionInfoToJson ( versionInfo , outputRoot ) ;
126+ saveVersionInfoToTS ( versionInfo , outputRoot ) ;
111127
112128 const htmlScript = generateHtmlInjectionScript ( versionInfo ) ;
113- const htmlScriptPath = path . resolve ( __dirname , '../ src/web-ui/src/generated/version-injection.html' ) ;
129+ const htmlScriptPath = path . resolve ( outputRoot , 'src/web-ui/src/generated/version-injection.html' ) ;
114130
115131 const htmlDir = path . dirname ( htmlScriptPath ) ;
116132 if ( ! fs . existsSync ( htmlDir ) ) {
@@ -123,12 +139,11 @@ function main() {
123139 printSuccess ( `${ versionInfo . name } v${ versionInfo . version } ${ gitStr } ` ) ;
124140}
125141
126- // On failure: warn and exit 0 so build is not interrupted
127142try {
128143 main ( ) ;
129144} catch ( err ) {
130- printWarning ( 'Version info generation failed, skipped : ' + ( err . message || err ) ) ;
131- process . exit ( 0 ) ;
145+ printWarning ( 'Version info generation failed: ' + ( err . message || err ) ) ;
146+ process . exit ( 1 ) ;
132147}
133148
134149
0 commit comments