-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
49 lines (39 loc) · 1.02 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
'use strict';
var lambdaCalculator = require('./index.js');
function runWithTTY() {
var readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
readline.on('line', function (line) {
try {
console.log(lambdaCalculator.interpret(line));
} catch (e) {
console.log(e.toString());
}
readline.prompt();
});
readline.on('close', function () {
console.log(''); // Print a newline to clean up
});
// Synchronously executed
readline.prompt();
}
function runWithStream() {
var input = '';
process.stdin.on('data', function (chunk) {
input += chunk;
});
process.stdin.on('end', function () {
try {
console.log(lambdaCalculator.interpret(input));
} catch (e) {
console.log(e.toString());
}
});
}
if (process.stdin.isTTY === true && process.stdout.isTTY === true) {
runWithTTY();
} else {
runWithStream();
}