@@ -4,6 +4,7 @@ const sharp = require('sharp');
4
4
const fsExtra = require ( 'fs-extra' ) ;
5
5
const prompt = require ( 'prompt-sync' ) ( ) ;
6
6
const { Worker } = require ( 'worker_threads' ) ;
7
+ const { exec } = require ( 'child_process' ) ;
7
8
8
9
const colorMap = {
9
10
'0000' : { r : 255 , g : 255 , b : 255 } ,
@@ -95,77 +96,135 @@ function GenerateImagesFromRGBData(width, height, outputDir) {
95
96
CreateImageFromArray ( paddedChunk , width , height , outputFilePath ) ;
96
97
}
97
98
98
- function ConvertFileToBinaryStream ( filePath , callback ) {
99
- if ( ! fs . existsSync ( filePath ) ) {
100
- throw new Error ( 'File does not exist.' ) ;
101
- }
99
+ async function ConvertFileToBinaryStream ( filePath ) {
100
+ return new Promise ( ( resolve , reject ) => {
101
+ if ( ! fs . existsSync ( filePath ) ) {
102
+ reject ( new Error ( 'File does not exist.' ) ) ;
103
+ return ;
104
+ }
102
105
103
- const stats = fs . statSync ( filePath ) ;
104
- const totalBytes = stats . size ;
105
- let processedBytes = 0 ;
106
+ const stats = fs . statSync ( filePath ) ;
107
+ const totalBytes = stats . size ;
108
+ let processedBytes = 0 ;
106
109
107
- const progressWorker = new Worker ( path . join ( __dirname , 'progressWorker.js' ) ) ;
110
+ const progressWorker = new Worker ( path . join ( __dirname , 'progressWorker.js' ) ) ;
108
111
109
- progressWorker . on ( 'message' , ( progressBar ) => {
110
- process . stdout . clearLine ( ) ;
111
- process . stdout . cursorTo ( 0 ) ;
112
- process . stdout . write ( `Processing: ${ progressBar } ` ) ;
113
- } ) ;
112
+ progressWorker . on ( 'message' , ( progressBar ) => {
113
+ process . stdout . clearLine ( ) ;
114
+ process . stdout . cursorTo ( 0 ) ;
115
+ process . stdout . write ( `Processing: ${ progressBar } ` ) ;
116
+ } ) ;
114
117
115
- const stream = fs . createReadStream ( filePath ) ;
118
+ const stream = fs . createReadStream ( filePath ) ;
116
119
117
- stream . on ( 'data' , ( chunk ) => {
118
- const chunkSize = chunk . length ;
119
- processedBytes += chunkSize ;
120
+ stream . on ( 'data' , ( chunk ) => {
121
+ const chunkSize = chunk . length ;
122
+ processedBytes += chunkSize ;
120
123
121
- const percentage = ( processedBytes / totalBytes ) * 100 ;
124
+ const percentage = ( processedBytes / totalBytes ) * 100 ;
122
125
123
- if ( Math . round ( percentage ) % 1 === 0 ) {
124
- progressWorker . postMessage ( Math . round ( percentage ) ) ;
125
- }
126
+ if ( Math . round ( percentage ) % 1 === 0 ) {
127
+ progressWorker . postMessage ( Math . round ( percentage ) ) ;
128
+ }
129
+
130
+ const binaryData = Array . from ( chunk ) . map ( byte => byte . toString ( 2 ) . padStart ( 8 , '0' ) ) ;
131
+ binaryData . forEach ( data => {
132
+ const [ part1 , part2 ] = SplitByteTo2Nibbles ( parseInt ( data , 2 ) ) ;
133
+ RGBDataArray . push ( GetColorFromNibble ( part1 ) , GetColorFromNibble ( part2 ) ) ;
126
134
127
- const binaryData = Array . from ( chunk ) . map ( byte => byte . toString ( 2 ) . padStart ( 8 , '0' ) ) ;
128
- binaryData . forEach ( data => {
129
- const [ part1 , part2 ] = SplitByteTo2Nibbles ( parseInt ( data , 2 ) ) ;
130
- RGBDataArray . push ( GetColorFromNibble ( part1 ) , GetColorFromNibble ( part2 ) ) ;
135
+ if ( RGBDataArray . length >= imageWidth * imageHeight ) {
136
+ GenerateImagesFromRGBData ( imageWidth , imageHeight , output ) ;
137
+ RGBDataArray = [ ] ;
138
+ }
139
+ } ) ;
140
+ } ) ;
131
141
132
- if ( RGBDataArray . length >= imageWidth * imageHeight ) {
142
+ stream . on ( 'end' , ( ) => {
143
+ if ( RGBDataArray . length > 0 ) {
144
+ RGBDataArray . push ( { r : 128 , g : 0 , b : 128 } ) ;
133
145
GenerateImagesFromRGBData ( imageWidth , imageHeight , output ) ;
134
- RGBDataArray = [ ] ;
135
146
}
147
+ RGBDataArray = [ ] ;
148
+ console . log ( '\nFile reading completed.' ) ;
149
+ console . timeEnd ( "Processing Time" ) ;
150
+ progressWorker . terminate ( ) ;
151
+ resolve ( ) ; // Resolve when processing is finished
152
+ } ) ;
153
+
154
+ stream . on ( 'error' , ( err ) => {
155
+ console . error ( 'Error reading file:' , err . message ) ;
156
+ reject ( err ) ; // Reject if there's an error
136
157
} ) ;
137
158
} ) ;
159
+ }
138
160
139
- stream . on ( 'end' , ( ) => {
140
- if ( RGBDataArray . length > 0 ) {
141
- RGBDataArray . push ( { r : 128 , g : 0 , b : 128 } ) ;
142
- GenerateImagesFromRGBData ( imageWidth , imageHeight , output ) ;
161
+ function getAllFiles ( dirPath , filesArray = [ ] ) {
162
+ const files = fs . readdirSync ( dirPath ) ;
163
+
164
+ files . forEach ( file => {
165
+ const fullPath = path . join ( dirPath , file ) ;
166
+ if ( fs . statSync ( fullPath ) . isDirectory ( ) ) {
167
+ getAllFiles ( fullPath , filesArray ) ; // Recursive call for subdirectories
168
+ } else {
169
+ filesArray . push ( fullPath ) ; // Add full file path to the array
143
170
}
144
- console . log ( '\nFile reading completed.' ) ;
145
- console . timeEnd ( "Processing Time" ) ;
146
- progressWorker . terminate ( ) ;
147
171
} ) ;
148
172
149
- stream . on ( 'error' , ( err ) => console . error ( 'Error reading file:' , err . message ) ) ;
173
+ return filesArray ;
174
+ }
175
+
176
+ function playSound ( times , delay = 500 ) {
177
+ let count = 0 ;
178
+
179
+ const interval = setInterval ( ( ) => {
180
+ exec ( 'powershell -c [System.Media.SystemSounds]::Beep.Play()' , ( err , stdout , stderr ) => {
181
+ if ( err ) {
182
+ console . error ( 'Error playing sound:' , err ) ;
183
+ }
184
+ } ) ;
185
+
186
+ count ++ ;
187
+ if ( count >= times ) {
188
+ clearInterval ( interval ) ;
189
+ }
190
+ } , delay ) ;
150
191
}
151
192
152
193
// --------------------------------------------------------------
153
194
154
195
const imageWidth = 1920 ;
155
196
const imageHeight = 1080 ;
156
- const inputFile = process . argv [ 2 ] || prompt ( "Define input file: " ) ;
157
- const input = path . join ( __dirname , inputFile ) ;
197
+ const input = process . argv [ 2 ] || prompt ( "Define input file: " ) ;
158
198
const output = path . join ( __dirname , "out" ) ;
159
- const name = path . basename ( input ) ;
160
- fsExtra . emptyDirSync ( output ) ;
161
199
200
+ fsExtra . emptyDirSync ( output ) ;
162
201
console . time ( "Processing Time" ) ;
163
202
164
- PushFilename ( name ) ;
203
+ async function processFiles ( input ) {
204
+ if ( fs . statSync ( input ) . isDirectory ( ) ) {
205
+ const allFiles = getAllFiles ( input ) ;
165
206
166
- ConvertFileToBinaryStream ( input , ( binaryData ) => {
167
- binaryData . forEach ( data => {
168
- const [ part1 , part2 ] = SplitByteTo2Nibbles ( parseInt ( data , 2 ) ) ;
169
- RGBDataArray . push ( GetColorFromNibble ( part1 ) , GetColorFromNibble ( part2 ) ) ;
170
- } ) ;
171
- } ) ;
207
+ for ( const file of allFiles ) {
208
+ try {
209
+ PushFilename ( file ) ;
210
+ await ConvertFileToBinaryStream ( file ) ;
211
+ } catch ( error ) {
212
+ console . error ( 'Error processing file:' , error . message ) ;
213
+ }
214
+ }
215
+ } else {
216
+ try {
217
+ PushFilename ( input ) ;
218
+ await ConvertFileToBinaryStream ( input ) ;
219
+ } catch ( error ) {
220
+ console . error ( 'Error processing file:' , error . message ) ;
221
+ }
222
+ }
223
+ }
224
+
225
+ processFiles ( input ) . then ( ( ) => {
226
+ playSound ( 1 ) ;
227
+ console . log ( 'All files processed.' ) ;
228
+ } ) . catch ( err => {
229
+ console . error ( 'Error during file processing:' , err . message ) ;
230
+ } ) ;
0 commit comments