1- import { minify } from ' minify' ;
2- import tryToCatch from ' try-to-catch' ;
3- import fse from ' fs-extra' ;
4- import path from ' path' ;
1+ import { minify } from " minify" ;
2+ import tryToCatch from " try-to-catch" ;
3+ import fse from " fs-extra" ;
4+ import path from " path" ;
55
66function getFilesWithExtension ( root , directory , extension , ignoreUnderscored ) {
77 const target = root + directory ;
8- if ( ! fse . existsSync ( target ) ) { // Check if the directory exists
8+ if ( ! fse . existsSync ( target ) ) {
9+ // Check if the directory exists
910 console . log ( `(error) \x1b[91mdirectory "${ target } " does not exist!\x1b[0m` ) ;
1011 throw new Error ( "FAILED" ) ;
1112 }
@@ -16,12 +17,11 @@ function getFilesWithExtension(root, directory, extension, ignoreUnderscored) {
1617 // Filter files by the extension
1718 const filtered = files . filter ( ( file ) => {
1819 const ext = path . extname ( file ) . toLowerCase ( ) ;
19- if ( ignoreUnderscored && file . startsWith ( '_' ) ) return false ;
20+ if ( ignoreUnderscored && file . startsWith ( "_" ) ) return false ;
2021 return ext === extension . toLowerCase ( ) ;
2122 } ) ;
2223
23-
24- const result = filtered . map ( file => directory + file ) ;
24+ const result = filtered . map ( ( file ) => directory + file ) ;
2525 return result ;
2626}
2727
@@ -30,16 +30,20 @@ async function calculateFileSize(fileList) {
3030 let totalSize = 0 ;
3131
3232 for ( const filePath of fileList ) {
33- const stats = await fse . stat ( filePath ) ;
34-
35- if ( stats . isFile ( ) ) {
36- totalSize += stats . size ;
33+ // Check if file exists before trying to stat it
34+ if ( fse . existsSync ( filePath ) ) {
35+ const stats = await fse . stat ( filePath ) ;
36+ if ( stats . isFile ( ) ) {
37+ totalSize += stats . size ;
38+ }
3739 }
3840 }
3941
4042 return totalSize ;
4143 } catch ( error ) {
42- console . log ( "(error) \x1b[91mfailed while calculating size:\x1b[0m\n" + error . message ) ;
44+ console . log (
45+ "(error) \x1b[91mfailed while calculating size:\x1b[0m\n" + error . message
46+ ) ;
4347 throw new Error ( "FAILED" ) ;
4448 }
4549}
@@ -53,7 +57,9 @@ async function grabAndMinify(file) {
5357
5458 const [ error , data ] = await tryToCatch ( minify , file , options ) ;
5559 if ( error ) {
56- console . log ( "(error) \x1b[91merror occured while minifying:\x1b[0m\n" + error . message ) ;
60+ console . log (
61+ "(error) \x1b[91merror occured while minifying:\x1b[0m\n" + error . message
62+ ) ;
5763 throw new Error ( "FAILED" ) ;
5864 }
5965 return data ;
@@ -67,21 +73,26 @@ async function main() {
6773 src = "../" ;
6874 dest = "../docs/" ;
6975 }
70-
76+
7177 const htmlFiles = getFilesWithExtension ( src , "" , ".html" , true ) ;
7278 const cssFiles = getFilesWithExtension ( src , "css/" , ".css" ) ;
7379 const jsFiles = getFilesWithExtension ( src , "js/" , ".js" ) ;
74-
80+
7581 const files = [ ...htmlFiles , ...cssFiles , ...jsFiles ] ;
76- const srcFiles = files . map ( file => src + file ) ;
77- const destFiles = files . map ( file => dest + file ) ;
78-
79- const prevTotal = await calculateFileSize ( destFiles ) / 1000 ;
82+ const srcFiles = files . map ( ( file ) => src + file ) ;
83+ const destFiles = files . map ( ( file ) => dest + file ) ;
84+
85+ const prevTotal = ( await calculateFileSize ( destFiles ) ) / 1000 ;
8086
8187 console . log ( `(setup) clearing ${ dest } folder and contents` ) ;
8288 fse . rmSync ( dest , { recursive : true , force : true } ) ;
8389 fse . mkdirSync ( dest ) ;
8490
91+ // Will always be 0.
92+ if ( prevTotal === 0 ) {
93+ console . log ( `(info) starting fresh build - no previous files found` ) ;
94+ }
95+
8596 const dirs = [ "js" , "css" ] ;
8697 for ( let i = 0 ; i < dirs . length ; i ++ ) {
8798 console . log ( `(setup) creating empty ${ dest + dirs [ i ] } folder` ) ;
@@ -103,18 +114,33 @@ async function main() {
103114 console . log ( `\n(recursive copy) ${ src + "assets" } --> ${ dest + "assets" } ` ) ;
104115 fse . copySync ( src + "assets" , dest + "assets" , { overwrite : true } ) ;
105116
106- const srcTotal = await calculateFileSize ( srcFiles ) / 1000 ;
107- const destTotal = await calculateFileSize ( destFiles ) / 1000 ;
117+ const srcTotal = ( await calculateFileSize ( srcFiles ) ) / 1000 ;
118+ const destTotal = ( await calculateFileSize ( destFiles ) ) / 1000 ;
108119
109120 console . log ( `\n(result) raw size: \x1b[93m${ srcTotal } \x1b[0m kb` ) ;
110121 console . log ( `(result) minified size: \x1b[93m${ destTotal } \x1b[0m kb` ) ;
111- console . log ( `(result) compressed by \x1b[92m${ Math . round ( ( 1 - ( destTotal / srcTotal ) ) * 100 ) } %\x1b[0m\n` ) ;
112-
122+ console . log (
123+ `(result) compressed by \x1b[92m${ Math . round (
124+ ( 1 - destTotal / srcTotal ) * 100
125+ ) } %\x1b[0m\n`
126+ ) ;
127+
113128 const sizeChange = Math . round ( ( destTotal - prevTotal ) * 100 ) / 100 ;
114129 console . log ( `\n(result) previous size: \x1b[93m${ prevTotal } \x1b[0m kb` ) ;
115- const resultColor = sizeChange > 0 ? "\x1b[91m+" : sizeChange < 0 ? "\x1b[92m" : "~" ;
116- console . log ( `(result) build size change: ${ resultColor } ${ sizeChange } \x1b[0m kb` ) ;
130+
131+ // Will always be 0.
132+ if ( prevTotal === 0 ) {
133+ console . log (
134+ `(result) fresh build completed - final size: \x1b[93m${ destTotal } \x1b[0m kb`
135+ ) ;
136+ } else {
137+ const resultColor =
138+ sizeChange > 0 ? "\x1b[91m+" : sizeChange < 0 ? "\x1b[92m" : "~" ;
139+ console . log (
140+ `(result) build size change: ${ resultColor } ${ sizeChange } \x1b[0m kb`
141+ ) ;
142+ }
117143}
118144
119145console . log ( "\n>>> Starting minification of GeoSMART site...\n" ) ;
120- main ( ) ;
146+ main ( ) ;
0 commit comments