Skip to content
Draft
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
26 changes: 14 additions & 12 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -19,26 +20,27 @@ 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',
newDestination,
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);
}
});
})
Expand All @@ -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);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"author": "Chanaka Athurugiriya <[email protected]>",
"license": "MIT",
"dependencies": {
"fs-extra": "^9.0.0"
"fs-extra": "^9.0.0",
"mkdirp": "^1.0.4"
},
"keywords": [
"gatsby",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down