/
prefixer.js
executable file
·70 lines (58 loc) · 1.85 KB
/
prefixer.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
70
#!/usr/bin/env node
var cmdopt = require('cmdopt'),
getOpts = new cmdopt.Parser(),
opts,
polsky = require('./polsky'),
fs = require('fs'),
fileName,
reduce = false,
purple = '\u001b[1;35m',
yellow = '\u001b[1;33m',
reset = '\u001b[0m';
getOpts.option('-h, --help', 'Show this.');
getOpts.option('-r, --reduce', 'Will attempt to reduce the equation when integer math is possible.');
try {
opts = getOpts.parse(process.argv.slice(2));
if (opts.help) {
process.stdout.write(getOpts.help());
process.exit();
} else if (opts.r || opts.reduce) {
reduce = true;
}
} catch (optex) {
if (optex instanceof cmdopt.ParseError) {
process.stdout.write(optex.message + '\n');
process.exit(1);
}
throw optex;
}
fileName = process.argv.slice(2).pop();
if (!fileName) {
process.stdout.write('Please provide a file to process\n');
process.exit(1);
}
fs.exists(fileName, function (exists){
if (!exists) {
process.stdout.write('File not found: ' + fileName + '\n');
process.exit(1);
} else {
fs.readFile(fileName, 'utf8', function (err, data) {
var EOL = data.indexOf("\r\n") >= 0 ? "\r\n" : "\n",
lineEnd = new RegExp(EOL, 'g'),
parser,
lines;
lines =data.split(lineEnd);
lines.pop(); //drop last EOL
lines.forEach(function(expr){
var output = '';
try {
parser = new polsky.Parser(expr);
output = purple + expr + reset + ' -> ' + yellow + parser.print(reduce) + reset;
} catch (e) {
process.stdout.write('Error encountered: ' + e.message + EOL);
}
process.stdout.write(output + EOL);
});
});
}
});