diff --git a/gatsby-node.js b/gatsby-node.js index 1218f53..c8c9512 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,17 @@ 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)) - + mkdirp.sync(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,9 +38,9 @@ exports.onCreateNode = ({ node }, pluginOptions) => { relativeToDest, 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); } }); }) @@ -53,9 +55,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 <chanakaathurugiriya@gmail.com>", "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"