-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
55 lines (49 loc) · 1.25 KB
/
utils.js
File metadata and controls
55 lines (49 loc) · 1.25 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
import fs from "fs";
import path from "path";
import ProgressBar from "progress";
import fetch from "node-fetch";
export const formatNumber = (number) => number.toLocaleString();
export const fetchJson = async (url) => {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch ${url}: ${response.status} ${response.statusText}`
);
}
return await response.json();
};
export const createProgressBar = (total) => {
const progressBar = new ProgressBar(`[:bar] :current/:total :percent`, {
total,
width: 40,
complete: "=",
incomplete: " ",
});
return progressBar;
};
const isExists = async (path) => {
try {
await fs.access(path);
return true;
} catch {
return false;
}
};
export const writeFile = async (filePath, data) => {
try {
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
await fs.promises.writeFile(filePath, data);
} catch (err) {
throw new Error(err);
}
};
export const chunk = (array, chunkCount) => {
const chunkSize = Math.ceil(array.length / chunkCount);
const chunks = [];
let index = 0;
while (index < array.length) {
chunks.push(array.slice(index, index + chunkSize));
index += chunkSize;
}
return chunks;
};