/
index.js
125 lines (118 loc) · 3.26 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const fse = require("fs-extra");
const fs = require("fs");
const path = require("path");
const name = "webpack-copy-noloop-plugin";
const isDir = location => fs.statSync(location).isDirectory();
const gatherFiles = location => pattern => {
return fs.readdirSync(location).reduce((last, file) => {
const fileInAbs = path.join(location, "/", file);
if (isDir(fileInAbs)) {
last.concat(gatherFiles(path.join(location, "/", file))(pattern));
} else {
if (pattern.test(fileInAbs)) {
last.push(fileInAbs);
}
}
return last;
}, []);
};
/**
* if copy file from directory
* generate proper dest path
* @param {string} dirFromPath
* @param {string} dirToPath
* @returns {function(string, boolean=)}
*/
const getDestPath = (dirFromPath, dirToPath) => (
fileFromPath,
flatten = false
) => {
const relativePath = flatten
? path.basename(fileFromPath)
: path.relative(dirFromPath, fileFromPath);
return path.join(dirToPath, "/", relativePath);
};
/**
* @typedef {object} CopyItem
* @property {string} from
* @property {string} to
* @property {function(string, string)=} filter
* @property {regexp=} pattern
* @property {boolean=} flatten
*/
class CopyNoLoopPlugin {
/**
* @param {object} options
* @param {CopyItem[]} options.list
* @param {string=} options.root root directory
* @param {boolean=} [options.move=false] whether to move or not
* @param {function|number=} [options.waitFor=() => Promise.resolve()]
*/
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.hooks.done.tap("CopyNoLoopPlugin", async () => {
const {
list,
root,
move = false,
waitFor = () => Promise.resolve()
} = this.options;
const action = move ? "moveSync" : "copySync";
if (!Array.isArray(list)) return;
let listMut = list.slice();
if (root) {
// check for absolute
if (!path.isAbsolute(root)) {
throw new Error(`[${name}]: root needs to be an absolute path!`);
}
listMut = listMut.map(item => {
return {
...item,
from: path.resolve(root, item.from),
to: path.resolve(root, item.to)
};
});
}
try {
switch (typeof waitFor) {
case "function":
await waitFor();
break;
case "number":
await new Promise(resolve => setTimeout(resolve, waitFor));
break;
default:
break;
}
} catch (e) {
console.log(`[${name}]: Err occurred when trying to execute waitFor`);
console.log(e);
process.exit(1);
}
listMut.forEach(item => {
const {
from,
to,
filter = () => true,
pattern,
flatten = false
} = item;
if (isDir(from) && pattern) {
const files = gatherFiles(from)(pattern);
files.forEach(file => {
const dest = getDestPath(from, to)(file, flatten);
fse[action](file, dest, { filter, overwrite: true });
});
} else {
fse[action](from, to, {
filter,
overwrite: true
});
}
});
});
}
}
module.exports = CopyNoLoopPlugin;