diff --git a/src/gatsby-node.js b/src/gatsby-node.js index 4b246d3..fdf3a7c 100644 --- a/src/gatsby-node.js +++ b/src/gatsby-node.js @@ -2,6 +2,8 @@ const { resolve, relative } = require(`path`) const { ensureDir, readdir, copy } = require(`fs-extra`) +const { readFileCount } = require('./utils') + async function calculateDirs( store, { extraDirsToCache = [], cachePublic = false } @@ -43,8 +45,8 @@ function generateCacheDirectoryNames(rootDirectory, netlifyCacheDir, dirPath) { } exports.onPreInit = async function( - { store }, - { extraDirsToCache, cachePublic } + { store, reporter }, + { extraDirsToCache, cachePublic, verbose = false } ) { if (!process.env.NETLIFY_BUILD_BASE) { return @@ -67,25 +69,34 @@ exports.onPreInit = async function( await ensureDir(cachePath) - const dirFiles = await readdir(dirPath) - const cacheFiles = await readdir(cachePath) + let dirFileCount + let cacheFileCount + if (verbose) { + dirFileCount = await readFileCount(dirPath) + cacheFileCount = await readFileCount(cachePath) + } else { + const dirFiles = await readdir(dirPath) + const cacheFiles = await readdir(cachePath) + dirFileCount = dirFiles.length + cacheFileCount = cacheFiles.length + } - console.log( + reporter.info( `plugin-netlify-cache: Restoring ${ - cacheFiles.length + cacheFileCount } cached files for ${humanName} directory with ${ - dirFiles.length + dirFileCount } already existing files.` ) await copy(cachePath, dirPath) } - console.log(`plugin-netlify-cache: Netlify cache restored`) + reporter.success(`plugin-netlify-cache: Netlify cache restored`) } exports.onPostBuild = async function( - { store }, + { store, reporter }, { extraDirsToCache, cachePublic } ) { if (!process.env.NETLIFY_BUILD_BASE) { @@ -107,10 +118,10 @@ exports.onPostBuild = async function( dirPath ) - console.log(`plugin-netlify-cache: Caching ${humanName}...`) + reporter.info(`plugin-netlify-cache: Caching ${humanName}...`) await copy(dirPath, cachePath) } - console.log(`plugin-netlify-cache: Netlify cache refilled`) + reporter.success(`plugin-netlify-cache: Netlify cache refilled`) } diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..d97cd91 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,24 @@ +const fs = require(`fs-extra`) +const path = require(`path`) + +async function readFileCount(targetPath) { + const files = await fs.readdir(targetPath) + const countP = files.map(async file => { + const filePath = path.join(targetPath, file) + const stats = await fs.stat(filePath) + if (stats.isDirectory()) { + const count = await readFileCount(filePath) + return count + } + if (stats.isFile()) { + return 1 + } + return 0 + }) + + const results = await Promise.all(countP) + const total = results.reduce((cur, acc) => (acc + cur), 0) + return total +} + +exports.readFileCount = readFileCount \ No newline at end of file