-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
58 lines (53 loc) · 1.45 KB
/
utils.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
const readline = require("readline");
/**
* Get input from terminal
* @param {string} questionText
* @returns Promise
*/
const ask = (questionText) => {
return new Promise((resolve, reject) => {
const _ask = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
_ask.question(questionText, (input) => resolve(input));
});
}
/**
* Check the str is a valid .env variable.
* If str is a valid .env handleEnv will returns
* the variable and it's value as an object
* @param {Object} {line}
* @param {string} line.str
* @returns object
*/
const handleEnv = ({ str = null }) => {
if (typeof str != "string") {
throw new Error(`❗: ${str} must be a string`);
}
const splitted = str.split('=');
if (splitted.length < 2) {
throw new Error(`❗❗: Your input malformed. It must be like "MY_VAR=MY_VALUE"`);
}
const extractValue = str.replace(`${splitted[0]}=`,"");
return { ENV_VAR: splitted[0], ENV_VAL: extractValue };
}
/**
* Check if str is a comment line
* @param {Object} {line}
* @param {string} line.str
* @returns {boolean}
*/
const isCommentLine = ({str=null}) => {
if (typeof str != "string") {
throw new Error(`${str} must be a string`);
}
const removeSpaces = str.replace(/\s/g, "");
return removeSpaces.startsWith("#");
}
module.exports = {
ask,
handleEnv,
isCommentLine
}