@@ -19,7 +19,7 @@ const {
1919
2020const homeDir = os . homedir ( )
2121
22- async function cacheMagic ( cacheFolder , contents ) {
22+ async function cacheMeOutside ( cacheFolder , contents ) {
2323 const options = { }
2424 const currentWorkingDir = process . cwd ( )
2525 const defaultOptions = {
@@ -113,43 +113,45 @@ async function checkCache(d, opts, cacheDirectory) {
113113
114114 let changeTriggered = false
115115 let shouldCacheUpdate = false
116+ let shouldUpdateFunction = defaultShouldUpdate
117+
116118 if ( d . shouldCacheUpdate && typeof d . shouldCacheUpdate === 'function' ) {
117- shouldCacheUpdate = await d . shouldCacheUpdate ( cacheData , {
118- diff : diffWrapper
119- } )
120- console . log ( 'shouldCacheUpdate' , shouldCacheUpdate )
121- if ( ! cacheData || shouldCacheUpdate ) {
122- console . log ( 'TRIGGER UPDATE LOGIC' )
123-
124- changeTriggered = true
125-
126- // Clear out cache directory
127- await removeDirectory ( cachedContents )
128-
129- // TODO use updateCacheFunction
130- if ( d . handleCacheUpdate ) {
131- console . log ( 'handleCacheUpdate' , d . handleCacheUpdate )
132-
133- // Handle CLI commands
134- if ( typeof d . handleCacheUpdate === 'string' ) {
135- console . log ( 'Do command' )
136- console . log ( `cwd` , parentDirectory )
137- await execPromise ( d . handleCacheUpdate , {
138- cwd : parentDirectory
139- } )
140- }
141-
142- // Handle custom functions
143- if ( typeof d . handleCacheUpdate === 'function' ) {
144- console . log ( 'Do function' )
145- await d . handleCacheUpdate ( cacheData )
146- }
119+ shouldUpdateFunction = d . shouldCacheUpdate
120+ }
121+
122+ // Run custom or default shouldCacheUpdate
123+ shouldCacheUpdate = await shouldUpdateFunction ( cacheData , {
124+ diff : diffWrapper
125+ } )
126+
127+ if ( ! cacheData || shouldCacheUpdate ) {
128+ console . log ( 'TRIGGER UPDATE LOGIC' )
129+
130+ changeTriggered = true
131+
132+ // Clear out cache directory
133+ await removeDirectory ( cachedContents )
134+
135+ // Run handleCacheUpdate cmd/function
136+ if ( d . handleCacheUpdate ) {
137+ // Handle CLI commands
138+ if ( typeof d . handleCacheUpdate === 'string' ) {
139+ console . log ( `cwd` , parentDirectory )
140+ await execPromise ( d . handleCacheUpdate , {
141+ cwd : parentDirectory
142+ } )
147143 }
148144
149- console . log ( `3. Copying over ${ directory } to ${ cachedContents } ...` )
150- await copyDirectory ( directory , cachedContents )
151- console . log ( `✓ Dependencies copied to cache ${ cachedContents } \n` )
145+ // Handle custom functions
146+ if ( typeof d . handleCacheUpdate === 'function' ) {
147+ console . log ( 'Running handleCacheUpdate function...' )
148+ await d . handleCacheUpdate ( cacheData )
149+ }
152150 }
151+
152+ console . log ( `3. Copying over ${ directory } to ${ cachedContents } ...` )
153+ await copyDirectory ( directory , cachedContents )
154+ console . log ( `✓ Dependencies copied to cache ${ cachedContents } \n` )
153155 }
154156
155157 console . log ( `4. Saving new dependencies hash...` )
@@ -175,6 +177,7 @@ async function checkCache(d, opts, cacheDirectory) {
175177 const defaultInfo = {
176178 ...info ,
177179 cacheDir : path . dirname ( cachedContents ) ,
180+ cacheDirContents : cachedContents ,
178181 contents : {
179182 src : directory ,
180183 hash : folderHash . hash ,
@@ -192,9 +195,33 @@ async function checkCache(d, opts, cacheDirectory) {
192195}
193196
194197// "build": "npm-install-cache && npm run build"
195- module . exports = cacheMagic
198+ module . exports = cacheMeOutside
196199module . exports . getHash = getHash
197200
201+ /* Default should update checks for folder hash change */
202+ async function defaultShouldUpdate ( cacheData ) {
203+ console . log ( 'DEFUALT defaultShouldUpdate' )
204+ if ( ! cacheData || ! cacheData . contents || ! cacheData . contents . hash ) {
205+ // No cache, is first run
206+ return true
207+ }
208+
209+ const cacheHash = cacheData . contents . hash
210+
211+ const folderHash = await hashFolder ( cacheData . contents . src )
212+
213+ if ( folderHash . hash !== cacheHash ) {
214+ console . log ( 'FOLDER HAS CHANGED PURGE CACHE' )
215+ return true
216+ }
217+
218+ if ( folderHash . hash === cacheData . contents . hash ) {
219+ console . log ( 'NO changes to FOLDER' )
220+ }
221+
222+ return false
223+ }
224+
198225async function diff ( filePath , cacheFile ) {
199226 const cacheData = await getCacheData ( cacheFile ) || { }
200227 // No change file exists
0 commit comments