-
Notifications
You must be signed in to change notification settings - Fork 0
/
brlc-converter.js
46 lines (39 loc) · 1.37 KB
/
brlc-converter.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
const {program} = require("commander");
const {name, description, version} = require("./package.json");
const fs = require("fs");
const path = require("path");
const convert = require("./convert");
program
.name(name)
.description(description)
.version(version)
.option("-f, --from <format>", "The braille encoding of the input file", "unicode")
.option("-t, --to <format>", "The braille encoding of the output file", "unicode")
.requiredOption("-i, --input <file>", "Path to the file to be converted")
.option("-o, --output <file>", "Path to the output file")
.showHelpAfterError();
const args = program.parse().opts()
const dataPath = path.dirname(process.execPath);
let inFormat = args.from;
if (inFormat != "unicode") {
inFormat = require(path.join(dataPath, "data", `${inFormat}.json`));
}
let outFormat = args.to;
if (outFormat != "unicode") {
outFormat = require(path.join(dataPath, "data", `${outFormat}.json`));
}
let inText = fs.readFileSync(args.input);
let outText = convert(inFormat, outFormat, inText);
let fileName = ""
if ("output" in args) {
fileName = args.output;
} else {
fileName = args.input.replace(/\.\w+$/, ""); // Remove file extension
if (outFormat == "unicode") {
fileName += ".txt";
} else {
fileName += `.${outFormat.format}`;
}
}
fs.writeFileSync(fileName, outText);
console.log("Done!");