-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·103 lines (95 loc) · 3.46 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
const pgn = require("./parsers/pgn.js");
const game = require("./parsers/game.js");
const axel = require("./parsers/axel.js");
const json = require("./parsers/json.js");
const alexbay = require("./parsers/alexbay.js");
const preview = require("./preview.js");
const fs = require("fs");
const yargs = require("yargs");
yargs.scriptName("5dchess-notation").command("convert <from> <to> <file>", "Convert from <from> to <to>", (y) => {
y.positional("from", {
describe: "the notation to convert from (shad, json, axel, alexbay)"
}).positional("to", {
describe: "the notation to convert into (shad, json, axel, alexbay)"
}).positional("file", {
describe: "the file to read from"
}).option("v", {
alias: "v",
default: false,
type: "boolean",
describe: "more verbose parsing/writing"
}).option("board", {
default: "Standard",
describe: "The board that is played on (used for axel)",
})
}, (argv) => {
let g;
let raw = fs.readFileSync(argv.file, "utf8");
let from = argv.from.toLowerCase();
let to = argv.to.toLowerCase();
if (from === "shad") {
g = pgn.parse(raw, argv.verbose || false);
} else if (from === "json") {
g = json.parse(raw);
} else if (from === "4xel" || from === "axel") {
g = axel.parse(raw, argv.verbose, argv.board);
} else if (from === "alexbay") {
g = alexbay.parse(raw, argv.verbose, false);
} else {
throw new Error("No notation named " + from + " found!");
}
if (to === "shad") {
console.log(pgn.write(g));
} else if (to === "json") {
console.log(JSON.stringify(g));
} else if (to === "4xel" || to === "axel") {
console.log(axel.write(g));
} else if (to === "alexbay") {
console.log(alexbay.write(g, argv.verbose, false));
} else {
throw new Error("No notation named " + to + " found!");
}
}).command("preview <format> <file>", "Previews the given game", (y) => {
y.positional("format", {
describe: "The format that <file> is in (shad, json, axel, alexbay)",
}).positional("file", {
describe: "The file to read from"
}).option("board", {
default: "Standard",
describe: "The board that is played on (used for axel's notation)",
}).option("unicode", {
default: false,
type: "boolean",
describe: "Use unicode values for chess pieces (U+2654 through U+265F, Unicorns and Dragons will still be displayed with latin letters)"
}).option("multi", {
default: false,
type: "boolean",
describe: "Enable multi-board preview"
}).option("black-bg", {
default: false,
type: "boolean",
describe: "Puts a black background behind each board (multi)"
}).option("princess-to-queen", {
alias: "q",
default: false,
type: "boolean",
describe: "Turn princesses into queens (used for alexbay; tries to convert back when parsing alexbay's notation, but cannot revert the information loss)"
})
}, (argv) => {
let g;
let raw = fs.readFileSync(argv.file, "utf8");
let format = argv.format.toLowerCase();
if (format === "shad") {
g = pgn.parse(raw, argv.verbose || false);
} else if (format === "json") {
g = json.parse(raw);
} else if (format === "4xel" || format === "axel") {
g = axel.parse(raw, argv.verbose, argv.board);
} else if (format === "alexbay") {
g = alexbay.parse(raw, argv.verbose, argv.princessToQueen);
} else {
throw new Error("No notation named " + format + " found!");
}
preview.preview(g, argv.unicode, argv.multi, argv["black-bg"]);
}).demandCommand().argv;