This repository was archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
127 lines (107 loc) · 2.88 KB
/
utils.js
File metadata and controls
127 lines (107 loc) · 2.88 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require("fs");
async function prompt(question, password = false) {
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const promise = new Promise((resolve) => {
readline.question(question, (answer) => {
if (password) readline.close();
resolve(answer);
});
});
if (password)
readline._writeToOutput = function _writeToOutput(stringToWrite) {
readline.output.write("*");
};
const result = await promise;
readline.close();
return result;
}
async function yesOrNo(question) {
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const promise = new Promise((resolve) => {
readline.question(question, (answer) => {
readline.close();
resolve(answer);
});
});
const result = await promise;
return result.toLowerCase().startsWith("y");
}
const promisifyStream = (stream, send = "") => {
return new Promise((resolve) => {
let data = "";
stream.on("data", (chunk) => {
data += chunk.toString();
});
stream.on("end", () => {
resolve(data);
});
if (send) stream.write(send);
});
};
async function supportedContainers(docker, verbose) {
//load all engines
const engines = fs.readdirSync("./engines").map((f) => f.split(".")[0]);
const list = await docker.container.list();
if (verbose) console.log(list);
const dbPorts = [];
//loop through all engines
for (const engine of engines) {
const engineModule = require(`./engines/${engine}`);
const runningDatabases = await engineModule.detectRunning(list);
dbPorts.push(...runningDatabases.map((c) => ({ type: engine, data: c })));
}
return dbPorts;
}
async function choose(options) {
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const promise = new Promise((resolve) => {
readline.question(
options.map((o, i) => `${i + 1}. ${o}`).join("\n") + "\n",
(answer) => {
readline.close();
resolve(answer);
}
);
});
const result = await promise;
return options[result - 1];
}
function randomString(length) {
const chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
async function findFreePort() {
const net = require("net");
return new Promise((resolve, reject) => {
const server = net.createServer();
server.unref();
server.on("error", reject);
server.listen(0, () => {
const port = server.address().port;
server.close(() => resolve(port));
});
});
}
module.exports = {
prompt,
yesOrNo,
promisifyStream,
supportedContainers,
choose,
randomString,
findFreePort,
};