-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
executable file
·69 lines (60 loc) · 2.1 KB
/
cli.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
#!/usr/bin/env node
'use strict';
const interviewQuestion = require('./index.js');
const getThemArgs = require('get-them-args');
const args = getThemArgs(process.argv.slice(2));
const verbose = args.verbose || false;
const filter = args.top ? 'top' : args.filter || 'all';
const amount = args.top || '';
const level = args.level || '';
const format = args.format || 'text';
const theme = args.theme || '';
const logHeader = (header) => console.log('\x1b[32m%s\x1b[0m', header);
const logDivider = () => console.log('_________________________');
const logExampleParams = () => {
console.log("\x1b[32m%s\x1b[0m", "Params example:");
console.log("--top ${amount}");
console.log("--level ( basic | intermediate | advanced )");
console.log("--theme ( algorithm, closures | storage | es6 | classes | database ... and other 203 themes)");
console.log("--format ( text | array | json )");
};
const displayQuestions = (result, format) => {
switch (format) {
case 'json':
console.log(JSON.stringify(result));
break;
case 'array':
console.log(result);
break;
default:
result.forEach(({ title, url, text, theme }) => {
console.log('');
logHeader(`Question: ${title}`);
if (url) {
console.info(`Answer: \x1b]8;;${url}\x1b\\${url}\x1b]8;;\x1b\\`);
} else {
console.info(`Answer: ${text}`);
}
console.info(`Hash: ${theme.split(',').map((data) => ` #${data.trim().toLowerCase()}`)}`);
logDivider();
});
}
};
const main = async () => {
try {
logHeader('Question:');
if (amount) console.log('Top -', amount);
if (level) console.log('Level -', level);
if (filter) console.log('Filter -', filter);
console.log('');
const result = await interviewQuestion({ filter, amount, level, format, theme });
displayQuestions(result, format);
console.log('');
console.log(`Found ${result.length} questions`);
logExampleParams();
if (verbose) console.log('Send questions');
} catch (error) {
if (verbose) console.error(`Could not send questions: ${error.message}`);
}
};
main();