1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-19 08:15:32 -04:00

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

View File

@@ -39,14 +39,13 @@
# Cache resoures between builds, see plugins/netlify-plugin-cache/index.js # Cache resoures between builds, see plugins/netlify-plugin-cache/index.js
[[plugins]] [[plugins]]
package = "/plugins/netlify-plugin-cache" package = "./plugins/netlify-plugin-cache"
[plugins.inputs] [plugins.inputs]
# TODO: make this better. paths = ["resources", "_vendor"]
paths = "[\"resources\", \"_vendor\"]" # [[plugins]]
[[plugins]] # package = "netlify-plugin-debug-cache"
package = "netlify-plugin-debug-cache" # [[plugins]]
[[plugins]] # package = "@netlify/plugin-local-install-core"
package = "@netlify/plugin-local-install-core"
# The most important headers and redirects are specified in the _headers and # The most important headers and redirects are specified in the _headers and
# _redirects files generated by Hugo. These are additional custom rules. # _redirects files generated by Hugo. These are additional custom rules.

View File

@@ -14,9 +14,9 @@
"url": "git+https://github.com/jakejarvis/jarv.is.git" "url": "git+https://github.com/jakejarvis/jarv.is.git"
}, },
"scripts": { "scripts": {
"clean": "rimraf public/ builds/", "clean": "rimraf public/ resources/ builds/ && hugo mod clean",
"build": "run-s clean hugo minify", "build": "run-s hugo minify",
"build:preview": "run-s clean hugo:dev", "build:preview": "run-s hugo:dev",
"hugo": "hugo --gc --cleanDestinationDir --verbose", "hugo": "hugo --gc --cleanDestinationDir --verbose",
"hugo:dev": "hugo --environment development --baseURL ${DEPLOY_PRIME_URL:-/} --buildDrafts --buildFuture --gc --cleanDestinationDir --verbose", "hugo:dev": "hugo --environment development --baseURL ${DEPLOY_PRIME_URL:-/} --buildDrafts --buildFuture --gc --cleanDestinationDir --verbose",
"start": "hugo server --disableFastRender --buildDrafts --buildFuture --port 1337 --bind 0.0.0.0 --verbose", "start": "hugo server --disableFastRender --buildDrafts --buildFuture --port 1337 --bind 0.0.0.0 --verbose",

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 = { module.exports = {
async onPreBuild({ utils, inputs }) { // try to restore cache before build begins, if it exists
const paths = JSON.parse(inputs.paths); onPreBuild: async ({ utils: { cache }, inputs }) => {
const success = await utils.cache.restore(paths); if (await cache.restore(inputs.paths)) {
const files = await cache.list(inputs.paths)
if (success) { console.log(`Successfully restored: ${inputs.paths.join(', ')} ... ${files.length} files in total.`)
const cachedFiles = await utils.cache.list(paths);
console.log(`Restored cache of '${paths}', totaling ${cachedFiles.length} files.`);
} else { } 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 }) { // only save/update cache if build was successful
const paths = JSON.parse(inputs.paths); onSuccess: async ({ utils: { cache, status }, inputs }) => {
const success = await utils.cache.save(paths); 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) { // show success & more detail in deploy summary
const cachedFiles = await utils.cache.list(paths); status.show({
console.log(`Successfully cached '${paths}', totaling ${cachedFiles.length} files.`); title: `${files.length} files cached`,
summary: 'These will be restored on the next build! ⚡',
text: `${inputs.paths.join(', ')}`,
})
} else { } else {
console.log(`Couldn't cache '${paths}'.`); console.log(`Failed caching ${inputs.paths.join(', ')}. :(`)
} }
} },
}; }

View File

@@ -1,4 +1,5 @@
name: netlify-plugin-cache name: netlify-plugin-cache
inputs: inputs:
- name: paths - name: paths
description: Array of directories to cache between builds. description: Array of files and/or directories to cache between builds.
required: true