Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update default publish directory logic #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
netlify-plugin-ttl-cache*.tgz
.idea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 11 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand 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 = {
Expand Down