@@ -10,13 +10,22 @@ const path = require('path');
1010// Parse command line arguments
1111const args = process . argv . slice ( 2 ) ;
1212const urlArgIndex = args . indexOf ( '--url' ) ;
13+ const portableArgIndex = args . indexOf ( '--portable' ) ;
1314let customUrl = null ;
15+ let isPortable = false ;
1416
1517if ( urlArgIndex !== - 1 && args [ urlArgIndex + 1 ] ) {
1618 customUrl = args [ urlArgIndex + 1 ] ;
1719 console . log ( `\x1b[36mCustom URL detected: ${ customUrl } \x1b[0m` ) ;
1820}
1921
22+ if ( portableArgIndex !== - 1 ) {
23+ isPortable = true ;
24+ console . log ( `\x1b[36mBuilding portable version\x1b[0m` ) ;
25+ } else {
26+ console . log ( `\x1b[36mBuilding installed version\x1b[0m` ) ;
27+ }
28+
2029// Update config if URL is provided
2130if ( customUrl ) {
2231 try {
@@ -41,7 +50,42 @@ if (customUrl) {
4150 }
4251}
4352
44- // Run the compilation step
53+ // Inject build configuration into the main process file
54+ try {
55+ const mainPath = path . join ( __dirname , '..' , 'src' , 'main' , 'index.js' ) ;
56+ console . log ( `\x1b[36mInjecting build configuration into: ${ mainPath } \x1b[0m` ) ;
57+
58+ // Read the main file
59+ let mainContent = fs . readFileSync ( mainPath , 'utf8' ) ;
60+
61+ // Remove any existing injected build configuration
62+ mainContent = mainContent . replace (
63+ / \/ \/ I n j e c t e d b u i l d c o n f i g u r a t i o n \n c o n s t B U I L D _ C O N F I G = .* ?; \n \n / g,
64+ ''
65+ ) ;
66+
67+ // Define build configuration
68+ const buildConfig = {
69+ packagingMethod : isPortable ? 'portable' : 'installed'
70+ } ;
71+
72+ // Inject BUILD_CONFIG before the main logic
73+ const buildConfigInjection = `// Injected build configuration
74+ const BUILD_CONFIG = ${ JSON . stringify ( buildConfig ) } ;
75+
76+ ` ;
77+
78+ // Add it at the beginning of the file (after requires)
79+ const requiresEndPattern = / c o n s t i s D e v = ! a p p \. i s P a c k a g e d ; / ;
80+ mainContent = mainContent . replace ( requiresEndPattern , `${ buildConfigInjection } const isDev = !app.isPackaged;` ) ;
81+
82+ // Write the updated main file back
83+ fs . writeFileSync ( mainPath , mainContent , 'utf8' ) ;
84+ console . log ( `\x1b[32mBuild configuration injected: ${ JSON . stringify ( buildConfig ) } \x1b[0m` ) ;
85+ } catch ( error ) {
86+ console . error ( `\x1b[31mError injecting build configuration: ${ error . message } \x1b[0m` ) ;
87+ process . exit ( 1 ) ;
88+ } // Run the compilation step
4589console . log ( '\x1b[36mCompiling the application...\x1b[0m' ) ;
4690try {
4791 execSync ( 'yarn compile' , { stdio : 'inherit' } ) ;
@@ -53,17 +97,30 @@ try {
5397// Run the appropriate electron-builder command based on the platform
5498console . log ( '\x1b[36mBuilding distributable packages...\x1b[0m' ) ;
5599try {
56- // Pass all original arguments (except --url and the URL ) to electron-builder
100+ // Pass all original arguments (except --url, --portable and their values ) to electron-builder
57101 const filteredArgs = args . filter ( ( arg , index ) => {
58- return index !== urlArgIndex && index !== urlArgIndex + 1 ;
102+ return index !== urlArgIndex && index !== urlArgIndex + 1 && index !== portableArgIndex ;
59103 } ) . join ( ' ' ) ;
60104
61- if ( process . platform === 'win32' ) {
62- execSync ( `npx yarn electron-builder --ia32 --x64 ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
63- } else if ( process . platform === 'darwin' ) {
64- execSync ( `electron-builder --mac --x64 ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
105+ if ( isPortable ) {
106+ // Build only portable versions
107+ if ( process . platform === 'win32' ) {
108+ execSync ( `npx yarn electron-builder --win zip --ia32 --x64 -c.win.artifactName="Sploder-Portable-\${arch}.\${ext}" ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
109+ } else if ( process . platform === 'darwin' ) {
110+ execSync ( `electron-builder --mac zip --x64 -c.mac.artifactName="Sploder-Portable-\${arch}.\${ext}" ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
111+ } else {
112+ // For Linux, we'll use the unpacked directory as "portable"
113+ execSync ( `electron-builder --linux dir ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
114+ }
65115 } else {
66- execSync ( `electron-builder --linux appimage snap deb rpm pacman ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
116+ // Build installer versions (default)
117+ if ( process . platform === 'win32' ) {
118+ execSync ( `npx yarn electron-builder --win nsis --ia32 --x64 ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
119+ } else if ( process . platform === 'darwin' ) {
120+ execSync ( `electron-builder --mac --x64 ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
121+ } else {
122+ execSync ( `electron-builder --linux appimage snap deb rpm pacman ${ filteredArgs } ` , { stdio : 'inherit' } ) ;
123+ }
67124 }
68125
69126 console . log ( '\x1b[32mBuild completed successfully!\x1b[0m' ) ;
0 commit comments