mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-15 06:35:20 -07:00
Test that publish can be restored from cache
This commit is contained in:
parent
a9ca864b54
commit
04d59d8a49
2 changed files with 69 additions and 21 deletions
|
|
@ -3,6 +3,7 @@ import path from 'path'
|
||||||
import { default as TOML } from '@iarna/toml'
|
import { default as TOML } from '@iarna/toml'
|
||||||
// https://github.com/jprichardson/node-fs-extra
|
// https://github.com/jprichardson/node-fs-extra
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
|
import Rsync from 'rsync'
|
||||||
|
|
||||||
import { isNetlify, rootDir } from '../environment.js'
|
import { isNetlify, rootDir } from '../environment.js'
|
||||||
|
|
||||||
|
|
@ -41,7 +42,6 @@ export class IncrementalCache {
|
||||||
this.cachePath = options.cachePath || path.join( CACHE_PATH, 'prod_publish' )
|
this.cachePath = options.cachePath || path.join( CACHE_PATH, 'prod_publish' )
|
||||||
this.publishDirectoryName = options.publishDirectoryName || null
|
this.publishDirectoryName = options.publishDirectoryName || null
|
||||||
this.publishDirectoryPath = options.publishDirectoryPath || null
|
this.publishDirectoryPath = options.publishDirectoryPath || null
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// export async function
|
// export async function
|
||||||
|
|
@ -100,8 +100,8 @@ export class IncrementalCache {
|
||||||
|
|
||||||
// Copy concurrently for speed
|
// Copy concurrently for speed
|
||||||
await Promise.all( endpoints.map( endpoint => {
|
await Promise.all( endpoints.map( endpoint => {
|
||||||
const cachedPagePath = path.join( this.cachePath, endpoint.route )
|
const cachedPagePath = path.join( this.cachePath, endpoint.route, '/index.html' )
|
||||||
const publishPagePath = path.join( this.publishDirectoryPath, endpoint.route )
|
const publishPagePath = path.join( this.publishDirectoryPath, endpoint.route, '/index.html' )
|
||||||
|
|
||||||
return fs.copy( cachedPagePath, publishPagePath )
|
return fs.copy( cachedPagePath, publishPagePath )
|
||||||
}))
|
}))
|
||||||
|
|
@ -109,26 +109,69 @@ export class IncrementalCache {
|
||||||
|
|
||||||
async restoreCachedNuxtFiles ( options = {} ) {
|
async restoreCachedNuxtFiles ( options = {} ) {
|
||||||
|
|
||||||
const cachedNuxtAssetsPath = path.resolve( this.cachePath, '_nuxt')
|
const nonEndpointPaths = [
|
||||||
const publishNuxtAssetsPath = path.resolve( this.publishDirectoryPath, '_nuxt')
|
'_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 )
|
const nuxtEndpoints = await readFromJSON( nuxtEndpointsPath )
|
||||||
|
|
||||||
await this.restoreCachedEndpoints({
|
await this.restoreCachedEndpoints({
|
||||||
endpoints: nuxtEndpoints,
|
endpoints: nuxtEndpoints,
|
||||||
|
shouldOverwrite: true,
|
||||||
...options
|
...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 () {
|
async syncInCachedPublishFolder () {
|
||||||
|
|
||||||
if ( (await this.hasPublishFolder()) !== true ) {
|
if ( (await this.hasPublishFolder()) !== true ) {
|
||||||
throw new Error('Publish folder not found')
|
throw new Error('Publish folder not found')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.rsyncCacheToPublish()
|
||||||
|
|
||||||
// const publishDirectoryPath = await this.getPublishDirectoryPath()
|
// const publishDirectoryPath = await this.getPublishDirectoryPath()
|
||||||
|
|
||||||
// console.log('publishDirectoryPath', publishDirectoryPath)
|
// console.log('publishDirectoryPath', publishDirectoryPath)
|
||||||
|
|
@ -140,13 +183,21 @@ export class IncrementalCache {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findMissingEndpoints () {
|
async getPublishSitemapEndpoints () {
|
||||||
const sitemapEndpointsPath = path.resolve( rootDir, 'static/sitemap-endpoints.json')
|
const sitemapEndpointsPath = path.resolve( this.publishDirectoryPath, 'sitemap-endpoints.json')
|
||||||
const sitemapEndpoints = await readFromJSON( sitemapEndpointsPath )
|
const sitemapEndpoints = await readFromJSON( sitemapEndpointsPath )
|
||||||
|
|
||||||
|
return sitemapEndpoints
|
||||||
|
}
|
||||||
|
|
||||||
|
async findMissingEndpoints ( options = {} ) {
|
||||||
|
const {
|
||||||
|
endpoints
|
||||||
|
} = options
|
||||||
|
|
||||||
const missingEndpoints = []
|
const missingEndpoints = []
|
||||||
|
|
||||||
for ( const endpoint of sitemapEndpoints ) {
|
for ( const endpoint of endpoints ) {
|
||||||
const publishPageFile = `${ this.publishDirectoryPath }/index.html`
|
const publishPageFile = `${ this.publishDirectoryPath }/index.html`
|
||||||
const exists = await fs.pathExists( publishPageFile )
|
const exists = await fs.pathExists( publishPageFile )
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -101,20 +101,17 @@ test.serial('Can restore publish folder from cache', async (t) => {
|
||||||
// would during a build
|
// would during a build
|
||||||
await cache.restoreCachedNuxtFiles()
|
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
|
// List missing endpoints
|
||||||
// so we can compare
|
// so we can compare for this test
|
||||||
const missingEndpoints = await cache.findMissingEndpoints()
|
const sitemapEndpoints = await cache.getPublishSitemapEndpoints()
|
||||||
|
const missingEndpoints = await cache.findMissingEndpoints({
|
||||||
|
endpoints: sitemapEndpoints
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
// t.log(`Cached publish folder at ${ testingCachePath }`)
|
// t.log(`Cached publish folder at ${ testingCachePath }`)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue