@@ -41,7 +41,7 @@ const brush = (colorName, str) => {
4141 return `\x1b[38;5;${ color } m${ str } \x1b[0m` ;
4242}
4343
44- const reportAction = ( ctx , pathJson , filename ) => {
44+ const reportAction = ( ctx , pathJson , filename , action ) => {
4545 const currTime = new Date ( ) . toLocaleString ( ctx . locales , {
4646 timeStyle : 'medium' ,
4747 } ) ;
@@ -51,9 +51,14 @@ const reportAction = (ctx, pathJson, filename) => {
5151 . replace ( APP_PATH , '.' )
5252 . replace ( filename , brush ( 'cyan' , filename ) ) ;
5353
54+ if ( action === 'Cached' ) {
55+ action = brush ( 'grey' , action ) ;
56+ }
57+
5458 const message = Util . format (
55- '[%s] Loaded %s%s' ,
59+ '[%s] %s %s%s' ,
5660 coloredCurrentTime ,
61+ action ,
5762 coloredRelativePath ,
5863 BREAK_LINE
5964 ) ;
@@ -77,33 +82,40 @@ const loadImportsRecursively = async (options) => {
7782 const { report, pathData } = ctx ;
7883
7984 if ( idx < imports . length ) {
85+ let action = 'Cached' ;
86+
8087 const jsonRelativePath = imports [ idx ] + '.json' ;
8188 const jsonFullPath = Path . join ( pathData , 'imports' , jsonRelativePath ) ;
8289 const jsonFilename = Path . basename ( jsonFullPath , '.json' ) ;
90+ const cacheKey = 'imports:' + jsonFullPath . replace ( APP_PATH , '' ) ;
8391
8492 if ( ! jsonFullPath . startsWith ( APP_PATH ) ) {
8593 reject ( new Error ( ERR_TOP_DIR ) ) ;
8694 return ;
8795 }
8896
89- try {
97+ if ( ! CachedData . hasOwnProperty ( cacheKey ) ) {
98+ try {
9099
91- const jsonData = await Fs . promises . readFile ( jsonFullPath , 'utf8' ) ;
92- const parsedData = JSON . parse ( jsonData ) ;
100+ const jsonData = await Fs . promises . readFile ( jsonFullPath , 'utf8' ) ;
93101
94- Object . defineProperty ( storage , jsonFilename , {
95- value : parsedData ,
96- enumerable : true
97- } ) ;
102+ CachedData [ cacheKey ] = JSON . parse ( jsonData ) ;
103+ action = 'Loaded' ;
98104
99- } catch ( error ) {
105+ } catch ( error ) {
100106
101- reject ( error ) ;
102- return ;
107+ reject ( error ) ;
108+ return ;
103109
110+ }
104111 }
105112
106- report && reportAction ( ctx , jsonFullPath , jsonFilename ) ;
113+ report && reportAction ( ctx , jsonFullPath , jsonFilename , action ) ;
114+
115+ Object . defineProperty ( storage , jsonFilename , {
116+ value : CachedData [ cacheKey ] ,
117+ enumerable : true
118+ } ) ;
107119
108120 options . idx = idx + 1 ;
109121 loadImportsRecursively ( options ) ;
@@ -120,12 +132,14 @@ async function loadJsonData(file) {
120132 . replace ( pathHtml , `${ pathData } /pages` )
121133 . slice ( 0 , - 3 ) + 'json' ;
122134
135+ const cacheKey = 'data:' + pathJson . replace ( APP_PATH , '' ) ;
136+
123137 const filename = Path . basename ( file . path , '.pug' ) ;
124138 const pocket = { filename } ;
125139
126140 if ( CachedData . hasOwnProperty ( pathJson ) ) {
127- report && reportAction ( this , pathJson , filename ) ;
128- return CachedData [ pathJson ] ;
141+ report && reportAction ( this , pathJson , filename , 'Cached' ) ;
142+ return CachedData [ cacheKey ] ;
129143 }
130144
131145 let jsonData = '' ;
@@ -137,7 +151,7 @@ async function loadJsonData(file) {
137151 return pocket ;
138152 }
139153
140- report && reportAction ( this , pathJson , filename ) ;
154+ report && reportAction ( this , pathJson , filename , 'Loaded' ) ;
141155
142156 if ( jsonData . hasOwnProperty ( 'imports' ) ) {
143157 const { data = { } , imports = [ ] } = jsonData ;
@@ -166,7 +180,7 @@ async function loadJsonData(file) {
166180 } ) ;
167181 }
168182
169- CachedData [ pathJson ] = pocket ;
183+ CachedData [ cacheKey ] = pocket ;
170184
171185 return pocket ;
172186}
0 commit comments