-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
42 lines (32 loc) · 916 Bytes
/
util.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
import path from 'path';
import url from 'url';
import util from 'util';
import module from 'module';
import TcpPing from 'tcp-ping';
export function dirname(importMetaUrl) {
return path.dirname(url.fileURLToPath(importMetaUrl));
}
export async function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function stringifyError(error) {
return JSON.stringify(error, Object.getOwnPropertyNames(error));
}
export async function ping({ ipAddress: address, timeout = 5 * 100, attempts = 10 }) {
let tcpPing = util.promisify(TcpPing.ping);
try {
await tcpPing({
address,
timeout,
attempts,
});
return true;
} catch (err) {
return false;
}
}
export async function getNodeModulePath(moduleName) {
const require = module.createRequire(import.meta.url);
const pathName = path.dirname(require.resolve(moduleName));
return pathName;
}