Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
improve logging and cache folder name generation to truely allow nest…
Browse files Browse the repository at this point in the history
…ed extra dirs
  • Loading branch information
axe312ger committed May 10, 2019
1 parent 041b353 commit 89da4ff
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const { resolve, basename } = require(`path`)
const { resolve, relative } = require(`path`)

const { ensureDir, readdir, copy } = require(`fs-extra`)

async function calculateDirs(store, extraDirsToCache = []) {
async function calculateDirs(store, { extraDirsToCache = [] }) {
const program = store.getState().program
const rootDirectory = program.directory

const dirsToCache = [
resolve(program.directory, `public`),
resolve(program.directory, `.cache`),
...extraDirsToCache.map(dirToCache =>
resolve(program.directory, dirToCache)
)
]
resolve(rootDirectory, `public`),
resolve(rootDirectory, `.cache`),
...extraDirsToCache.map(dirToCache => resolve(rootDirectory, dirToCache))
].filter(Boolean)

for (const dir of dirsToCache) {
await ensureDir(dir)
Expand All @@ -26,34 +25,50 @@ async function calculateDirs(store, extraDirsToCache = []) {
await ensureDir(netlifyCacheDir)

return {
rootDirectory,
dirsToCache,
netlifyCacheDir
}
}

function generateCacheDirectoryNames(rootDirectory, netlifyCacheDir, dirPath) {
const relativePath = relative(rootDirectory, dirPath)
const dirName = relativePath.replace('/', '--')
const cachePath = resolve(netlifyCacheDir, dirName)
const humanName = relativePath
return { cachePath, humanName }
}

exports.onPreInit = async function({ store }, { extraDirsToCache }) {
if (!process.env.NETLIFY_BUILD_BASE) {
return
}

const { dirsToCache, netlifyCacheDir } = await calculateDirs(
const { dirsToCache, netlifyCacheDir, rootDirectory } = await calculateDirs(
store,
extraDirsToCache
{
extraDirsToCache
}
)

for (const dirPath of dirsToCache) {
const dirName = basename(dirPath)
const cachePath = resolve(netlifyCacheDir, dirName)
const { cachePath, humanName } = generateCacheDirectoryNames(
rootDirectory,
netlifyCacheDir,
dirPath
)

await ensureDir(cachePath)

const dirFiles = await readdir(dirPath)
const cacheFiles = await readdir(cachePath)

console.log(
`Found ${cacheFiles.length} cached files for ${dirName} directory with ${
`plugin-netlify-cache: Restoring ${
cacheFiles.length
} cached files for ${humanName} directory with ${
dirFiles.length
} files.`
} already existing files.`
)

await copy(cachePath, dirPath)
Expand All @@ -67,24 +82,22 @@ exports.onPostBuild = async function({ store }, { extraDirsToCache }) {
return
}

const { dirsToCache, netlifyCacheDir } = await calculateDirs(
const { dirsToCache, netlifyCacheDir, rootDirectory } = await calculateDirs(
store,
extraDirsToCache
{
extraDirsToCache
}
)

for (const dirPath of dirsToCache) {
const dirName = basename(dirPath)
const cachePath = resolve(netlifyCacheDir, dirName)

const dirFiles = await readdir(dirPath)
const cacheFiles = await readdir(cachePath)

console.log(
`Found ${dirFiles.length} files in ${dirName} directory with ${
cacheFiles.length
} already cached files.`
const { cachePath, humanName } = generateCacheDirectoryNames(
rootDirectory,
netlifyCacheDir,
dirPath
)

console.log(`plugin-netlify-cache: Caching ${humanName}...`)

await copy(dirPath, cachePath)
}

Expand Down

0 comments on commit 89da4ff

Please sign in to comment.