diff --git a/.gitignore b/.gitignore index cf58b84..c46d1ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules netlify-plugin-ttl-cache*.tgz +.idea \ No newline at end of file diff --git a/README.md b/README.md index e8e81fd..a9fd6be 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ _Build output directory._ **type:** `string` -**default:** `"build"` +**default:** The "Publish directory" provided in the Build settings of your Netlify Deployment ### ttl diff --git a/manifest.yml b/manifest.yml index 60e6529..e79a960 100644 --- a/manifest.yml +++ b/manifest.yml @@ -1,8 +1,7 @@ name: netlify-plugin-ttl-cache inputs: - name: path - description: Path of build output - default: "build" + description: Path of build output, defaults to using the publish directory defined in the Build settings of Netlify - name: ttl description: Number of days to keep old assets default: 90 diff --git a/package-lock.json b/package-lock.json index b3a9adb..dbc947b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "netlify-plugin-ttl-cache", - "version": "1.0.0", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.0.0", + "version": "2.0.0", "license": "MIT", "devDependencies": { "jest": "^27.1.0" diff --git a/package.json b/package.json index c0915c3..3e0f1b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "netlify-plugin-ttl-cache", - "version": "1.0.2", + "version": "2.0.0", "description": "A Netlify plugin for persisting immutable build assets across releases.", "keywords": [ "netlify", diff --git a/src/index.js b/src/index.js index 1b8623d..f64e65d 100644 --- a/src/index.js +++ b/src/index.js @@ -4,16 +4,18 @@ const { getDaysApart, getDirFilenames, addTrailingSlash } = require("./utils"); const TMP_CACHE_DIR = ".netlify-plugin-ttl-cache"; /** Get old cache and prepare files. */ -const onPreBuild = async ({ utils, inputs }) => { +const onPreBuild = async ({ constants, utils, inputs }) => { + const path = inputs.path || constants.PUBLISH_DIR; + // Restore build cache - const hasCache = await utils.cache.restore(inputs.path); + const hasCache = await utils.cache.restore(path); if (!hasCache) { return; } // Remove files that have passed ttl threshold or match exclude regex - const files = await getDirFilenames(inputs.path); + const files = await getDirFilenames(path); const today = new Date(); const exclude = new RegExp(inputs.exclude); await Promise.all( @@ -26,23 +28,25 @@ const onPreBuild = async ({ utils, inputs }) => { ); // Move to temporary directory to be restored post-build - await utils.run("cp", ["-r", inputs.path, TMP_CACHE_DIR]); + await utils.run("cp", ["-r", path, TMP_CACHE_DIR]); }; /** Restore cached files along with latest build assets (without replacement). */ -const onPostBuild = async ({ utils, inputs }) => { +const onPostBuild = async ({ constants, utils, inputs }) => { + const path = inputs.path || constants.PUBLISH_DIR + if (await stat(TMP_CACHE_DIR).catch(() => false)) { await utils.run("rsync", [ "-r", "--ignore-existing", addTrailingSlash(TMP_CACHE_DIR), - addTrailingSlash(inputs.path), + addTrailingSlash(path), ]); await rmdir(TMP_CACHE_DIR, { recursive: true }); } // Save new cache - await utils.cache.save(inputs.path); + await utils.cache.save(path); }; module.exports = {