1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+
4+ // Configuration: Define what to copy
5+ const COPY_CONFIG = [
6+ { src : 'dist/server.exe' , dest : 'server.exe' , type : 'file' , required : true } ,
7+ { src : 'service_install.bat' , dest : 'service_install.bat' , type : 'file' , required : true } ,
8+ { src : 'service_uninstall.bat' , dest : 'service_uninstall.bat' , type : 'file' , required : true } ,
9+ { src : 'tools' , dest : 'tools' , type : 'dir' , required : true } ,
10+ { src : 'cert' , dest : 'cert' , type : 'dir' , required : true } ,
11+ { src : 'node_modules/engine.io-parser' , dest : 'node_modules/engine.io-parser' , type : 'dir' , required : true } ,
12+ { src : 'node_modules/socket.io' , dest : 'node_modules/socket.io' , type : 'dir' , required : true } ,
13+ { src : 'node_modules/socket.io-parser' , dest : 'node_modules/socket.io-parser' , type : 'dir' , required : true }
14+ ] ;
15+
16+ // Utility function to copy directory recursively
17+ function copyDir ( src , dest ) {
18+ if ( ! fs . existsSync ( dest ) ) {
19+ fs . mkdirSync ( dest , { recursive : true } ) ;
20+ }
21+
22+ const entries = fs . readdirSync ( src , { withFileTypes : true } ) ;
23+
24+ for ( let entry of entries ) {
25+ const srcPath = path . join ( src , entry . name ) ;
26+ const destPath = path . join ( dest , entry . name ) ;
27+
28+ if ( entry . isDirectory ( ) ) {
29+ copyDir ( srcPath , destPath ) ;
30+ } else {
31+ fs . copyFileSync ( srcPath , destPath ) ;
32+ }
33+ }
34+ }
35+
36+ // Utility function to copy file
37+ function copyFile ( src , dest ) {
38+ const destDir = path . dirname ( dest ) ;
39+ if ( ! fs . existsSync ( destDir ) ) {
40+ fs . mkdirSync ( destDir , { recursive : true } ) ;
41+ }
42+ fs . copyFileSync ( src , dest ) ;
43+ }
44+
45+ // Copy items based on configuration
46+ function copyItems ( packageDir , baseDir ) {
47+ console . log ( '📋 Copying files and folders...' ) ;
48+
49+ for ( const item of COPY_CONFIG ) {
50+ const srcPath = path . join ( baseDir , item . src ) ;
51+ const destPath = path . join ( packageDir , item . dest ) ;
52+
53+ if ( ! fs . existsSync ( srcPath ) ) {
54+ if ( item . required ) {
55+ console . error ( `❌ Error: Required ${ item . type } not found: ${ item . src } ` ) ;
56+ if ( item . src === 'dist/server.exe' ) {
57+ console . log ( ' Run your build script to create server.exe' ) ;
58+ } else if ( item . src . startsWith ( 'node_modules/' ) ) {
59+ console . log ( ' Run npm install first' ) ;
60+ }
61+ process . exit ( 1 ) ;
62+ } else {
63+ console . warn ( `⚠️ Warning: Optional ${ item . type } not found: ${ item . src } ` ) ;
64+ continue ;
65+ }
66+ }
67+
68+ if ( item . type === 'file' ) {
69+ console . log ( ` 📄 ${ item . src } → ${ item . dest } ` ) ;
70+ copyFile ( srcPath , destPath ) ;
71+ } else if ( item . type === 'dir' ) {
72+ console . log ( ` 📂 ${ item . src } / → ${ item . dest } /` ) ;
73+ copyDir ( srcPath , destPath ) ;
74+ }
75+ }
76+ }
77+
78+ // Print directory tree
79+ function printTree ( dir , prefix = '' , isLast = true , maxDepth = 2 , currentDepth = 0 ) {
80+ const items = [ ] ;
81+
82+ if ( currentDepth >= maxDepth ) {
83+ return items ;
84+ }
85+
86+ try {
87+ const entries = fs . readdirSync ( dir , { withFileTypes : true } ) ;
88+ entries . sort ( ( a , b ) => {
89+ // Directories first, then files
90+ if ( a . isDirectory ( ) && ! b . isDirectory ( ) ) return - 1 ;
91+ if ( ! a . isDirectory ( ) && b . isDirectory ( ) ) return 1 ;
92+ return a . name . localeCompare ( b . name ) ;
93+ } ) ;
94+
95+ entries . forEach ( ( entry , index ) => {
96+ const isLastEntry = index === entries . length - 1 ;
97+ const connector = isLastEntry ? '└──' : '├──' ;
98+ const newPrefix = prefix + ( isLastEntry ? ' ' : '│ ' ) ;
99+
100+ const itemPath = path . join ( dir , entry . name ) ;
101+
102+ if ( entry . isDirectory ( ) ) {
103+ items . push ( `${ prefix } ${ connector } ${ entry . name } /` ) ;
104+ const subItems = printTree ( itemPath , newPrefix , isLastEntry , maxDepth , currentDepth + 1 ) ;
105+ items . push ( ...subItems ) ;
106+ } else {
107+ items . push ( `${ prefix } ${ connector } ${ entry . name } ` ) ;
108+ }
109+ } ) ;
110+ } catch ( error ) {
111+ items . push ( `${ prefix } [Error reading directory: ${ error . message } ]` ) ;
112+ }
113+
114+ return items ;
115+ }
116+
117+ // Print package contents from filesystem
118+ function printPackageContents ( packageDir , packageName ) {
119+ console . log ( '\n✅ Package created successfully!' ) ;
120+ console . log ( `📦 Location: redist/${ packageName } ` ) ;
121+ console . log ( `\n📋 Package contents:` ) ;
122+
123+ const tree = printTree ( packageDir ) ;
124+ tree . forEach ( line => console . log ( line ) ) ;
125+ }
126+
127+ // Main packaging function
128+ async function createPackage ( ) {
129+ console . log ( '🚀 Starting packaging process...' ) ;
130+
131+ const baseDir = path . join ( __dirname , '..' ) ;
132+
133+ // 1. Read version from webinterface/version.txt
134+ const versionPath = path . join ( baseDir , 'webinterface' , 'version.txt' ) ;
135+ if ( ! fs . existsSync ( versionPath ) ) {
136+ console . error ( '❌ Error: webinterface/version.txt not found!' ) ;
137+ process . exit ( 1 ) ;
138+ }
139+
140+ const version = fs . readFileSync ( versionPath , 'utf8' ) . trim ( ) ;
141+ console . log ( `📋 Version: ${ version } ` ) ;
142+
143+ // 2. Create package directory name
144+ const packageName = `webinterface_${ version } ` ;
145+ const packageDir = path . join ( baseDir , 'redist' , packageName ) ;
146+
147+ // Clean existing package directory
148+ if ( fs . existsSync ( packageDir ) ) {
149+ console . log ( `🧹 Removing existing package directory...` ) ;
150+ fs . rmSync ( packageDir , { recursive : true , force : true } ) ;
151+ }
152+
153+ console . log ( `📁 Creating package directory: ${ packageName } ` ) ;
154+ fs . mkdirSync ( packageDir , { recursive : true } ) ;
155+
156+ // 3. Copy all configured items
157+ copyItems ( packageDir , baseDir ) ;
158+
159+ // 4. Print actual package contents from filesystem
160+ printPackageContents ( packageDir , packageName ) ;
161+ }
162+
163+ // Run the packaging
164+ createPackage ( ) . catch ( error => {
165+ console . error ( '❌ Packaging failed:' , error ) ;
166+ process . exit ( 1 ) ;
167+ } ) ;
0 commit comments