-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
64 lines (55 loc) · 1.6 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const path = require('path');
const fsExtra = require('fs-extra');
const getDirectories = source =>
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;
const sourceNormalized = path.normalize(source);
if (node.internal.type === 'File') {
const dir = path.normalize(node.dir);
if (dir.includes(sourceNormalized)) {
const relativeToDest = dir.replace(sourceNormalized, '');
// 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))
dirList.forEach(e => {
const newDestination = regPrefix + '/' + e + regPostfix;
const newPath = path.join(
process.cwd(),
'public',
newDestination,
relativeToDest,
node.base
);
fsExtra.copy(node.absolutePath, newPath, { overwrite: purge }, err => {
if (err) {
console.error('Error copying file', err);
}
});
})
}
// if regex not enabled
else {
const newPath = path.join(
process.cwd(),
'public',
destination,
relativeToDest,
node.base
);
fsExtra.copy(node.absolutePath, newPath, { overwrite: purge }, err => {
if (err) {
console.error('Error copying file', err);
}
});
}
}
}
};