1
1
/* eslint-disable unicorn/prefer-string-replace-all */
2
2
import { createHash } from 'node:crypto' ;
3
- import { mkdirSync , readFileSync , writeFileSync } from 'node:fs' ;
3
+ import { mkdirSync , writeFileSync } from 'node:fs' ;
4
4
import path from 'node:path' ;
5
5
import { gzipSync } from 'node:zlib' ;
6
6
@@ -19,18 +19,22 @@ const summary = {
19
19
20
20
const sources : CppCodeSources = [ ] ;
21
21
const filesByExtension : ExtensionGroups = [ ] ;
22
+
23
+ console . log ( 'Collecting source files' ) ;
22
24
const files = getFiles ( ) ;
23
- if ( files . length === 0 ) {
25
+ if ( files . size === 0 ) {
24
26
console . error ( `Directory ${ cmdLine . sourcepath } is empty` ) ;
25
27
process . exit ( 1 ) ;
26
28
}
27
29
28
- const rightPad = files . reduce ( ( p , c ) => ( c . length > p ? c . length : p ) , 0 ) ;
29
- for ( const file of files ) {
30
- const mime = lookup ( file ) || 'text/plain' ;
30
+ console . log ( ) ;
31
+ console . log ( 'Translation to header file' ) ;
32
+ const longestFilename = [ ...files . keys ( ) ] . reduce ( ( p , c ) => ( c . length > p ? c . length : p ) , 0 ) ;
33
+ for ( const [ originalFilename , content ] of files ) {
34
+ const mime = lookup ( originalFilename ) || 'text/plain' ;
31
35
summary . filecount ++ ;
32
36
33
- const filename = file . replace ( / \\ / g, '/' ) ;
37
+ const filename = originalFilename . replace ( / \\ / g, '/' ) ;
34
38
const dataname = filename . replace ( / [ . / - ] / g, '_' ) ;
35
39
let extension = path . extname ( filename ) . toUpperCase ( ) ;
36
40
if ( extension . startsWith ( '.' ) ) extension = extension . slice ( 1 ) ;
@@ -39,7 +43,6 @@ for (const file of files) {
39
43
if ( group ) group . count += 1 ;
40
44
else filesByExtension . push ( { extension, count : 1 } ) ;
41
45
42
- const content = readFileSync ( path . join ( cmdLine . sourcepath , file ) , { flag : 'r' } ) ;
43
46
const md5 = createHash ( 'md5' ) . update ( content ) . digest ( 'hex' ) ;
44
47
summary . size += content . length ;
45
48
const zipContent = gzipSync ( content , { level : 9 } ) ;
@@ -58,7 +61,7 @@ for (const file of files) {
58
61
} ) ;
59
62
console . log (
60
63
greenLog (
61
- `[${ file } ] ${ ' ' . repeat ( rightPad - file . length ) } ✓ gzip used (${ content . length } -> ${ zipContent . length } = ${ zipRatio } %)`
64
+ `[${ originalFilename } ] ${ ' ' . repeat ( longestFilename - originalFilename . length ) } ✓ gzip used (${ content . length } -> ${ zipContent . length } = ${ zipRatio } %)`
62
65
)
63
66
) ;
64
67
} else {
@@ -74,7 +77,7 @@ for (const file of files) {
74
77
} ) ;
75
78
console . log (
76
79
yellowLog (
77
- `[${ file } ] ${ ' ' . repeat ( rightPad - file . length ) } x gzip unused ${ content . length <= 1024 ? `(too small) ` : '' } (${ content . length } -> ${ zipContent . length } = ${ zipRatio } %)`
80
+ `[${ originalFilename } ] ${ ' ' . repeat ( longestFilename - originalFilename . length ) } x gzip unused ${ content . length <= 1024 ? `(too small) ` : '' } (${ content . length } -> ${ zipContent . length } = ${ zipRatio } %)`
78
81
)
79
82
) ;
80
83
}
0 commit comments