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

feat: verbose file count #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -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
Copy link
Author

@d4rekanguok d4rekanguok Oct 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually after testing this code, since each netlify build is completely fresh, I'm not sure we need to log out already existing files, it'll always be 0?

Instead it could be more informative to store the file count of each cache directory & compare it on post build, i.e cached extra 12 files & removed 1 file

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you additionally cache files within node_modules/.cache, it might happen that netlify already restored files for you.

Still, that post build information would be pretty useful. Happy to merge code that implements it :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah true, I didn't think of the caches in node_modules. Will work on that post build info when I can! :)

} 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) {
Expand All @@ -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`)
}
24 changes: 24 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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