-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.input.js
61 lines (59 loc) · 1.73 KB
/
get.input.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import prompt from "prompt";
prompt.message = "Nyatictl";
const cache = {};
const isEmpty = (str) => {
return str === "" || str === null || str === undefined;
};
export default async function getInput(client, opt) {
const { label, name, required = true, hidden = false } = opt;
let value = client.server[name];
const [next_value, optional] = await parseEnv(client, name, value);
value = next_value;
if (!isEmpty(value)) return value;
if (optional) return undefined;
value = await readInput(label, name, required, hidden);
return value;
}
async function readInput(label, name, required, hidden) {
const options = {
name: "value",
description: `Enter '${label}' ${name}`,
hidden: !!hidden,
replace: hidden ? "*" : undefined,
required: required,
type: "string",
};
const result = await prompt.get([options]).catch((err) => false);
if (result == false) {
console.log("");
process.exit(0);
}
return result.value;
}
async function parseEnv(client, name, value) {
let _env_name = null;
let optional = false;
if (isEmpty(value)) _env_name = `NYATI_${name}`;
if (!isEmpty(value) && value.toLowerCase().indexOf("#env:") == 0) {
_env_name = `NYATI_${value.split("#env:")[1]}`;
} else if (!isEmpty(value) && value.toLowerCase().indexOf("#env?:") == 0) {
_env_name = `NYATI_${value.split("#env?:")[1]}`;
optional = true;
} else {
return [value, optional];
}
let nvalue = client.env[_env_name];
if (nvalue == undefined && !optional)
console.log(` ${_env_name} not defined`);
// console.log(
// "get: ",
// name,
// ", env_name:",
// _env_name,
// ", client:",
// client.env,
// " value: ",
// nvalue
// );
return [nvalue, optional];
}