-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
86 lines (75 loc) · 1.87 KB
/
Copy pathutils.js
File metadata and controls
86 lines (75 loc) · 1.87 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const fs = require('fs-extra')
const shell = require('shelljs')
const yaml = require('js-yaml')
const toml = require('@iarna/toml')
const request = require('sync-request');
const chainHostName = "cess-chain"
const apiUrls = [
"https://api.seeip.org",
"https://api.ipify.org?format=json",
"https://api.my-ip.io/ip",
"https://ip.zxinc.org/getip",
"https://ip.3322.net/",
"https://api.ipify.org?format=text",
"https://api.ip.sb/ip"
];
function getPublicEndpoint(port) {
for (const url of apiUrls) {
try {
const res = request('GET', url);
if (res.statusCode !== 200) continue;
const contentType = res.headers['content-type'];
if (contentType && contentType.includes("application/json")) {
const data = JSON.parse(res.getBody('utf8'));
const ip = data.ip || data;
return `${ip}:${port}`;
} else {
const ip = res.getBody('utf8').trim();
return `${ip}:${port}`;
}
} catch (error) {
}
}
return `127.0.0.1:${port}`;
}
async function createDir(dir) {
if(shell.mkdir('-p', dir).code !== 0) {
throw `failed to create directory: ${dir}`
}
}
async function writeConfig(path, cfg) {
if(path.endsWith(".yaml")) {
return writeYaml(path, cfg);
}
else if(path.endsWith(".toml")) {
return writeToml(path, cfg);
}
return writeJson(path, cfg);
}
async function writeJson(path, cfg) {
await fs.outputJson(path, cfg, {
spaces: 2,
})
return true
}
async function writeYaml(path, cfg) {
await fs.outputFile(path, yaml.safeDump(cfg, {ident: 2, lineWidth: -1}))
return true
}
async function writeToml(path, cfg) {
await fs.outputFile(path, toml.stringify(cfg))
return true
}
function imageTagByProfile(profile) {
return profile
}
module.exports = {
createDir,
writeConfig,
writeJson,
writeYaml,
writeToml,
imageTagByProfile,
chainHostName,
getPublicEndpoint,
}