33/**
44 * Inject version information into the build
55 * This script generates a version file that can be imported at runtime
6+ *
7+ * In Docker builds, version.json is generated with git info.
8+ * This script reads that file and generates the appropriate exports.
69 */
710
811import fs from 'fs' ;
@@ -16,25 +19,73 @@ const __dirname = path.dirname(__filename);
1619const packagePath = path . join ( __dirname , '..' , 'package.json' ) ;
1720const packageData = JSON . parse ( fs . readFileSync ( packagePath , 'utf8' ) ) ;
1821
22+ // Try to read version.json (generated during Docker build)
23+ let gitInfo = { commit : 'dev' , ref : 'local' , buildTime : new Date ( ) . toISOString ( ) , commitsAhead : 0 } ;
24+ const versionJsonPath = path . join ( __dirname , '..' , 'version.json' ) ;
25+ if ( fs . existsSync ( versionJsonPath ) ) {
26+ try {
27+ gitInfo = JSON . parse ( fs . readFileSync ( versionJsonPath , 'utf8' ) ) ;
28+ } catch {
29+ // Ignore errors, use defaults
30+ }
31+ }
32+
1933// Create version info object
2034const versionInfo = {
2135 version : packageData . version ,
2236 name : packageData . name ,
23- buildTime : new Date ( ) . toISOString ( ) ,
37+ commit : gitInfo . commit ,
38+ ref : gitInfo . ref ,
39+ buildTime : gitInfo . buildTime ,
40+ commitsAhead : gitInfo . commitsAhead || 0 ,
2441 nodeVersion : process . version
2542} ;
2643
27- // Generate version file content
44+ // Generate version file content with all exports
2845const versionFileContent = `/**
2946 * Auto-generated version information
3047 * DO NOT EDIT - This file is automatically generated during build
3148 */
3249
3350export const VERSION_INFO = ${ JSON . stringify ( versionInfo , null , 2 ) } ;
3451
35- export const VERSION = '${ packageData . version } ';
36- export const PACKAGE_NAME = '${ packageData . name } ';
52+ export const VERSION = '${ versionInfo . version } ';
53+ export const PACKAGE_NAME = '${ versionInfo . name } ';
3754export const BUILD_TIME = '${ versionInfo . buildTime } ';
55+ export const GIT_COMMIT = '${ versionInfo . commit } ';
56+ export const GIT_REF = '${ versionInfo . ref } ';
57+ export const COMMITS_AHEAD = ${ versionInfo . commitsAhead } ;
58+
59+ /**
60+ * Get a formatted version string for display
61+ * Examples:
62+ * - Production (on tag): "1.0.0"
63+ * - Development: "1.0.0-rc.1 (e5f1b19)"
64+ */
65+ export function getDisplayVersion() {
66+ // If ref is a tag or commit is dev/unknown, just show version
67+ if (
68+ GIT_REF.startsWith('v') ||
69+ GIT_COMMIT === 'dev' ||
70+ GIT_COMMIT === 'unknown'
71+ ) {
72+ return VERSION;
73+ }
74+ // Otherwise show version with commit
75+ return \`\${VERSION} (\${GIT_COMMIT.substring(0, 7)})\`;
76+ }
77+
78+ /**
79+ * Check if this is a development build
80+ */
81+ export function isDevBuild() {
82+ return (
83+ BUILD_TIME === 'development' ||
84+ GIT_REF === 'develop' ||
85+ GIT_REF.includes('dev') ||
86+ GIT_REF === 'local'
87+ );
88+ }
3889` ;
3990
4091// Ensure dist/src directory exists
@@ -55,19 +106,29 @@ const versionDtsContent = `/**
55106export interface VersionInfo {
56107 version: string;
57108 name: string;
109+ commit: string;
110+ ref: string;
58111 buildTime: string;
112+ commitsAhead: number;
59113 nodeVersion: string;
60114}
61115
62116export declare const VERSION_INFO: VersionInfo;
63117export declare const VERSION: string;
64118export declare const PACKAGE_NAME: string;
65119export declare const BUILD_TIME: string;
120+ export declare const GIT_COMMIT: string;
121+ export declare const GIT_REF: string;
122+ export declare const COMMITS_AHEAD: number;
123+ export declare function getDisplayVersion(): string;
124+ export declare function isDevBuild(): boolean;
66125` ;
67126
68127const versionDtsPath = path . join ( distSrcPath , 'version.d.ts' ) ;
69128fs . writeFileSync ( versionDtsPath , versionDtsContent ) ;
70129
71130console . log ( `✅ Version ${ packageData . version } injected into build` ) ;
72131console . log ( ` - Version file: ${ versionFilePath } ` ) ;
73- console . log ( ` - Build time: ${ versionInfo . buildTime } ` ) ;
132+ console . log ( ` - Build time: ${ versionInfo . buildTime } ` ) ;
133+ console . log ( ` - Git commit: ${ versionInfo . commit } ` ) ;
134+ console . log ( ` - Git ref: ${ versionInfo . ref } ` ) ;
0 commit comments