Test that publish can be restored from cache

This commit is contained in:
Sam Carlton 2021-06-26 15:32:20 -05:00
parent a9ca864b54
commit 04d59d8a49
2 changed files with 69 additions and 21 deletions

View file

@ -3,6 +3,7 @@ import path from 'path'
import { default as TOML } from '@iarna/toml'
// https://github.com/jprichardson/node-fs-extra
import fs from 'fs-extra'
import Rsync from 'rsync'
import { isNetlify, rootDir } from '../environment.js'
@ -41,7 +42,6 @@ export class IncrementalCache {
this.cachePath = options.cachePath || path.join( CACHE_PATH, 'prod_publish' )
this.publishDirectoryName = options.publishDirectoryName || null
this.publishDirectoryPath = options.publishDirectoryPath || null
}
// export async function
@ -100,8 +100,8 @@ export class IncrementalCache {
// Copy concurrently for speed
await Promise.all( endpoints.map( endpoint => {
const cachedPagePath = path.join( this.cachePath, endpoint.route )
const publishPagePath = path.join( this.publishDirectoryPath, endpoint.route )
const cachedPagePath = path.join( this.cachePath, endpoint.route, '/index.html' )
const publishPagePath = path.join( this.publishDirectoryPath, endpoint.route, '/index.html' )
return fs.copy( cachedPagePath, publishPagePath )
}))
@ -109,26 +109,69 @@ export class IncrementalCache {
async restoreCachedNuxtFiles ( options = {} ) {
const cachedNuxtAssetsPath = path.resolve( this.cachePath, '_nuxt')
const publishNuxtAssetsPath = path.resolve( this.publishDirectoryPath, '_nuxt')
const nonEndpointPaths = [
'_nuxt',
'index.html',
'sitemap-endpoints.json',
'nuxt-endpoints.json'
]
await fs.copy( cachedNuxtAssetsPath, publishNuxtAssetsPath )
for ( const assetPath of nonEndpointPaths ) {
const cachedNuxtAssetsPath = path.resolve( this.cachePath, assetPath)
const publishNuxtAssetsPath = path.resolve( this.publishDirectoryPath, assetPath)
const nuxtEndpointsPath = path.resolve( rootDir, 'static/nuxt-endpoints.json')
await fs.copy( cachedNuxtAssetsPath, publishNuxtAssetsPath )
}
// await fs.copy( cachedNuxtAssetsPath, publishNuxtAssetsPath )
// await fs.copy( this.cachePath + '/index.html', this.publishDirectoryPath + '/index.html' )
// await fs.copy( this.cachePath + '/static', this.publishDirectoryPath + '/index.html' )
const nuxtEndpointsPath = path.resolve( this.publishDirectoryPath, 'nuxt-endpoints.json')
const nuxtEndpoints = await readFromJSON( nuxtEndpointsPath )
await this.restoreCachedEndpoints({
endpoints: nuxtEndpoints,
shouldOverwrite: true,
...options
})
}
// Original concept: https://github.com/hanbyul-here/nuxt-incremental-build-exp/blob/master/cache-me.js
rsyncCacheToPublish = () => new Promise( ( resolve, reject ) => {
const rsync = new Rsync()
.shell('ssh')
.flags('azq')
.source(this.cachePath + '/')
.destination(this.publishDirectoryPath)
try {
// await fs.ensureDir(CACHE_PATH)
rsync.execute(async function(error, code, cmd) {
if (!error) {
// await fs.copy(CACHE_PATH, BUILD_PATH)
// await cacheFinalFiles()
console.log('Rsync finished')
resolve()
} else console.error('error')
})
} catch (err) {
// handle error
console.warn('Rsync error', err)
reject(err)
}
})
async syncInCachedPublishFolder () {
if ( (await this.hasPublishFolder()) !== true ) {
throw new Error('Publish folder not found')
}
await this.rsyncCacheToPublish()
// const publishDirectoryPath = await this.getPublishDirectoryPath()
// console.log('publishDirectoryPath', publishDirectoryPath)
@ -140,13 +183,21 @@ export class IncrementalCache {
}
async findMissingEndpoints () {
const sitemapEndpointsPath = path.resolve( rootDir, 'static/sitemap-endpoints.json')
async getPublishSitemapEndpoints () {
const sitemapEndpointsPath = path.resolve( this.publishDirectoryPath, 'sitemap-endpoints.json')
const sitemapEndpoints = await readFromJSON( sitemapEndpointsPath )
return sitemapEndpoints
}
async findMissingEndpoints ( options = {} ) {
const {
endpoints
} = options
const missingEndpoints = []
for ( const endpoint of sitemapEndpoints ) {
for ( const endpoint of endpoints ) {
const publishPageFile = `${ this.publishDirectoryPath }/index.html`
const exists = await fs.pathExists( publishPageFile )

View file

@ -101,20 +101,17 @@ test.serial('Can restore publish folder from cache', async (t) => {
// would during a build
await cache.restoreCachedNuxtFiles()
// 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()
// Sync remain cached files into publish folder
// so Eleventy only has to build updated endpoints
await cache.syncInCachedPublishFolder()
// List missing endpoints
// so we can compare
const missingEndpoints = await cache.findMissingEndpoints()
// so we can compare for this test
const sitemapEndpoints = await cache.getPublishSitemapEndpoints()
const missingEndpoints = await cache.findMissingEndpoints({
endpoints: sitemapEndpoints
})
// t.log(`Cached publish folder at ${ testingCachePath }`)