mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-18 06:44:46 -07:00
Test number of missing endpoints
This commit is contained in:
parent
12326c5b74
commit
a9ca864b54
3 changed files with 119 additions and 20 deletions
|
|
@ -25,11 +25,20 @@ export async function getNetlifyConfig () {
|
|||
return TOML.parse( netlifyTomlContents )
|
||||
}
|
||||
|
||||
export async function readFromJSON ( path ) {
|
||||
// Read back the JSON we just wrote to ensure it exists
|
||||
const savedJSON = await fs.readFile( path , 'utf-8' )
|
||||
|
||||
// console.log('savedListJSON', savedListJSON)
|
||||
|
||||
return JSON.parse( savedJSON )
|
||||
}
|
||||
|
||||
|
||||
export class IncrementalCache {
|
||||
|
||||
constructor( options = {} ) {
|
||||
this.cachePath = options.cachePath || CACHE_PATH
|
||||
this.cachePath = options.cachePath || path.join( CACHE_PATH, 'prod_publish' )
|
||||
this.publishDirectoryName = options.publishDirectoryName || null
|
||||
this.publishDirectoryPath = options.publishDirectoryPath || null
|
||||
|
||||
|
|
@ -44,6 +53,13 @@ export class IncrementalCache {
|
|||
return await fs.pathExists( homePageFile )
|
||||
}
|
||||
|
||||
async hasPublishFolder () {
|
||||
const homePageFile = `${ this.publishDirectoryPath }/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()
|
||||
|
||||
|
|
@ -51,8 +67,6 @@ export class IncrementalCache {
|
|||
}
|
||||
|
||||
async getPublishDirectoryPath () {
|
||||
// const publishDirectoryPath = await this.getPublishDirectoryName()
|
||||
|
||||
return path.resolve( rootDir, this.publishDirectoryName )
|
||||
}
|
||||
|
||||
|
|
@ -70,11 +84,77 @@ export class IncrementalCache {
|
|||
}
|
||||
|
||||
async emptyPublishDirectory () {
|
||||
// const publishDirectoryPath = await this.getPublishDirectoryName()
|
||||
|
||||
return await fs.emptyDir( this.publishDirectoryPath, 'utf-8' )
|
||||
}
|
||||
|
||||
async restoreCachedEndpoints ( options = {} ) {
|
||||
|
||||
const {
|
||||
endpoints,
|
||||
shouldOverwrite = false
|
||||
} = options
|
||||
|
||||
if ( !shouldOverwrite && (await this.hasPublishFolder()) === true ) {
|
||||
throw new Error('Publish folder already exists!')
|
||||
}
|
||||
|
||||
// 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 )
|
||||
|
||||
return fs.copy( cachedPagePath, publishPagePath )
|
||||
}))
|
||||
}
|
||||
|
||||
async restoreCachedNuxtFiles ( options = {} ) {
|
||||
|
||||
const cachedNuxtAssetsPath = path.resolve( this.cachePath, '_nuxt')
|
||||
const publishNuxtAssetsPath = path.resolve( this.publishDirectoryPath, '_nuxt')
|
||||
|
||||
await fs.copy( cachedNuxtAssetsPath, publishNuxtAssetsPath )
|
||||
|
||||
const nuxtEndpointsPath = path.resolve( rootDir, 'static/nuxt-endpoints.json')
|
||||
const nuxtEndpoints = await readFromJSON( nuxtEndpointsPath )
|
||||
|
||||
await this.restoreCachedEndpoints({
|
||||
endpoints: nuxtEndpoints,
|
||||
...options
|
||||
})
|
||||
}
|
||||
|
||||
async syncInCachedPublishFolder () {
|
||||
|
||||
if ( (await this.hasPublishFolder()) !== true ) {
|
||||
throw new Error('Publish folder not found')
|
||||
}
|
||||
|
||||
// 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 findMissingEndpoints () {
|
||||
const sitemapEndpointsPath = path.resolve( rootDir, 'static/sitemap-endpoints.json')
|
||||
const sitemapEndpoints = await readFromJSON( sitemapEndpointsPath )
|
||||
|
||||
const missingEndpoints = []
|
||||
|
||||
for ( const endpoint of sitemapEndpoints ) {
|
||||
const publishPageFile = `${ this.publishDirectoryPath }/index.html`
|
||||
const exists = await fs.pathExists( publishPageFile )
|
||||
|
||||
if (!exists) missingEndpoints.push( endpoint )
|
||||
}
|
||||
|
||||
return missingEndpoints
|
||||
}
|
||||
|
||||
async init () {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue