-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromptInterface.js
executable file
·228 lines (210 loc) · 6.33 KB
/
promptInterface.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env node
'use-strict';
const path = require('path');
const emojis = require('node-emoji');
const lib = require('./index');
const minimist = require('minimist');
const inquirer = require('inquirer');
const HELP_FILE = './config/help.txt';
const opts = minimist(process.argv.slice(2));
const defaultMessage = `${emojis.get(
'no_entry'
)} \x20please send a valid command or try with 'hi' or 'example'`;
const launchErrorMessageAndExit = message => {
console.error(`${emojis.get('warning')}\x20 ${message}`);
process.exit(1);
};
const checkFileName = fileName => {
const pattern = /^[0-9a-zA-Z_]+$/;
if (!pattern.test(fileName)) {
launchErrorMessageAndExit(
'the file name can only contain alpha-numeric characters for example "FileName_1"'
);
}
return true;
};
const help = () => {
let helpFileData = lib.readFile(HELP_FILE);
const params = helpFileData.match(/(\{\{)(.*?)(\}\})/g).map(word => {
return word.replace(/{{|}}/g, '');
});
for (let index in params) {
helpFileData = helpFileData.replace(
`{{${params[index]}}}`,
`${emojis.get(params[index])}\x20`
);
}
console.log(helpFileData);
};
const showMe = () => {
console.log(`executing with EXAMPLE file... ${emojis.random().emoji}`);
console.time('time-process');
const result = lib.executeExample();
console.log('File data: ');
console.log(result.fileData);
console.log('String number: ');
console.log(result.stringNumber);
console.timeEnd('time-process');
};
const createFile = options => {
if (!options.name) {
launchErrorMessageAndExit(
'this option require file "--name" as parameter (without extension file)'
);
}
if (checkFileName(options.name)) {
if (options.name.toLowerCase().includes('cancel')) {
console.log('"Cancel" is a reserved work and it can´t be used');
return;
}
const result = lib.createNewFile(options.name, options.number || null);
console.log(`File ${options.name} created and added to list: `);
console.log(result);
console.log('try run "sspe list" for see the files list');
}
};
const listFiles = async () => {
const filesList = await lib.listFiles();
if (!filesList || filesList.length === 0) {
launchErrorMessageAndExit(
'There are no previously generated files. Try to generate one with "sspe create --name MyFileName"!'
);
return;
}
const cancelOption = `CANCEL ${emojis.get('back')}`;
filesList.push(cancelOption);
inquirer
.prompt({
type: 'list',
name: 'fileName',
message: 'Select the file you want to process',
choices: filesList,
})
.then(({ fileName }) => {
if (fileName === cancelOption) {
console.log('The action was canceled!');
return;
}
const filePath = path.join(__dirname, `/files/${fileName}`);
console.log(`Executing with file ${fileName} ${emojis.get('coffee')}`);
console.time('time-process');
const result = lib.processFile(filePath);
console.log('File data: ');
console.log(result.fileData);
console.log('String number: ');
console.log(result.stringNumber);
console.timeEnd('time-process');
})
.catch(error => {
launchErrorMessageAndExit(
`Something went wrong. Error: ${error.message}`
);
});
};
const processSelectedFile = options => {
if (!options.path) {
launchErrorMessageAndExit('this option require file "--path" as parameter');
}
console.log(`Executing with file ${options.path} ${emojis.get('coffee')}`);
console.time('time-process');
const result = lib.processFile(options.path);
console.log('File data: ');
console.log(result.fileData);
console.log('String number: ');
console.log(result.stringNumber);
console.timeEnd('time-process');
};
const addDataForTraining = options => {
console.log(
`Adding number ${options.add || 'aleatory'} to training file ${emojis.get(
'robot_face'
)}`
);
console.time('time-process');
const result = lib.addDataForTraining(options.add);
console.log(result.message);
console.log('File data: ');
console.log(result.data);
console.timeEnd('time-process');
};
const doTraining = options => {
inquirer
.prompt({
type: 'confirm',
name: 'action',
message: `¿You want to run the network training${
options.iter ? ' with ' + options.iter.toString() + ' iterations?' : '?'
}`,
})
.then(({ action }) => {
if (action) {
console.time('time-process');
const result = lib.doTraining(options.iter, options.error);
console.log(result.message);
console.timeEnd('time-process');
} else {
console.log(`Okay, don't panic, nothing's gonna happen.`);
process.exit(1);
}
});
};
const analiceFile = options => {
console.log(
`Executing net with file ${options.path} ${emojis.get('coffee')}`
);
console.time('time-process');
const result = lib.analiceFile(options.path);
console.log('File data: ');
console.log(result.fileData);
console.log('String number: ');
console.log(result.stringNumber);
console.timeEnd('time-process');
};
const neuralNetWork = options => {
if (options.add === 0 || options.add) {
addDataForTraining(options);
return;
} else if (options.path) {
analiceFile(options);
return;
} else if (Object.keys(options).length === 1 || options.iter) {
doTraining(options);
return;
}
launchErrorMessageAndExit(
'the option for the "ml" action are not recognized.'
);
};
const noEntry = () => {
console.log(defaultMessage);
};
const processCommand = async options => {
const command = options._[0].toLowerCase();
const selected = {
hi: help,
showme: showMe,
create: createFile,
list: listFiles,
select: processSelectedFile,
ml: neuralNetWork,
default: noEntry,
};
return selected[command] ? selected[command](options) : selected.default();
};
const main = async () => {
if (!process.argv[2]) {
console.error(defaultMessage);
process.exit(1);
}
processCommand(opts);
};
main();
// // For debugger only
// (async function main() {
// // const options = { _: ['create'], name: 'File_Funny', number: 1234567890 };
// // const options = { _: ['list'] };
// // const options = { _: ['showme'], };
// // const options = { _: ['ml'], add: 'auto' };
// const options = { _: ['hi'] };
// processCommand(options);
// })();