mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-18 06:44:46 -07:00
Enable caching publish directory
This commit is contained in:
parent
a29acbb27b
commit
170f705f0e
4 changed files with 85 additions and 4 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -80,6 +80,9 @@ dist
|
||||||
# IDE
|
# IDE
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
|
# Incremental Cache
|
||||||
|
/.app_build_cache
|
||||||
|
|
||||||
|
|
||||||
# Other
|
# Other
|
||||||
/static/app-list.json
|
/static/app-list.json
|
||||||
|
|
@ -88,3 +91,4 @@ dist
|
||||||
/commits-data.json
|
/commits-data.json
|
||||||
.DS_Store
|
.DS_Store
|
||||||
/static/tailwind.css
|
/static/tailwind.css
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,7 @@
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const isProduction = process.env.NODE_ENV === 'PRODUCTION'
|
export const isProduction = process.env.NODE_ENV === 'PRODUCTION'
|
||||||
|
|
||||||
|
export const rootDir = path.resolve(__dirname, '../')
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,17 @@
|
||||||
import { promises as fs } from 'fs'
|
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
|
||||||
|
import fs from 'fs-extra'
|
||||||
|
|
||||||
|
import { isProduction, rootDir } from '../environment.js'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// https://github.com/hanbyul-here/nuxt-incremental-build-exp/blob/cb3ef6b001b283de77efee64733db273d991129b/cache-me.js
|
||||||
|
export const CACHE_PATH = isProduction
|
||||||
|
? path.join('/', 'opt', 'build', 'cache', 'app_build') // Netlify cache path
|
||||||
|
: path.join(rootDir, '.app_build_cache')//path.resolve(__dirname, '.app_build_cache')
|
||||||
|
|
||||||
|
|
||||||
export async function getNetlifyConfig () {
|
export async function getNetlifyConfig () {
|
||||||
|
|
@ -11,3 +21,39 @@ export async function getNetlifyConfig () {
|
||||||
|
|
||||||
return TOML.parse( netlifyTomlContents )
|
return TOML.parse( netlifyTomlContents )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 )
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
import test from 'ava'
|
import test from 'ava'
|
||||||
|
|
||||||
import { isObject, isString } from '../type-checks.js'
|
import { isObject, isString } from '../type-checks.js'
|
||||||
import { getNetlifyConfig } from './caching.js'
|
import {
|
||||||
|
getNetlifyConfig,
|
||||||
|
hasCachedPublishFolder,
|
||||||
|
cachePublishFolder
|
||||||
|
} from './caching.js'
|
||||||
|
|
||||||
|
|
||||||
test('Can read netlify.toml', async (t) => {
|
test('Can read netlify.toml', async (t) => {
|
||||||
|
|
@ -15,8 +19,30 @@ test('Can read netlify.toml', async (t) => {
|
||||||
t.is( isString( netlifyConfig.build.publish ) , true )
|
t.is( isString( netlifyConfig.build.publish ) , true )
|
||||||
})
|
})
|
||||||
|
|
||||||
// test('Can cache publish folder', async (t) => {
|
|
||||||
|
test('Can cache publish folder', async (t) => {
|
||||||
|
// So that we don't overwrite the cached files
|
||||||
|
// we check if a cached file already exists
|
||||||
|
|
||||||
|
if ( await hasCachedPublishFolder() ) {
|
||||||
|
t.log('Publish folder has been cached already')
|
||||||
|
t.pass()
|
||||||
|
}
|
||||||
|
|
||||||
|
t.log('No prexisting cache folder found')
|
||||||
|
|
||||||
|
|
||||||
|
// If there's no files there already
|
||||||
|
// then we can write to the directory with
|
||||||
|
await cachePublishFolder()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// })
|
if ( (await hasCachedPublishFolder()) === false ) {
|
||||||
|
t.fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.log('Publish folder has been cached already')
|
||||||
|
t.pass()
|
||||||
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue