mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-18 06:44:46 -07:00
Move Incremental Cache into class
This commit is contained in:
parent
aa960903de
commit
12326c5b74
2 changed files with 124 additions and 42 deletions
|
|
@ -26,37 +26,65 @@ export async function getNetlifyConfig () {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function getPublishDirectoryName () {
|
export class IncrementalCache {
|
||||||
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 )
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,33 @@
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
import test from 'ava'
|
import test from 'ava'
|
||||||
|
|
||||||
import { isObject, isString } from '../type-checks.js'
|
import { isObject, isString } from '../type-checks.js'
|
||||||
import {
|
import {
|
||||||
getNetlifyConfig,
|
getNetlifyConfig,
|
||||||
hasCachedPublishFolder,
|
CACHE_PATH,
|
||||||
cachePublishFolder,
|
|
||||||
CACHE_PATH
|
IncrementalCache
|
||||||
} from './caching.js'
|
} 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)
|
t.plan(2)
|
||||||
|
|
||||||
const netlifyConfig = await getNetlifyConfig()
|
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
|
// So that we don't overwrite the cached files
|
||||||
// we check if a cached file already exists
|
// we check if a cached file already exists
|
||||||
|
|
||||||
if ( await hasCachedPublishFolder() ) {
|
if ( await cache.hasCachedPublishFolder() ) {
|
||||||
t.log(`Found cached publish folder at ${ CACHE_PATH }`)
|
t.log(`Found cached publish folder at ${ testingCachePath }`)
|
||||||
t.pass()
|
t.pass()
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
@ -37,15 +60,46 @@ test('Can cache publish folder', async (t) => {
|
||||||
|
|
||||||
// If there's no files there already
|
// If there's no files there already
|
||||||
// then we can write to the directory with
|
// then we can write to the directory with
|
||||||
await cachePublishFolder()
|
await cache.cachePublishFolder()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ( (await hasCachedPublishFolder()) === false ) {
|
if ( (await cache.hasCachedPublishFolder()) === false ) {
|
||||||
t.fail()
|
t.fail()
|
||||||
return
|
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()
|
t.pass()
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue