-
Notifications
You must be signed in to change notification settings - Fork 21
/
generator.js
89 lines (71 loc) · 2.1 KB
/
generator.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
var fs = require("fs");
var process = require("process");
var program = require("commander");
var package = require("./package.json");
function prepareCLIOptions() {
program
.version("1.0.0")
.usage("[options] files")
.option("-o, --output <output file>", "Specify output file")
.option("-p, --platform <JSON file>", "Specify platform JSON file")
.parse(process.argv)
}
function prepareFileList() {
var fileList = package.keybindings;
if (program.args.length) {
fileList = fileList.concat(program.args);
}
return fileList;
}
function applyPlatformJSON(prev, platform) {
var keybindings = prev;
var length = keybindings.length;
var unshiftKeybinds = [];
for (pf of platform) {
for (var i = 0; i < length; i++) {
var k = keybindings[i];
if (k.key === pf.key && k.command === pf.command && k.when === pf.when) {
keybindings[i] = pf;
break;
}
}
unshiftKeybinds.push(pf);
}
keybindings = unshiftKeybinds.concat(keybindings);
return keybindings;
}
function sortKeybindings(keybindings) {
keybindings.sort((aKey, bKey) => {
if ((!aKey.when && !bKey.when) || (aKey.when && bKey.when)) {
return 0;
} else if (!aKey.when) {
return -1;
} else {
return 1;
}
});
}
function readJSONFiles(fileList, platformFile) {
var keybindings = [];
for (fileName of fileList) {
var buffer = fs.readFileSync(fileName, "utf8");
keybindings = keybindings.concat(JSON.parse(buffer));
}
if (platformFile !== undefined) {
var buffer = fs.readFileSync(fileName, "utf8");
keybindings = applyPlatformJSON(keybindings, JSON.parse(buffer));
}
sortKeybindings(keybindings);
return keybindings;
}
function outputJSONFile(keybindings) {
if (program.output !== undefined) {
var stream = fs.createWriteStream(program.output);
process.stdout.write = process.stderr.write = stream.write.bind(stream);
}
console.log(JSON.stringify(keybindings, null, 4));
}
prepareCLIOptions();
var fileList = prepareFileList();
var keybindings = readJSONFiles(fileList, program.platform);
outputJSONFile(keybindings);