-
Notifications
You must be signed in to change notification settings - Fork 2
/
copy-files.js
53 lines (47 loc) · 1.23 KB
/
copy-files.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
/**
*
*
* @module copy-files
*
* Created by Evgeniy Malyarov on 21.11.2018.
*/
const path = require('path');
const fs = require('fs');
const md5File = require('md5-file');
const lpath = path.resolve(__dirname, './dist');
const rpath = path.resolve(__dirname, './src');;
function fromDir(startPath, filter, callback) {
if(!fs.existsSync(startPath)) {
console.log('no dir ', startPath);
return;
}
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
if(/node_modules/.test(filename)){
continue;
}
const stat = fs.lstatSync(filename);
if(stat.isDirectory()) {
fromDir(filename, filter, callback); //recurse
}
else if(filter.test(filename)) callback(filename);
};
};
let copied;
let i = 0;
fromDir(rpath, /\.(css|md|woff2|svg|gif|png)$/, (rname) => {
const name = rname.replace(rpath, '');
const lame = path.join(lpath, name);
if(!fs.existsSync(lame) || (md5File.sync(rname) != md5File.sync(lame))){
i++;
fs.createReadStream(rname).pipe(fs.createWriteStream(lame));
}
});
if(i){
copied = true;
console.log(`from ${rpath} written ${i} files`);
}
if(!copied){
console.log(`all files match`);
}