Skip to content

Commit 7ced3a7

Browse files
committed
update usage example
1 parent ad4655d commit 7ced3a7

File tree

3 files changed

+70
-44
lines changed

3 files changed

+70
-44
lines changed

lib/index.js

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {
1919

2020
const 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
196199
module.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+
198225
async function diff(filePath, cacheFile) {
199226
const cacheData = await getCacheData(cacheFile) || {}
200227
// No change file exists

lib/utils/fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function fileExists(filePath) {
8787
return new Promise((resolve, reject) => {
8888
fs.access(filePath, fs.F_OK, (err) => {
8989
if (err) {
90-
console.log(`${consolePrefix} No ${filePath} found. Proceeding`)
90+
// console.log(`${consolePrefix} No ${filePath} found. Proceeding`)
9191
return resolve(false) // eslint-disable-line
9292
}
9393
return resolve(true)

usage-example.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@ const contentsToCache = [
2222
},
2323
{
2424
contents: path.join(__dirname, 'other/node_modules'),
25-
handleCacheUpdate: 'npm install',
26-
shouldCacheUpdate: (data) => {
27-
// console.log('data', data)
25+
shouldCacheUpdate: function() {
2826
return false
2927
},
28+
handleCacheUpdate: 'yarn install'
3029
},
3130
{
3231
contents: path.join(__dirname, 'serverless-test/.serverless'),
33-
shouldCacheUpdate: function() {
34-
return true
35-
},
36-
handleCacheUpdate: 'echo "hi"'
37-
}
32+
handleCacheUpdate: () => {
33+
console.log('run my custom stuff here')
34+
}
35+
// shouldCacheUpdate if omitted will use contents folder hash
36+
},
3837
]
3938

4039
cacheMeOutside(cacheDir, contentsToCache).then((cacheInfo) => {

0 commit comments

Comments
 (0)