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

src/Cli/index.js   A

Complexity

Total Complexity 6
Complexity/F 1.2

Size

Lines of Code 58
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 6
c 1
b 0
f 0
nc 1
mnd 1
bc 7
fnc 5
dl 0
loc 58
rs 10
bpm 1.4
cpm 1.2
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ??? 0 3 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