Completed
Push — master ( 579bab...0a200f )
by Mathias
01:14
created

index.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 1
c 5
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
'use strict';
2
3
import * as fs from 'fs';
4
import * as readline from 'readline';
5
6
import Botlang from '../Botlang';
7
import pkg from '../../package.json';
8
import Styles from './Styles';
9
10
const options = new Map()
11
        .set('botname', 'Bot')
12
        .set('path', process.argv[process.argv.length - 1])
13
        .set('prompt', '> ')
14
        .set('system', 'Sys')
15
        .set('username', 'You'),
16
      rl = readline.createInterface({
17
        input  : process.stdin,
18
        output : process.stdout
19
      }),
20
      sourceCode = fs.readFileSync(options.get('path'), {
21
        encoding : 'utf8',
22
        flag     : 'r'
23
      }),
24
      bot = new Botlang(sourceCode),
25
      write = (text) => {
26
        process.stdout.write(`${text}`);
27
      },
28
      writeBot = (text) => {
29
        write(`${Styles.green.open}[${options.get('botname')}]:${Styles.green.close} ${text}`);
30
      },
31
      writeSys = (text) => {
32
        write(`${Styles.green.open}[${options.get('system')}]:${Styles.green.close} ${text}`);
33
      };
34
35
write(`\n${Styles.yellow.open}`);
36
write(`🤖 Welcome to botlang#${pkg.version}\n`);
37
write(`   Brain from "${options.get('path')}" loaded\n`);
38
write(`${Styles.yellow.close}\n`);
39
40
rl.setPrompt(`${Styles.green.open}[${options.get('username')}]:${Styles.green.close} `);
41
rl.prompt();
42
43
rl.on('line', (line) => {
44
  const input = line.trim();
45
46
  if (/\\help/.test(input)) {
47
    writeSys('Not implemented yet. Read the source code in the meantime ...\n');
48
  } else {
49
    writeBot(`${bot.reply(input)}\n`);
50
  }
51
52
  rl.prompt();
53
});
54
55
rl.on('close', () => {
56
  write(`\n\n${Styles.yellow.open}🤖 Bye bye ...${Styles.yellow.close}\n`);
57
  process.exit(0);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
58
});
59