From b3be1140869981c9f2f56985783ca1b2d8fea3a0 Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Sat, 2 Oct 2021 12:12:01 -0700 Subject: [PATCH 1/2] fix: Make sure the directory exists before trying to copy files to it --- gatsby-node.js | 33 ++++++++++++++++++--------------- package.json | 3 ++- yarn.lock | 5 +++++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 1218f53..702c85c 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,15 +1,16 @@ const path = require('path'); +const mkdirp = require('mkdirp') const fsExtra = require('fs-extra'); const getDirectories = source => - fsExtra.readdirSync(source, { withFileTypes: true }) + fsExtra.readdirSync(source, {withFileTypes: true}) .filter(dirent => dirent.isDirectory()) .map(dirent => dirent.name) const regex1 = RegExp('(.*)\\/\\*(.*)'); -exports.onCreateNode = ({ node }, pluginOptions) => { - const { source, destination = '', purge = false } = pluginOptions; +exports.onCreateNode = ({node, reporter}, pluginOptions) => { + const {source, destination = '', purge = false} = pluginOptions; const sourceNormalized = path.normalize(source); if (node.internal.type === 'File') { const dir = path.normalize(node.dir); @@ -19,16 +20,16 @@ exports.onCreateNode = ({ node }, pluginOptions) => { // if regex enabled if (regex1.test(destination)) { const hits = regex1.exec(destination) - + if (!hits) return - + const regPrefix = hits[1] const regPostfix = hits[2] - const dirList = getDirectories(path.join(process.cwd(), 'public', regPrefix)) - + const dirList = getDirectories(path.join(process.cwd(), 'public', regPrefix)) + dirList.forEach(e => { - const newDestination = regPrefix + '/' + e + regPostfix; + const newDestination = regPrefix + '/' + e + regPostfix; const newPath = path.join( process.cwd(), 'public', @@ -36,11 +37,13 @@ exports.onCreateNode = ({ node }, pluginOptions) => { relativeToDest, node.base ); - fsExtra.copy(node.absolutePath, newPath, { overwrite: purge }, err => { - if (err) { - console.error('Error copying file', err); - } - }); + mkdirp(path.dirname(newPath)).then(() => { + fsExtra.copy(node.absolutePath, newPath, {overwrite: purge}, err => { + if (err) { + reporter.error('Error copying file', err); + } + }); + }) }) } // if regex not enabled @@ -53,9 +56,9 @@ exports.onCreateNode = ({ node }, pluginOptions) => { node.base ); - fsExtra.copy(node.absolutePath, newPath, { overwrite: purge }, err => { + fsExtra.copy(node.absolutePath, newPath, {overwrite: purge}, err => { if (err) { - console.error('Error copying file', err); + reporter.error('Error copying file', err); } }); } diff --git a/package.json b/package.json index 6f319fa..33feb0a 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "author": "Chanaka Athurugiriya ", "license": "MIT", "dependencies": { - "fs-extra": "^9.0.0" + "fs-extra": "^9.0.0", + "mkdirp": "^1.0.4" }, "keywords": [ "gatsby", diff --git a/yarn.lock b/yarn.lock index 92c6d1f..2e71c58 100644 --- a/yarn.lock +++ b/yarn.lock @@ -31,6 +31,11 @@ jsonfile@^6.0.1: optionalDependencies: graceful-fs "^4.1.6" +mkdirp@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" + integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== + universalify@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/universalify/-/universalify-1.0.0.tgz#b61a1da173e8435b2fe3c67d29b9adf8594bd16d" From 042d0ba1f519829be709e03771876cab7229f3a7 Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Sat, 2 Oct 2021 12:16:43 -0700 Subject: [PATCH 2/2] Make sure directory is created before we scan the directory --- gatsby-node.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 702c85c..c8c9512 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -26,6 +26,7 @@ exports.onCreateNode = ({node, reporter}, pluginOptions) => { const regPrefix = hits[1] const regPostfix = hits[2] + mkdirp.sync(path.join(process.cwd(), 'public', regPrefix)) const dirList = getDirectories(path.join(process.cwd(), 'public', regPrefix)) dirList.forEach(e => { @@ -37,13 +38,11 @@ exports.onCreateNode = ({node, reporter}, pluginOptions) => { relativeToDest, node.base ); - mkdirp(path.dirname(newPath)).then(() => { - fsExtra.copy(node.absolutePath, newPath, {overwrite: purge}, err => { - if (err) { - reporter.error('Error copying file', err); - } - }); - }) + fsExtra.copy(node.absolutePath, newPath, {overwrite: purge}, err => { + if (err) { + reporter.error('Error copying file', err); + } + }); }) } // if regex not enabled