cleaning up the netlify plugin

didn't actually try just passing an array of dirs to Netlify plugin, and now only saving cache if build successful

[skip ci]
This commit is contained in:
2020-06-05 19:47:27 -04:00
parent eafbf87f49
commit fd06f71df4
4 changed files with 32 additions and 29 deletions
+21 -18
View File
@@ -1,27 +1,30 @@
// Based on https://github.com/cdeleeuwe/netlify-plugin-hugo-cache-resources/blob/master/index.js
// https://github.com/netlify/build/blob/master/packages/cache-utils/README.md
module.exports = {
async onPreBuild({ utils, inputs }) {
const paths = JSON.parse(inputs.paths);
const success = await utils.cache.restore(paths);
if (success) {
const cachedFiles = await utils.cache.list(paths);
console.log(`Restored cache of '${paths}', totaling ${cachedFiles.length} files.`);
// try to restore cache before build begins, if it exists
onPreBuild: async ({ utils: { cache }, inputs }) => {
if (await cache.restore(inputs.paths)) {
const files = await cache.list(inputs.paths)
console.log(`Successfully restored: ${inputs.paths.join(', ')} ... ${files.length} files in total.`)
} else {
console.log(`A cache of '${paths}' doesn't exist (yet).`);
console.log(`A cache of ${inputs.paths.join(', ')} doesn't exist (yet).`)
}
},
async onPostBuild({ utils, inputs }) {
const paths = JSON.parse(inputs.paths);
const success = await utils.cache.save(paths);
// only save/update cache if build was successful
onSuccess: async ({ utils: { cache, status }, inputs }) => {
if (await cache.save(inputs.paths)) {
const files = await cache.list(inputs.paths)
console.log(`Successfully cached: ${inputs.paths.join(', ')} ... ${files.length} files in total.`)
if (success) {
const cachedFiles = await utils.cache.list(paths);
console.log(`Successfully cached '${paths}', totaling ${cachedFiles.length} files.`);
// show success & more detail in deploy summary
status.show({
title: `${files.length} files cached`,
summary: 'These will be restored on the next build! ⚡',
text: `${inputs.paths.join(', ')}`,
})
} else {
console.log(`Couldn't cache '${paths}'.`);
console.log(`Failed caching ${inputs.paths.join(', ')}. :(`)
}
}
};
},
}
+2 -1
View File
@@ -1,4 +1,5 @@
name: netlify-plugin-cache
inputs:
- name: paths
description: Array of directories to cache between builds.
description: Array of files and/or directories to cache between builds.
required: true