-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyAssets.mjs
55 lines (51 loc) · 1.53 KB
/
copyAssets.mjs
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
import {
mkdir,
readdir,
stat,
copyFile,
writeFile,
readFile,
} from "fs/promises";
import { join, extname } from "path";
const sourceDir = "./src/locales";
const destDir = "./dist/locales";
// Function to minify JSON content
function minifyJSON(content) {
return JSON.stringify(JSON.parse(content));
}
// Function to recursively copy directories
async function copyDirAsync(src, dest) {
try {
await mkdir(dest, { recursive: true });
const files = await readdir(src);
await Promise.all(
files.map(async (file) => {
const srcPath = join(src, file);
const destPath = join(dest, file);
const fileStat = await stat(srcPath);
if (fileStat.isDirectory()) {
// Recursively copy subdirectories
await copyDirAsync(srcPath, destPath);
} else {
// Check if it's a JSON file
if (extname(file) === ".json") {
// Read JSON content
const content = await readFile(srcPath, "utf-8");
// Minify JSON content
const minifiedContent = minifyJSON(content);
// Write minified JSON to destination
await writeFile(destPath, minifiedContent);
} else {
// Copy files
await copyFile(srcPath, destPath);
}
}
}),
);
console.log("JSON files copied successfully!");
} catch (error) {
console.error("Error copying JSON files:", error);
}
}
// Copy the source directory to the destination directory
await copyDirAsync(sourceDir, destDir);