-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
67 lines (58 loc) · 1.75 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
var concat = require('concat-stream');
var fs = require('fs');
var program = require('commander');
var pkg = require('./package.json');
var opt = require('./index');
program
.version(pkg.version)
.option('-i, --infile [file]', 'set the filename for input (default: stdin)')
.option('-o, --outfile [file]', 'set the filename for output (default: stdout)')
.option('-f, --format [format]', 'set the input and output MML format')
.option('-I, --infmt [format]', 'set the input MML format')
.option('-O, --outfmt [format]', 'set the output MML format')
.option('-t, --transpose [steps]', 'transpose the input MML up this many steps');
program.on('--help', function () {
console.log(' Supported formats:');
console.log();
console.log(' aa ArcheAge (default)');
console.log(' mabi Mabinogi');
});
program.parse(process.argv);
if (program.format && program.infmt) {
console.error('Error: cannot use both --format and --infmt');
process.exit(1);
}
if (program.format && program.outfmt) {
console.error('Error: cannot use both --format and --outfmt');
process.exit(1);
}
var options = {
input: program.format || program.infmt || 'aa',
output: program.format || program.outfmt || 'aa',
transpose: parseInt(program.transpose, 10) || 0
};
if (program.infile) {
fs.readFile(program.infile, 'utf8', function (err, data) {
if (err) {
console.error(err);
process.exit(1);
}
run(data);
});
} else {
process.stdin.pipe(concat({ encoding: 'string' }, run));
}
function run(mml) {
var optimized = opt(mml, options);
if (program.outfile) {
fs.writeFile(program.outfile, optimized, 'utf8', function (err) {
if (err) {
console.error(err);
process.exit(1);
}
});
} else {
process.stdout.write(optimized+"\n");
}
}