-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.mjs
40 lines (33 loc) · 1.21 KB
/
util.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
import fs from 'fs';
import chalk from 'chalk';
import fetch from 'node-fetch';
export function getUrlFileName(url) {
const pathName = new URL(url).pathname;
const index = pathName.lastIndexOf('/');
return -1 !== index ? pathName.substring(index + 1) : pathName;
}
export async function downloadFile(path, url) {
const response = await fetch(url);
const fileStream = fs.createWriteStream(path);
return new Promise((resolve, reject) => {
response.body.pipe(fileStream);
response.body.on('error', reject);
fileStream.on('finish', resolve);
});
}
export async function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
export function getArrayLowerCase(array) {
return array?.map((element) => element.toString().toLowerCase());
}
export async function asyncMapFilter(array, mapFunction, filterFunction = (element) => element) {
return (await Promise.all(array.map(mapFunction))).filter(filterFunction);
}
export function error(message, exitCode = undefined) {
console.error(`${chalk.bgRedBright(' error ')} ${message}`);
if (exitCode) process.exit(exitCode);
}
export function warn(message) {
console.warn(`${chalk.bgYellow(' warn ')} ${message}`);
}