diff --git a/packages/gatsby/src/commands/build-utils.ts b/packages/gatsby/src/commands/build-utils.ts index 93565b526b72a..a239130e121e9 100644 --- a/packages/gatsby/src/commands/build-utils.ts +++ b/packages/gatsby/src/commands/build-utils.ts @@ -8,6 +8,9 @@ import { import { remove as removePageDataFile, fixedPagePath } from "../utils/page-data" import { IGatsbyState } from "../redux/types" +const checkFolderIsEmpty = (path: string): boolean => + fs.existsSync(path) && !fs.readdirSync(path).length + export const getChangedPageDataKeys = ( state: IGatsbyState, cachedPageData: Map @@ -55,11 +58,11 @@ const checkAndRemoveEmptyDir = (publicDir: string, pagePath: string): void => { `page-data`, fixedPagePath(pagePath) ) - const hasFiles = fs.readdirSync(pageHtmlDirectory) - - // if page's html folder is empty also remove matching page-data folder - if (!hasFiles.length) { + // if page's folder is empty also remove matching page-data folder + if (checkFolderIsEmpty(pageHtmlDirectory)) { fs.removeSync(pageHtmlDirectory) + } + if (checkFolderIsEmpty(pageDataDirectory)) { fs.removeSync(pageDataDirectory) } } diff --git a/packages/gatsby/src/utils/page-data.js b/packages/gatsby/src/utils/page-data.js index 277975d1d8fd7..374bdae1df472 100644 --- a/packages/gatsby/src/utils/page-data.js +++ b/packages/gatsby/src/utils/page-data.js @@ -15,7 +15,10 @@ const read = async ({ publicDir }, pagePath) => { const remove = async ({ publicDir }, pagePath) => { const filePath = getFilePath({ publicDir }, pagePath) - return fs.remove(filePath) + if (fs.existsSync(filePath)) { + return await fs.remove(filePath) + } + return Promise.resolve() } const write = async ({ publicDir }, page, result) => { diff --git a/packages/gatsby/src/utils/page-html.ts b/packages/gatsby/src/utils/page-html.ts index 10108890fc25f..505a8471caf8b 100644 --- a/packages/gatsby/src/utils/page-html.ts +++ b/packages/gatsby/src/utils/page-html.ts @@ -23,6 +23,8 @@ export const remove = async ( pagePath: string ): Promise => { const filePath = getPageHtmlFilePath(publicDir, pagePath) - - return await fs.remove(filePath) + if (fs.existsSync(filePath)) { + return await fs.remove(filePath) + } + return Promise.resolve() }