From 12326c5b74ce7cbb7008aae4f51faf815596dc3a Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Sat, 26 Jun 2021 12:56:49 -0500 Subject: [PATCH] Move Incremental Cache into class --- helpers/incremental/caching.js | 92 +++++++++++++++++++---------- helpers/incremental/caching.test.js | 74 +++++++++++++++++++---- 2 files changed, 124 insertions(+), 42 deletions(-) diff --git a/helpers/incremental/caching.js b/helpers/incremental/caching.js index be73e3e..f9e8e07 100644 --- a/helpers/incremental/caching.js +++ b/helpers/incremental/caching.js @@ -26,37 +26,65 @@ export async function getNetlifyConfig () { } -export async function getPublishDirectoryName () { - const netlifyConfig = await getNetlifyConfig() - - return netlifyConfig.build.publish -} - - -export async function getPublishDirectoryPath () { - const publishDirectoryPath = await getPublishDirectoryName() - - return path.resolve( rootDir, publishDirectoryPath ) -} - - -export async function hasCachedPublishFolder () { - const homePageFile = `${ CACHE_PATH }/index.html` - - // https://github.com/jprichardson/node-fs-extra/blob/master/docs/pathExists.md - return await fs.pathExists( homePageFile ) -} - - -export async function cachePublishFolder () { - - const publishDirectoryPath = await getPublishDirectoryPath() - - // console.log('publishDirectoryPath', publishDirectoryPath) - - // Make sure cache folder exists - await fs.ensureDir( CACHE_PATH ) - - await fs.copy( publishDirectoryPath, CACHE_PATH ) +export class IncrementalCache { + constructor( options = {} ) { + this.cachePath = options.cachePath || CACHE_PATH + this.publishDirectoryName = options.publishDirectoryName || null + this.publishDirectoryPath = options.publishDirectoryPath || null + + } + + // export async function + + async hasCachedPublishFolder () { + const homePageFile = `${ this.cachePath }/index.html` + + // https://github.com/jprichardson/node-fs-extra/blob/master/docs/pathExists.md + return await fs.pathExists( homePageFile ) + } + + async getPublishDirectoryName () { + const netlifyConfig = await getNetlifyConfig() + + return netlifyConfig.build.publish + } + + async getPublishDirectoryPath () { + // const publishDirectoryPath = await this.getPublishDirectoryName() + + return path.resolve( rootDir, this.publishDirectoryName ) + } + + async cachePublishFolder () { + + // const publishDirectoryPath = await this.getPublishDirectoryPath() + + // console.log('publishDirectoryPath', publishDirectoryPath) + + // Make sure cache folder exists + await fs.ensureDir( this.cachePath ) + + await fs.copy( this.publishDirectoryPath, this.cachePath ) + + } + + async emptyPublishDirectory () { + // const publishDirectoryPath = await this.getPublishDirectoryName() + + return await fs.emptyDir( this.publishDirectoryPath, 'utf-8' ) + } + + + async init () { + + if ( this.publishDirectoryName === null ) { + this.publishDirectoryName = await this.getPublishDirectoryName() + } + + if ( this.publishDirectoryPath === null ) { + this.publishDirectoryPath = await this.getPublishDirectoryPath() + } + + } } diff --git a/helpers/incremental/caching.test.js b/helpers/incremental/caching.test.js index f604b0d..4df4eb6 100644 --- a/helpers/incremental/caching.test.js +++ b/helpers/incremental/caching.test.js @@ -1,15 +1,33 @@ +import path from 'path' + import test from 'ava' import { isObject, isString } from '../type-checks.js' import { getNetlifyConfig, - hasCachedPublishFolder, - cachePublishFolder, - CACHE_PATH + CACHE_PATH, + + IncrementalCache } from './caching.js' -test('Can read netlify.toml', async (t) => { +// Keeps tests from messing up production publish folder and files +const testingCachePath = path.join( CACHE_PATH, 'test' ) + + +test.before(async t => { + + t.context.cache = new IncrementalCache({ + cachePath: testingCachePath + }) + + // Set up cache + await t.context.cache.init() + +}) + + +test.serial('Can read netlify.toml', async (t) => { t.plan(2) const netlifyConfig = await getNetlifyConfig() @@ -21,12 +39,17 @@ test('Can read netlify.toml', async (t) => { }) -test('Can cache publish folder', async (t) => { +test.serial('Can cache publish folder', async (t) => { + + const { + cache + } = t.context + // So that we don't overwrite the cached files // we check if a cached file already exists - if ( await hasCachedPublishFolder() ) { - t.log(`Found cached publish folder at ${ CACHE_PATH }`) + if ( await cache.hasCachedPublishFolder() ) { + t.log(`Found cached publish folder at ${ testingCachePath }`) t.pass() return @@ -37,15 +60,46 @@ test('Can cache publish folder', async (t) => { // If there's no files there already // then we can write to the directory with - await cachePublishFolder() + await cache.cachePublishFolder() - if ( (await hasCachedPublishFolder()) === false ) { + if ( (await cache.hasCachedPublishFolder()) === false ) { t.fail() return } - t.log(`Cached publish folder at ${ CACHE_PATH }`) + t.log(`Cached publish folder at ${ testingCachePath }`) + t.pass() +}) + + +test.serial('Can restore publish folder from cache', async (t) => { + + const { + cache + } = t.context + + if ( (await cache.hasCachedPublishFolder( testingCachePath )) === false ) { + t.log(`Could not find publish folder at ${ testingCachePath }`) + t.fail() + return + } + + // await cache.emptyPublishDirectory() + + + // t.log('No prexisting cache folder found') + + + // // If there's no files there already + // // then we can write to the directory with + await cache.cachePublishFolder() + + + + + + // t.log(`Cached publish folder at ${ testingCachePath }`) t.pass() })