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

Commit

Permalink
deprecate plugin behavior and warn upgrading users about deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Jun 15, 2021
1 parent b3fceee commit 6f95fcb
Showing 1 changed file with 3 additions and 110 deletions.
113 changes: 3 additions & 110 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,112 +1,5 @@
const { resolve, relative } = require(`path`)

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

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

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

for (const dir of dirsToCache) {
await ensureDir(dir)
}

const netlifyCacheDir = resolve(
process.env.NETLIFY_BUILD_BASE,
`cache`,
`gatsby`
exports.onPreInit = async function ({ reporter }) {
reporter.warn(
'gatsby-plugin-netlify-cache got deprecated. This version of the plugin will do nothing. Please migrate to https://github.com/netlify/netlify-plugin-gatsby'
)

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, cachePublic }
) {
if (!process.env.NETLIFY_BUILD_BASE) {
return
}

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

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

await ensureDir(cachePath)

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

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

await copy(cachePath, dirPath)
}

console.log(`plugin-netlify-cache: Netlify cache restored`)
}

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

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

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

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

await copy(dirPath, cachePath)
}

console.log(`plugin-netlify-cache: Netlify cache refilled`)
}

0 comments on commit 6f95fcb

Please sign in to comment.