1 | 'use strict'; |
||
2 | |||
3 | import * as fs from 'fs'; |
||
4 | import * as readline from 'readline'; |
||
5 | |||
6 | import Botlang from '../Botlang'; |
||
7 | import Styles from './Styles'; |
||
8 | |||
9 | if ('undefined' === typeof process.argv[2]) { |
||
10 | process.stdout.write(`${Styles.red.open}No botscript has been defined.${Styles.red.close}\n`); |
||
11 | process.stdout.write(`${Styles.red.open}Usage: "/botlang <path-to-your-botlang-script>"${Styles.red.close}\n`); |
||
12 | process.exit(1); |
||
0 ignored issues
–
show
Compatibility
Debugging Code
Best Practice
introduced
by
![]() |
|||
13 | } else if (-1 !== ['-h', '--help'].indexOf(process.argv[2])) { |
||
14 | process.stdout.write('Usage: botlang <script.bot>\n\n'); |
||
15 | process.stdout.write('Options:\n'); |
||
16 | process.stdout.write(' -h, --help Show this help context\n'); |
||
17 | process.stdout.write(' -v, --version Print botlang version\n\n'); |
||
18 | process.stdout.write('Learn more on https://botlang.org\n'); |
||
19 | process.exit(0); |
||
0 ignored issues
–
show
Compatibility
Debugging Code
Best Practice
introduced
by
|
|||
20 | } else if (-1 !== ['-v', '--version'].indexOf(process.argv[2])) { |
||
21 | process.stdout.write(`v${Botlang.version()}\n`); |
||
22 | process.exit(0); |
||
0 ignored issues
–
show
Compatibility
Debugging Code
Best Practice
introduced
by
|
|||
23 | } else if (!fs.existsSync(process.argv[2])) { |
||
24 | process.stdout.write(`${Styles.red.open}File "${process.argv[2]}" does not exist.${Styles.red.close}\n`); |
||
25 | process.stdout.write(`${Styles.red.open}Type "botlang --help" to print the help context.${Styles.red.close}\n`); |
||
26 | process.exit(1); |
||
0 ignored issues
–
show
Compatibility
Debugging Code
Best Practice
introduced
by
|
|||
27 | } |
||
28 | |||
29 | const options = new Map() |
||
30 | .set('botname', 'Bot') |
||
31 | .set('path', process.argv[2]) |
||
32 | .set('prompt', '> ') |
||
33 | .set('system', 'Sys') |
||
34 | .set('username', 'You'), |
||
35 | rl = readline.createInterface({ |
||
36 | input : process.stdin, |
||
37 | output : process.stdout |
||
38 | }), |
||
39 | sourceCode = fs.readFileSync(options.get('path'), { |
||
40 | encoding : 'utf8', |
||
41 | flag : 'r' |
||
42 | }), |
||
43 | bot = new Botlang(sourceCode), |
||
44 | write = (text) => { |
||
45 | process.stdout.write(`${text}`); |
||
46 | }, |
||
47 | writeBot = (text) => { |
||
48 | write(`${Styles.green.open}[${options.get('botname')}]:${Styles.green.close} ${text}`); |
||
49 | }, |
||
50 | writeSys = (text) => { |
||
51 | write(`${Styles.green.open}[${options.get('system')}]:${Styles.green.close} ${text}`); |
||
52 | }; |
||
53 | |||
54 | write(`\n${Styles.yellow.open}`); |
||
55 | write(`🤖 Welcome to botlang#${Botlang.version()}\n`); |
||
56 | write(` Brain from "${options.get('path')}" loaded\n`); |
||
57 | write(`${Styles.yellow.close}\n`); |
||
58 | |||
59 | rl.setPrompt(`${Styles.green.open}[${options.get('username')}]:${Styles.green.close} `); |
||
60 | rl.prompt(); |
||
61 | |||
62 | rl.on('line', (line) => { |
||
63 | const input = line.trim(); |
||
64 | |||
65 | if ('' === input) write(); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
66 | else if (/\\help/.test(input)) writeSys('Not implemented yet. Read the source code in the meantime ...\n'); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
67 | else if (/\\version/.test(input)) writeSys(`${Botlang.version()}\n`); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
68 | else writeBot(`${bot.reply(input)}\n`); |
||
69 | |||
70 | rl.prompt(); |
||
71 | }); |
||
72 | |||
73 | rl.on('close', () => { |
||
74 | write(`\n\n${Styles.yellow.open}🤖 Bye bye ...${Styles.yellow.close}\n`); |
||
75 | process.exit(0); |
||
0 ignored issues
–
show
Compatibility
Debugging Code
Best Practice
introduced
by
|
|||
76 | }); |
||
77 |