-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (87 loc) · 2.83 KB
/
index.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
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
"use strict";
// Dependencies
const colors = require('colors/safe');
// Local dependencies
const cli = require('./cli-controller.js');
const csv = require('./csv.js');
// Global variables
let userInput = process.argv.slice(2);
let inputValues = {};
main();
async function main() {
initInputValues();
// If user passsed arguments
if (userInput.length) {
// If first argument is a flag
if (userInput[0][0] == '-') {
console.error(colors.red('\n**ERROR** First argument cannot be a flag.\n'));
process.exit(1);
}
// Parse args into inputValues
for (const inputId in cli.template) {
if (!userInput.length) {
break;
}
let inputObj = cli.parseOneArg({ userInput, inputId });
Object.assign(inputValues, inputObj);
}
// If user entered too many args, reset values
if (userInput.length) {
console.error(colors.red('\n**ERROR** Too many arguments\n'));
userInput = [];
initInputValues();
}
// Validation
for (const inputId in cli.template) {
let argValid = false;
if (!inputValues[inputId].value == '') {
argValid = validateInput({ inputId });
}
// Get user input if isn't valid or empty because user didn't provide enough args
while (!argValid) {
if (!argValid) {
await getUserInput({ inputId });
}
argValid = validateInput({ inputId });
}
}
} else {
for (const inputId in cli.template) {
await getUserInput({ inputId });
}
}
await csv.generateCsv({
inputPath: inputValues.inputPath.value,
flags: inputValues.inputPath.flags,
outputPath: inputValues.outputPath.value
});
console.log(colors.green('\nDone.\n'));
process.exit(0);
}
function initInputValues() {
inputValues = {};
for (const inputId in cli.template) {
inputValues[inputId] = {};
inputValues[inputId].value = '';
inputValues[inputId].flags = [];
}
}
async function getUserInput({ inputId }) {
let argValid = false;
// Continue to request input until user does it correctly
while (!argValid) {
// Request user input
userInput = await cli.get({ inputId });
// Parse input and add to inputValues
Object.assign(inputValues, cli.parseOneArg({ userInput, inputId }));
argValid = validateInput({ inputId });
}
}
function validateInput({ inputId }) {
if (cli.validate.args({ userInput, inputId, inputValues })
&& cli.validate.value({ inputId, inputValues })
&& cli.validate.flags({ inputId, inputValues })) {
return true;
}
return false;
}