Move Incremental Cache into class

This commit is contained in:
Sam Carlton 2021-06-26 12:56:49 -05:00
parent aa960903de
commit 12326c5b74
2 changed files with 124 additions and 42 deletions

View file

@ -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()
}
}
}

View file

@ -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()
})