-
Notifications
You must be signed in to change notification settings - Fork 3
/
copy-static-files.js
89 lines (81 loc) · 2.42 KB
/
copy-static-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
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
import fs from 'fs';
import { fileURLToPath } from "url";
function getCurrentFolder() {
const folder = fileURLToPath(new URL(".", import.meta.url));
// for windows OS, convert backslash to regular slash and drop drive letter from path
const path = folder.includes('\\')? folder.replaceAll('\\', '/') : folder;
const colon = path.indexOf(':');
return colon == 1? path.substring(colon+1) : path;
}
function getFolder(target) {
return getCurrentFolder() + target;
}
function isDir(folder) {
if (fs.existsSync(folder)) {
const stats = fs.statSync(folder);
return stats.isDirectory();
} else {
return false;
}
}
function copyFolder(src, target) {
if (fs.existsSync(src)) {
fs.readdirSync(src).forEach(f => {
const srcPath = `${src}/${f}`
const targetPath = `${target}/${f}`
const srcStats = fs.statSync(srcPath);
if (srcStats.isDirectory()) {
createParentFolders(targetPath);
copyFolder(srcPath, targetPath);
} else {
if (fs.existsSync(targetPath)) {
fs.rmSync(targetPath);
}
fs.copyFileSync(srcPath, targetPath);
}
})
}
}
function createParentFolders(path) {
const parts = path.split('/');
let s = '';
for (let i=0; i < parts.length; i++) {
if (parts[i]) {
s += `/${parts[i]}`;
if (!fs.existsSync(s)) {
fs.mkdirSync(s);
}
}
}
}
function copyResourceFiles(src, target) {
if (!fs.existsSync(src)) {
console.log(`ERROR: ${src} does not exist`);
return;
}
if (!fs.existsSync(target)) {
console.log(`ERROR: ${target} does not exist`);
return;
}
if (!isDir(src)) {
console.log(`ERROR: ${src} is not a folder`);
return;
}
if (!isDir(target)) {
console.log(`ERROR: ${target} is not a folder`);
return;
}
const srcResource = `${src}/resources`;
if (!isDir(srcResource)) {
console.log(`ERROR: ${srcResource} is not a folder`);
return;
}
const targetResource = `${target}/resources`;
if (!isDir(targetResource)) {
fs.mkdirSync(targetResource);
}
copyFolder(srcResource, targetResource);
}
const src = getFolder('src');
const target = getFolder('dist');
copyResourceFiles(src, target);