This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deprecate plugin behavior and warn upgrading users about deprecation
- Loading branch information
Showing
1 changed file
with
3 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} |