Repository navigation
Expand file tree
/
Copy pathpostbuild.js
More file actions
78 lines (71 loc) · 2.28 KB
/
Copy pathpostbuild.js
File metadata and controls
78 lines (71 loc) · 2.28 KB
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
import path from 'node:path';
import fs from 'node:fs';
const targetSource = './build'; // Relative path to copy files from
const targetDestination = './pysrc/ewoksweb/static'; // Relative path to copy files to
/**
* Remove directory recursively
* @param {string} dir_path
* @see https://stackoverflow.com/a/42505874
*/
function rimraf(dir_path) {
if (fs.existsSync(dir_path)) {
fs.readdirSync(dir_path).forEach((entry) => {
const entry_path = path.join(dir_path, entry);
if (fs.lstatSync(entry_path).isDirectory()) {
rimraf(entry_path);
} else {
fs.unlinkSync(entry_path);
}
});
fs.rmdirSync(dir_path);
}
}
/**
* Copy a file
* @param {string} source
* @param {string} target
* @see https://stackoverflow.com/a/26038979
*/
function copyFileSync(source, target) {
let targetFile = target;
// If target is a directory a new file with the same name will be created
if (fs.existsSync(target) && fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
/**
* Copy a folder recursively
* @param {string} source
* @param {string} target
* @see https://stackoverflow.com/a/26038979
*/
function copyFolderRecursiveSync(source, target, root = false) {
let files = [];
// Check if folder needs to be created or integrated
const targetFolder = root ? target : path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder, { recursive: true });
}
// Copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach((file) => {
const curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
// Calculate absolute paths using the relative paths we defined at the top
const sourceFolder = path.resolve(targetSource);
const destinationFolder = path.resolve(targetDestination);
// Remove destination folder if it exists to clear it
if (fs.existsSync(destinationFolder)) {
rimraf(destinationFolder);
}
// Copy the build over
copyFolderRecursiveSync(sourceFolder, destinationFolder, true);