-
Notifications
You must be signed in to change notification settings - Fork 2
/
copyAssets.js
46 lines (34 loc) · 1.31 KB
/
copyAssets.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
const fs = require("node:fs");
const path = require("node:path");
const copyAssets = (sourceDir, destinationDir) => {
const allFiles = fs.readdirSync(sourceDir);
let sourcePath, destinationPath, writeStream, ext;
let totalTransfers = 0;
let completedTransfers = 0;
for (let file of allFiles) {
ext = path.extname(file).toLowerCase();
if (ext === ".html" || ext === ".json") continue;
sourcePath = path.normalize(`${sourceDir}/${file}`);
const directoryExists = fs.existsSync(`${destinationDir}/assets`);
if (!directoryExists) fs.mkdirSync(`${destinationDir}/assets`);
destinationPath = path.normalize(`${destinationDir}/assets/${file}`);
const readStream = fs.createReadStream(sourcePath);
writeStream = fs.createWriteStream(destinationPath);
readStream.pipe(writeStream);
totalTransfers++;
writeStream.on("close", () => {
completedTransfers++;
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(
`\x1b[1m\x1b[31m${completedTransfers}/${totalTransfers} attachments transfered.\x1b[0m`
);
if (completedTransfers === totalTransfers) {
console.log(
"\n\x1b[92mTransfer completed. Attachments copied to Assets folder\x1b[0m\n"
);
}
});
}
};
module.exports = copyAssets;